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()
)。
: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()
显然,您仍然需要用您的值替换msg
和hmac_key
。