Skip to content

Commit

Permalink
moved gen_salt to the Base module
Browse files Browse the repository at this point in the history
  • Loading branch information
riverrun committed Jan 20, 2022
1 parent 5a987f8 commit 0c865e5
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 52 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v3.0.0 (2022-01-20)

* Changes
* moved `gen_salt` to the `Base` module

## v2.3.1 (2022-01-19)

* Changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ the `$2y$` prefix, see [this issue](https://github.com/riverrun/comeonin/issues/
```elixir
def deps do
[
{:bcrypt_elixir, "~> 2.0"}
{:bcrypt_elixir, "~> 3.0"}
]
end
```
Expand Down
22 changes: 2 additions & 20 deletions lib/bcrypt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ defmodule Bcrypt do
@moduledoc """
Elixir wrapper for the Bcrypt password hashing function.
Most applications will just need to use the `add_hash/2` and `check_pass/3`
convenience functions in this module.
For a lower-level API, see `Bcrypt.Base`.
## Configuration
Expand Down Expand Up @@ -36,7 +33,7 @@ defmodule Bcrypt do
It is also possible to generate hashes with the `$2a$` prefix by running
the following command:
Bcrypt.Base.hash_password("hard to guess", Bcrypt.gen_salt(12, true))
Bcrypt.Base.hash_password("hard to guess", Bcrypt.Base.gen_salt(12, true))
This option should only be used if you need to generate hashes that are
then checked by older libraries.
Expand All @@ -50,21 +47,6 @@ defmodule Bcrypt do

alias Bcrypt.Base

@doc """
Generate a salt for use with the `Bcrypt.Base.hash_password` function.
The `:log_rounds` parameter determines the computational complexity
of the generation of the password hash. Its default is 12, the minimum is 4,
and the maximum is 31.
The `:legacy` option is for generating salts with the old `$2a$` prefix.
Only use this option if you need to generate hashes that are then checked
by older libraries.
"""
def gen_salt(log_rounds \\ 12, legacy \\ false) do
Base.gensalt_nif(:crypto.strong_rand_bytes(16), log_rounds, (legacy and 97) || 98)
end

@doc """
Hashes a password with a randomly generated salt.
Expand Down Expand Up @@ -92,7 +74,7 @@ defmodule Bcrypt do
def hash_pwd_salt(password, opts \\ []) do
Base.hash_password(
password,
gen_salt(
Base.gen_salt(
Keyword.get(opts, :log_rounds, Application.get_env(:bcrypt_elixir, :log_rounds, 12)),
Keyword.get(opts, :legacy, false)
)
Expand Down
15 changes: 15 additions & 0 deletions lib/bcrypt/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ defmodule Bcrypt.Base do
end
end

@doc """
Generate a salt for use with the `hash_password` function.
The `:log_rounds` parameter determines the computational complexity
of the generation of the password hash. Its default is 12, the minimum is 4,
and the maximum is 31.
The `:legacy` option is for generating salts with the old `$2a$` prefix.
Only use this option if you need to generate hashes that are then checked
by older libraries.
"""
def gen_salt(log_rounds \\ 12, legacy \\ false) do
gensalt_nif(:crypto.strong_rand_bytes(16), log_rounds, (legacy and 97) || 98)
end

@doc """
Hash a password using Bcrypt.
"""
Expand Down
11 changes: 6 additions & 5 deletions lib/bcrypt/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ defmodule Bcrypt.Stats do
less than 12.
"""

alias Bcrypt.Base

@doc """
Hash a password with Bcrypt and print out a report.
Expand All @@ -39,16 +41,15 @@ defmodule Bcrypt.Stats do
* `:password` - the password used
* the default is "password"
* `:salt` - the salt used
* the default is the output of Bcrypt.gen_salt
* the default is the output of `Bcrypt.Base.gen_salt`
"""
def report(opts \\ []) do
password = Keyword.get(opts, :password, "password")
log_rounds = Keyword.get(opts, :log_rounds, 12)
salt = Keyword.get(opts, :salt, Bcrypt.gen_salt(log_rounds))
{exec_time, encoded} = :timer.tc(Bcrypt.Base, :hash_password, [password, salt])
salt = Keyword.get(opts, :salt, Base.gen_salt(log_rounds))
{exec_time, encoded} = :timer.tc(Base, :hash_password, [password, salt])

Bcrypt.verify_pass(password, encoded)
|> format_result(encoded, exec_time)
password |> Bcrypt.verify_pass(encoded) |> format_result(encoded, exec_time)
end

defp format_result(check, encoded, exec_time) do
Expand Down
7 changes: 2 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ defmodule BcryptElixir.Mixfile do
use Mix.Project

@source_url "https://github.com/riverrun/bcrypt_elixir"
@version "2.3.1"

@description """
Bcrypt password hashing algorithm for Elixir
"""
@version "3.0.0"
@description "Bcrypt password hashing algorithm for Elixir"

def project do
[
Expand Down
21 changes: 21 additions & 0 deletions test/base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ defmodule Bcrypt.BaseTest do
end
end

test "gen_salt number of rounds" do
assert String.starts_with?(Base.gen_salt(), "$2b$12$")
assert String.starts_with?(Base.gen_salt(8), "$2b$08$")
assert String.starts_with?(Base.gen_salt(20), "$2b$20$")
end

test "gen_salt length of salt" do
assert byte_size(Base.gen_salt(8)) == 29
assert byte_size(Base.gen_salt(20)) == 29
end

test "wrong input to gen_salt" do
assert String.starts_with?(Base.gen_salt(3), "$2b$04$")
assert String.starts_with?(Base.gen_salt(32), "$2b$31$")
end

test "gen_salt with support for $2a$ prefix" do
assert String.starts_with?(Base.gen_salt(8, true), "$2a$08$")
assert String.starts_with?(Base.gen_salt(12, true), "$2a$12$")
end

test "Openwall Bcrypt tests" do
[
{
Expand Down
21 changes: 0 additions & 21 deletions test/bcrypt_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,6 @@ defmodule BcryptTest do
assert String.starts_with?(Bcrypt.hash_pwd_salt("", legacy: false), "$2b$")
end

test "gen_salt number of rounds" do
assert String.starts_with?(Bcrypt.gen_salt(), "$2b$12$")
assert String.starts_with?(Bcrypt.gen_salt(8), "$2b$08$")
assert String.starts_with?(Bcrypt.gen_salt(20), "$2b$20$")
end

test "gen_salt length of salt" do
assert byte_size(Bcrypt.gen_salt(8)) == 29
assert byte_size(Bcrypt.gen_salt(20)) == 29
end

test "wrong input to gen_salt" do
assert String.starts_with?(Bcrypt.gen_salt(3), "$2b$04$")
assert String.starts_with?(Bcrypt.gen_salt(32), "$2b$31$")
end

test "gen_salt with support for $2a$ prefix" do
assert String.starts_with?(Bcrypt.gen_salt(8, true), "$2a$08$")
assert String.starts_with?(Bcrypt.gen_salt(12, true), "$2a$12$")
end

test "add_hash and check_pass" do
assert {:ok, user} = Bcrypt.add_hash("password") |> Bcrypt.check_pass("password")
assert {:error, "invalid password"} = Bcrypt.add_hash("pass") |> Bcrypt.check_pass("password")
Expand Down

0 comments on commit 0c865e5

Please sign in to comment.