new fns: hex_encode and sha256
This commit is contained in:
parent
831c37a26b
commit
ceafc8ccb7
1 changed files with 45 additions and 33 deletions
|
@ -2,10 +2,12 @@
|
||||||
|
|
||||||
-export([b32_decode/1]).
|
-export([b32_decode/1]).
|
||||||
-export([b32_encode/1]).
|
-export([b32_encode/1]).
|
||||||
|
-export([hex_encode/1]).
|
||||||
-export([hotp/2]).
|
-export([hotp/2]).
|
||||||
-export([rand_bytes/0, rand_bytes/1]).
|
-export([rand_bytes/0, rand_bytes/1]).
|
||||||
-export([rand_chars/1]).
|
-export([rand_chars/1]).
|
||||||
-export([rand_hash/0]).
|
-export([rand_hash/0]).
|
||||||
|
-export([sha256/1]).
|
||||||
-export([totp/1, totp/2]).
|
-export([totp/1, totp/2]).
|
||||||
-export([totp_check/2]).
|
-export([totp_check/2]).
|
||||||
-export([totp_generate/0]).
|
-export([totp_generate/0]).
|
||||||
|
@ -63,6 +65,13 @@ b32_std_enc(I) when is_integer(I) andalso I >= 26 andalso I =< 31 ->
|
||||||
b32_std_enc(I) when is_integer(I) andalso I >= 0 andalso I =< 25 ->
|
b32_std_enc(I) when is_integer(I) andalso I >= 0 andalso I =< 25 ->
|
||||||
I + $a.
|
I + $a.
|
||||||
|
|
||||||
|
hex_encode(<<X:128/big-unsigned-integer>>) ->
|
||||||
|
binary:list_to_bin(lists:flatten(io_lib:format("~32.16.0b", [X])));
|
||||||
|
hex_encode(<<X:160/big-unsigned-integer>>) ->
|
||||||
|
binary:list_to_bin(lists:flatten(io_lib:format("~40.16.0b", [X])));
|
||||||
|
hex_encode(<<X:256/big-unsigned-integer>>) ->
|
||||||
|
binary:list_to_bin(lists:flatten(io_lib:format("~64.16.0b", [X]))).
|
||||||
|
|
||||||
hotp(Token, Time) ->
|
hotp(Token, Time) ->
|
||||||
K = b32_decode(Token),
|
K = b32_decode(Token),
|
||||||
M = <<Time:8/big-unsigned-integer-unit:8>>,
|
M = <<Time:8/big-unsigned-integer-unit:8>>,
|
||||||
|
@ -92,7 +101,10 @@ rand_chars(Size) when Size > 0 ->
|
||||||
|
|
||||||
rand_hash() ->
|
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, sha256(<<"utools:rand_hash(", R/binary, ")">>)}.
|
||||||
|
|
||||||
|
sha256(<<Data/binary>>) ->
|
||||||
|
hex_encode(crypto:hash(sha256, Data)).
|
||||||
|
|
||||||
totp(<<Secret/binary>>) ->
|
totp(<<Secret/binary>>) ->
|
||||||
totp(Secret, erlang:timestamp()).
|
totp(Secret, erlang:timestamp()).
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue