From f96b50d00568154aabff7f956b974ad295c38fd6 Mon Sep 17 00:00:00 2001 From: Francois Paul Date: Wed, 17 Mar 2021 00:44:52 +0100 Subject: [PATCH] Update pot.erl to support sha256 and not use deprecated :crypto.hmac (#30) Update pot HMAC computation to support sha256 and be compatible with Erlang/OTP 23+ Resolves https://github.com/yuce/pot/issues/29 --- src/pot.erl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/pot.erl b/src/pot.erl index 980b79c..591300e 100644 --- a/src/pot.erl +++ b/src/pot.erl @@ -90,8 +90,8 @@ hotp(Secret, IntervalsNo, Opts) -> TokenLength = proplists:get_value(token_length, Opts, 6), Key = pot_base32:decode(Secret), Msg = <>, - Digest = crypto:hmac(DigestMethod, Key, Msg), - <<_:19/binary, Ob:8>> = Digest, + Digest = hmac(DigestMethod, Key, Msg), + <> = binary:part(Digest, {byte_size(Digest), -1}), O = Ob band 15, <> = binary:part(Digest, O, 4), TokenBase = TokenBase0 band 16#7fffffff, @@ -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.