diff --git a/Makefile b/Makefile index 246e0cb..42eb486 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PROJECT = utools PROJECT_DESCRIPTION = Umgeher Erlang Tools -PROJECT_VERSION = 0.1.1 +PROJECT_VERSION = 0.2.0 # Whitespace to be used when creating files from templates. SP = 2 diff --git a/src/utools.erl b/src/utools.erl index 822cf7c..9a76ee2 100644 --- a/src/utools.erl +++ b/src/utools.erl @@ -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({<>, 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(<>) -> + totp(Secret, erlang:timestamp()). -totp(<>) -> - totp(Token, erlang:timestamp()). - -totp(<>, {M, S, _}) -> +totp(<>, {M, S, _}) -> T = (M * 1000000 + S) / 30, Time = trunc(T), - hotp(Token, Time). + hotp(Secret, Time). + +totp_check(<>, <>) -> + {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).