Erlang计算HMAC-SHA1的例子?

bar*_*ta7 9 erlang sha1 hmac

在Erlang中计算HMAC-SHA1的任何示例或库?

我试过加密模块,但显然不完全匹配.任何例子?

YOU*_*LID 20

为了扩展前面的答案,这里是使用SHA-1算法的Python中的hmac模块,其密钥为"hello",消息为"world":

>>> import hashlib
>>> import hmac
>>> hmac.HMAC(key='hello', msg='world', digestmod=hashlib.sha1).hexdigest()
'8a3a84bcd0d0065e97f175d370447c7d02e00973'
Run Code Online (Sandbox Code Playgroud)

这是Erlang中的等价物.我会使用一种更有效的方法将二进制MAC转换为典型代码中的十六进制摘要,但为简洁起见,我使用了这个方法:

1> crypto:start().
ok
2> <<Mac:160/integer>> = crypto:hmac(sha, <<"hello">>, <<"world">>).
<<138,58,132,188,208,208,6,94,151,241,117,211,112,68,124,
  125,2,224,9,115>>
3> lists:flatten(io_lib:format("~40.16.0b", [Mac])). 
"8a3a84bcd0d0065e97f175d370447c7d02e00973"
Run Code Online (Sandbox Code Playgroud)