From ceafc8ccb7d3cc9cd95b1aa5f0fb51de65dbd3d8 Mon Sep 17 00:00:00 2001 From: Umgeher Torgersen Date: Sun, 8 Jan 2023 16:52:51 +0000 Subject: [PATCH] new fns: hex_encode and sha256 --- src/utools.erl | 78 +++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/utools.erl b/src/utools.erl index 9a76ee2..9665650 100644 --- a/src/utools.erl +++ b/src/utools.erl @@ -2,66 +2,75 @@ -export([b32_decode/1]). -export([b32_encode/1]). +-export([hex_encode/1]). -export([hotp/2]). -export([rand_bytes/0, rand_bytes/1]). -export([rand_chars/1]). -export([rand_hash/0]). +-export([sha256/1]). -export([totp/1, totp/2]). -export([totp_check/2]). -export([totp_generate/0]). b32_decode({<>, Bits}) -> - <>; + <>; b32_decode({<>, Bits}) -> - <>; + <>; b32_decode({<>, Bits}) -> - <>; + <>; b32_decode({<>) -> + binary:list_to_bin(lists:flatten(io_lib:format("~40.16.0b", [X]))); +hex_encode(<>) -> + binary:list_to_bin(lists:flatten(io_lib:format("~64.16.0b", [X]))). hotp(Token, Time) -> K = b32_decode(Token), @@ -92,7 +101,10 @@ rand_chars(Size) when Size > 0 -> rand_hash() -> {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(<>) -> + hex_encode(crypto:hash(sha256, Data)). totp(<>) -> totp(Secret, erlang:timestamp()).