问题描述
有一个bash脚本生成一个 HMAC-SHA1
哈希?
我在寻找的东西相当于下面的PHP code:
hash_hmac(SHA1,价值,重点);
我知道这不是你问什么了,但没有点重新发明轮子,写一个bash的版本。
您可以简单地使用命令脚本中生成散列。
[我@ home的]回声-n值| OpenSSL的DGST -sha1 -hmac钥匙
57443a4c052350a44638835d64fd66822f813319
或者干脆:
[我@ home的]回声-n值| OpenSSL的SHA1 -hmac钥匙
57443a4c052350a44638835d64fd66822f813319
记住使用 -n
与回声
或者换行字符被追加到字符串和变化您的数据和散列。
这是命令来自这应该已经在你选择的Linux / Unix,Cygwin和喜欢的安装(或容易地安装)OpenSSL包。
请注意,旧版本的的OpenSSL
的(如附带RHEL4)可能无法提供 -hmac
选项。
作为一种替代解决方案,但主要以证明其结果都是一样的,我们也可以称之为PHP的 HMAC_SHA1()
命令行:
[我@ home的] $回声'< = hash_hmac(SHA1,价值,重点)>? | PHP
57443a4c052350a44638835d64fd66822f813319
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");
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.
[me@home] echo -n "value" | openssl dgst -sha1 -hmac "key"
57443a4c052350a44638835d64fd66822f813319
Or simply:
[me@home] echo -n "value" | openssl sha1 -hmac "key"
57443a4c052350a44638835d64fd66822f813319
Remember to use -n
with echo
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:
[me@home]$ echo '<?= hash_hmac("sha1", "value", "key") ?>' | php
57443a4c052350a44638835d64fd66822f813319
这篇关于HMAC-SHA1在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!