Add choice of ops and mem limits to pwhash_str

It natively checks atoms, which is kinda messy, but it avoids having to
export the libsodium pwhash constants, which is nice.
This commit is contained in:
ECrownofFire
2018-10-27 17:23:06 -04:00
parent f650c72b02
commit d779071285
3 changed files with 83 additions and 8 deletions
+19 -1
View File
@@ -101,6 +101,9 @@
shorthash_size/0,
shorthash/2,
%% No Tests!
pwhash_str/3,
%% EQC
pwhash/2,
pwhash_str/1,
@@ -336,6 +339,7 @@ generichash_final({hashstate, HashSize, HashState}) ->
enacl_nif:crypto_generichash_final(HashSize, HashState).
-type pwhash_limit() :: interactive | moderate | sensitive | pos_integer().
%% @doc pwhash/2 hash a password
%%
%% This function generates a fixed size salted hash of a user defined password.
@@ -347,10 +351,24 @@ pwhash(Password, Salt) ->
%% @doc pwhash_str/1 generates a ASCII encoded hash of a password
%%
%% This function generates a fixed size, salted, ASCII encoded hash of a user defined password.
%% Defaults to interactive/interactive limits.
%% @end
-spec pwhash_str(iodata()) -> {ok, iodata()} | {error, term()}.
pwhash_str(Password) ->
case enacl_nif:crypto_pwhash_str(Password) of
pwhash_str(Password, interactive, interactive).
%% @doc pwhash_str/3 generates a ASCII encoded hash of a password
%%
%% This function generates a fixed size, salted, ASCII encoded hash of a user defined password
%% given Ops and Mem limits.
%% @end
-spec pwhash_str(Password, Ops, Mem) -> {ok, iodata()} | {error, term()}
when
Password :: iodata(),
Ops :: pwhash_limit(),
Mem :: pwhash_limit().
pwhash_str(Password, Ops, Mem) ->
case enacl_nif:crypto_pwhash_str(Password, Ops, Mem) of
{ok, ASCII} ->
{ok, strip_null_terminate(ASCII)};
{error, Reason} ->
+2 -2
View File
@@ -133,7 +133,7 @@
%% Password Hashing - Argon2 Algorithm
-export([
crypto_pwhash/2,
crypto_pwhash_str/1,
crypto_pwhash_str/3,
crypto_pwhash_str_verify/2
]).
@@ -189,7 +189,7 @@ crypto_generichash_update(_HashSize, _HashState, _Message) -> erlang:nif_error(
crypto_generichash_final(_HashSize, _HashState) -> erlang:nif_error(nif_not_loaded).
crypto_pwhash(_Password, _Salt) -> erlang:nif_error(nif_not_loaded).
crypto_pwhash_str(_Password) -> erlang:nif_error(nif_not_loaded).
crypto_pwhash_str(_Password, _Ops, _Mem) -> erlang:nif_error(nif_not_loaded).
crypto_pwhash_str_verify(_HashedPassword, _Password) -> erlang:nif_error(nif_not_loaded).
crypto_box_NONCEBYTES() -> erlang:nif_error(nif_not_loaded).