Skip to content

Commit

Permalink
Update pot.erl to support sha256 and not use deprecated :crypto.hmac (#…
Browse files Browse the repository at this point in the history
…30)

Update pot HMAC computation to support sha256 and be compatible with Erlang/OTP 23+
Resolves #29
  • Loading branch information
franc authored Mar 16, 2021
1 parent 82ac907 commit f96b50d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/pot.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ hotp(Secret, IntervalsNo, Opts) ->
TokenLength = proplists:get_value(token_length, Opts, 6),
Key = pot_base32:decode(Secret),
Msg = <<IntervalsNo:8/big-unsigned-integer-unit:8>>,
Digest = crypto:hmac(DigestMethod, Key, Msg),
<<_:19/binary, Ob:8>> = Digest,
Digest = hmac(DigestMethod, Key, Msg),
<<Ob:8>> = binary:part(Digest, {byte_size(Digest), -1}),
O = Ob band 15,
<<TokenBase0:4/integer-unit:8>> = binary:part(Digest, O, 4),
TokenBase = TokenBase0 band 16#7fffffff,
Expand Down Expand Up @@ -205,3 +205,16 @@ valid_hotp_return(LastInterval, true = _ReturnInterval) ->
{true, LastInterval};
valid_hotp_return(_LastInterval, _ReturnInterval) ->
true.

-ifdef(OTP_RELEASE).
-if(?OTP_RELEASE >= 23).
hmac(DigestMethod, Key, Msg) ->
crypto:mac(hmac, DigestMethod, Key, Msg).
-else.
hmac(DigestMethod, K, S) ->
crypto:hmac(DigestMethod, K, S).
-endif.
-else.
hmac(DigestMethod, K, S) ->
crypto:hmac(DigestMethod, K, S).
-endif.

0 comments on commit f96b50d

Please sign in to comment.