totp_check/2
This commit is contained in:
parent
1db1bff950
commit
831c37a26b
2 changed files with 14 additions and 8 deletions
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
||||||
PROJECT = utools
|
PROJECT = utools
|
||||||
PROJECT_DESCRIPTION = Umgeher Erlang Tools
|
PROJECT_DESCRIPTION = Umgeher Erlang Tools
|
||||||
PROJECT_VERSION = 0.1.1
|
PROJECT_VERSION = 0.2.0
|
||||||
|
|
||||||
# Whitespace to be used when creating files from templates.
|
# Whitespace to be used when creating files from templates.
|
||||||
SP = 2
|
SP = 2
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
-export([rand_chars/1]).
|
-export([rand_chars/1]).
|
||||||
-export([rand_hash/0]).
|
-export([rand_hash/0]).
|
||||||
-export([totp/1, totp/2]).
|
-export([totp/1, totp/2]).
|
||||||
|
-export([totp_check/2]).
|
||||||
-export([totp_generate/0]).
|
-export([totp_generate/0]).
|
||||||
|
|
||||||
b32_decode({<<V, "======">>, Bits}) ->
|
b32_decode({<<V, "======">>, Bits}) ->
|
||||||
|
@ -93,13 +94,18 @@ rand_hash() ->
|
||||||
{ok, R} = rand_chars(64),
|
{ok, R} = rand_chars(64),
|
||||||
{ok, binary:encode_hex(crypto:hash(sha256, <<"utools:rand_hash(", R/binary, ")">>))}.
|
{ok, binary:encode_hex(crypto:hash(sha256, <<"utools:rand_hash(", R/binary, ")">>))}.
|
||||||
|
|
||||||
totp_generate() ->
|
totp(<<Secret/binary>>) ->
|
||||||
rand_chars(16).
|
totp(Secret, erlang:timestamp()).
|
||||||
|
|
||||||
totp(<<Token/binary>>) ->
|
totp(<<Secret/binary>>, {M, S, _}) ->
|
||||||
totp(Token, erlang:timestamp()).
|
|
||||||
|
|
||||||
totp(<<Token/binary>>, {M, S, _}) ->
|
|
||||||
T = (M * 1000000 + S) / 30,
|
T = (M * 1000000 + S) / 30,
|
||||||
Time = trunc(T),
|
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).
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue