相当于丹药中的日期("c")、hash_hmac('sha512', "str1" , "str2" ) 和包( "H*" , $secretkey)



PHP的date("c"),hash_hmac('sha512', "str1", "str2")pack("H*", $secretkey)在Elixir中的等效是什么?

我想把它转换成elixir code

$binKey = pack("H*", $hmackey);
$hmac = strtoupper(hash_hmac('sha512', $msg, $binKey));

我不懂任何PHP,但这里有人已经问过如何格式化日期。如果你想要ISO8601作为输出,它是超级容易的(Date.utc_today() |> Date.to_iso8601())。

据我所知,Elixir在其标准库中不提供HMAC哈希。但是你可以使用Erlang函数::crypto.hash/2例如,:crypto.hash(:sha512, "text")。请参阅Erlang加密文档。注意,我不确定hash_hmac到底是做什么的。如果您需要消息验证码,请查看:crypto.mac/3函数。

对于pack("H*", $secretkey),是将整数转换为十六进制表示的字符串吗?如果是这样的话,那么secret_key |> Integer.to_string(16)就是你想要的。


在你的评论之后,我假设这是你想要复制和粘贴的内容:

hmac_key = "deadbeef"
msg = "your message to sign"
bin_key = hmac_key |> String.to_integer(16)
:crypto.mac(:hmac, :sha512, msg, bin_key) |> Base.encode16()

显然,您仍然需要用您的值替换msghmac_key

最新更新