-module(utools). -export([b32_decode/1]). -export([b32_encode/1]). -export([hotp/2]). -export([rand_bytes/0, rand_bytes/1]). -export([rand_chars/1]). -export([rand_hash/0]). -export([totp/1, totp/2]). -export([totp_check/2]). -export([totp_generate/0]). b32_decode({<>, Bits}) -> <>; b32_decode({<>, Bits}) -> <>; b32_decode({<>, Bits}) -> <>; b32_decode({<>}). rand_hash() -> {ok, R} = rand_chars(64), {ok, binary:encode_hex(crypto:hash(sha256, <<"utools:rand_hash(", R/binary, ")">>))}. totp(<>) -> totp(Secret, erlang:timestamp()). totp(<>, {M, S, _}) -> T = (M * 1000000 + S) / 30, Time = trunc(T), 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).