Current section
Files
Jump to
Current section
Files
src/beecrypt.erl
-module(beecrypt).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([hash/1, verify/2]).
-export_type([bcrype_erlang_error/0]).
-type bcrype_erlang_error() :: any().
-file("src/beecrypt.gleam", 18).
-spec generate_salt() -> binary().
generate_salt() ->
Salt@1 = case bcrypt:gen_salt() of
{ok, Salt} -> Salt;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"beecrypt"/utf8>>,
function => <<"generate_salt"/utf8>>,
line => 19})
end,
unicode:characters_to_binary(Salt@1).
-file("src/beecrypt.gleam", 23).
-spec hash_with_salt(binary(), binary()) -> binary().
hash_with_salt(Password, Salt) ->
Hash@1 = case bcrypt:hashpw(Password, Salt) of
{ok, Hash} -> Hash;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"beecrypt"/utf8>>,
function => <<"hash_with_salt"/utf8>>,
line => 24})
end,
unicode:characters_to_binary(Hash@1).
-file("src/beecrypt.gleam", 5).
-spec hash(binary()) -> binary().
hash(Password) ->
Salt = generate_salt(),
hash_with_salt(Password, Salt).
-file("src/beecrypt.gleam", 10).
-spec verify(binary(), binary()) -> boolean().
verify(Password, Hash) ->
Salt = gleam@string:slice(Hash, 0, 29),
Hashed = hash_with_salt(Password, Salt),
gleam@crypto:secure_compare(<<Hash/binary>>, <<Hashed/binary>>).