Current section
Files
Jump to
Current section
Files
src/aragorn2.erl
-module(aragorn2).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([hasher/0, with_time_cost/2, with_memory_cost/2, with_parallelism/2, with_hash_length/2, hash_password/2, verify_password/3]).
-export_type([algorithm/0, hasher/0]).
-type algorithm() :: argon2d | argon2i | argon2id.
-opaque hasher() :: {hasher,
algorithm(),
integer(),
integer(),
integer(),
integer()}.
-spec mib_to_kib(integer()) -> integer().
mib_to_kib(Mib) ->
Mib * 1024.
-spec hasher() -> hasher().
hasher() ->
{hasher, argon2id, 2, mib_to_kib(19), 1, 32}.
-spec with_time_cost(hasher(), integer()) -> hasher().
with_time_cost(Hasher, Time_cost) ->
erlang:setelement(3, Hasher, Time_cost).
-spec with_memory_cost(hasher(), integer()) -> hasher().
with_memory_cost(Hasher, Memory_cost) ->
erlang:setelement(4, Hasher, Memory_cost).
-spec with_parallelism(hasher(), integer()) -> hasher().
with_parallelism(Hasher, Parallelism) ->
erlang:setelement(5, Hasher, Parallelism).
-spec with_hash_length(hasher(), integer()) -> hasher().
with_hash_length(Hasher, Hash_length) ->
erlang:setelement(6, Hasher, Hash_length).
-spec hash_password(hasher(), bitstring()) -> {ok, binary()} | {error, nil}.
hash_password(Hasher, Password) ->
_pipe = aragorn2_ffi:hash_password(Hasher, Password),
gleam@result:nil_error(_pipe).
-spec verify_password(hasher(), bitstring(), bitstring()) -> {ok, nil} |
{error, nil}.
verify_password(Hasher, Candidate_password, Hashed_password) ->
_pipe = aragorn2_ffi:verify_password(
Hasher,
Candidate_password,
Hashed_password
),
_pipe@1 = gleam@result:map(_pipe, fun(_) -> nil end),
gleam@result:nil_error(_pipe@1).