bash 中的 HMAC-SHA1
是否有 bash 脚本来生成 HMAC-SHA1
哈希值?
我正在寻找与以下 PHP 代码等效的内容:
hash_hmac("sha1", "value", "key");
Is there a bash script to generate a HMAC-SHA1
hash?
I'm looking for something equivalent to the following PHP code:
hash_hmac("sha1", "value", "key");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我意识到这并不完全是您所要求的,但是重新发明轮子并编写 bash 版本是没有意义的。
您只需使用
openssl
命令在脚本中生成哈希值。或者简单地说:
记住将
-n
与echo
一起使用,否则换行符会附加到字符串中,从而更改您的数据和哈希值。该命令来自 OpenSSL 软件包,该软件包应该已经安装(或轻松安装)在您选择的 Linux/Unix、Cygwin 等操作系统中。
请注意,旧版本的
openssl
(例如随 RHEL4 附带的版本)可能不提供-hmac
选项。作为替代解决方案,但主要是为了证明结果是相同的,我们还可以从命令行调用 PHP 的
hmac_sha1()
:I realise this isn't exactly what you're asking for, but there's no point in reinventing the wheel and writing a bash version.
You can simply use the
openssl
command to generate the hash within your script.Or simply:
Remember to use
-n
withecho
or else a line break character is appended to the string and that changes your data and the hash.That command comes from the OpenSSL package which should already be installed (or easily installed) in your choice of Linux/Unix, Cygwin and the likes.
Do note that older versions of
openssl
(such as that shipped with RHEL4) may not provide the-hmac
option.As an alternative solution, but mainly to prove that the results are the same, we can also call PHP's
hmac_sha1()
from the command line:这是一个 bash 函数,其工作方式类似于 PHP 中的
hash_hmac
:Here is a bash function that works like
hash_hmac
from PHP:感谢 hash_hmac 函数!但这对于我的申请来说还不够。如果有人想知道,我必须使用先前哈希结果的密钥对内容进行多次重新哈希,因此是二进制输入。 (Amazon AWS 身份验证签名是这样创建的。)
因此,我需要一种以不会破坏算法的方式提供二进制密钥的方法。然后我发现了这个: http: //openssl.6102.n7.nabble.com/command-line-hmac-with-key-in-hex-td6754.html
斯蒂芬Henson 的回复要求 hash_hmac 函数返回十六进制格式的值。因此它需要回显以下内容:
然后下一个调用需要提供密钥作为十六进制:
希望这可以帮助任何人,可能是那些试图创建 bash 脚本以使 AWS 上的 CloudFront 条目无效的人(就像我一样!)(我还没有还没有测试过,但我认为这就是为什么我的 bash 脚本不起作用,而我的 PHP 脚本却起作用的原因......)
Thanks for the hash_hmac function! But it was not enough for my application. In case anyone wondered, I had to re-hash stuff several times using a key that was the result of the previous hashing, and therefore is a binary input. (The Amazon AWS authentication signature is created like this.)
So what I needed was a way to supply the binary key in some way that would not break the algorithm. Then I found this: http://openssl.6102.n7.nabble.com/command-line-hmac-with-key-in-hex-td6754.html
Stephen Henson's reply requires the hash_hmac function to return the value in hex format. So it needs to echo the following:
Then the next call would need to provide the key as an hexit:
Hopefully this helps anyone, probably someone who is trying to create bash scripts to invalidate CloudFront entries on AWS (like me!) (I haven't tested it yet, but I think this is the thing that is the cause of why my bash script does not work, and my PHP one does...)
安装了node.js后,您可以使用 HMAC-CLI 工具:
返回:
Having node.js installed you can use HMAC-CLI tool:
returns:
对于那些喜欢在命令行上探索更多 JWT 的人:
酷jwt bash脚本
To those who like to explore more JWT on the command line:
cool jwt bash script