totp_check/2

This commit is contained in:
Umgeher Torgersen 2022-12-29 15:07:54 +00:00
parent 1db1bff950
commit 831c37a26b
2 changed files with 14 additions and 8 deletions

View file

@ -7,6 +7,7 @@
-export([rand_chars/1]).
-export([rand_hash/0]).
-export([totp/1, totp/2]).
-export([totp_check/2]).
-export([totp_generate/0]).
b32_decode({<<V, "======">>, Bits}) ->
@ -93,13 +94,18 @@ rand_hash() ->
{ok, R} = rand_chars(64),
{ok, binary:encode_hex(crypto:hash(sha256, <<"utools:rand_hash(", R/binary, ")">>))}.
totp_generate() ->
rand_chars(16).
totp(<<Secret/binary>>) ->
totp(Secret, erlang:timestamp()).
totp(<<Token/binary>>) ->
totp(Token, erlang:timestamp()).
totp(<<Token/binary>>, {M, S, _}) ->
totp(<<Secret/binary>>, {M, S, _}) ->
T = (M * 1000000 + S) / 30,
Time = trunc(T),
hotp(Token, Time).
hotp(Secret, Time).
totp_check(<<Token/binary>>, <<Secret/binary>>) ->
{M, S, _} = erlang:timestamp(),
TL = [totp(Secret, {M, S - 30, 0}), totp(Secret, {M, S, 0}), totp(Secret, {M, S + 30, 0})],
{ok, lists:member({ok, Token}, TL)}.
totp_generate() ->
rand_chars(16).