我能否在Perl或PHP中生成SHA1哈希值?

4
Verotel要求使用sha1_hex函数对一些数据进行哈希处理。到底是什么?整个互联网上都没有关于它的信息。他们说“使用SHA-1哈希(十六进制输出)”。Sha1输出十六进制?以下是一个我无法重现的示例:
sha1_hex("abc777X:description=some description of product:priceAmount=51.20:priceCurrency=EUR:shopID=60678:version=1")
= 04d87d2718767ea0bef259c436ec63e3cde05be2
5个回答

9
echo sha1('abc777X:description=some description of product:priceAmount=51.20:priceCurrency=EUR:shopID=60678:version=1');

实际上,在php中,sha1_hex被命名为sha1()。下面是一个例子,可以处理您的输入:http://codepad.org/9fLlr9VJ


7
$ perl -e 'use Digest::SHA qw(sha1_hex); print sha1_hex("abc777X:description=some description of product:priceAmount=51.20:priceCurrency=EUR:shopID=60678:version=1")'
04d87d2718767ea0bef259c436ec63e3cde05be2

SHA-1哈希生成20字节的输出。如果你用十六进制表示这20个字节,你会得到40个字符。


7

对于Perl语言,可以使用Digest::SHA模块(已更新自Digest::SHA1)。

perl -MDigest::SHA=sha1_hex -le'print sha1_hex("abc777X:description=some description of product:priceAmount=51.20:priceCurrency=EUR:shopID=60678:version=1")'
04d87d2718767ea0bef259c436ec63e3cde05be2

6
Digest::SHA1已经过时。Perl 5.10中已将Digest::SHA加入核心,请使用它来替代。 - cjm
@cjm,我会更新以反映首选核心版本,但我想知道弃用通知在哪里。 - Ashley
1
我认为它并没有正式弃用,但是现在 Digest::SHA 是一个核心模块,而且可以完成 Digest::SHA1 的所有功能(甚至更多),所以最好使用 Digest::SHA。 - cjm
1
SHA1算法本身已经被弃用,而一开始使用Digest::SHA或许可以更轻松地升级到更复杂的SHA算法。 - ysth

0
在PHP中,函数是{{link1:sha1}},而不是sha1_hex

0
$ php -r 'var_dump(sha1("abc777X:description=some description of product:priceAmount=51.20:priceCurrency=EUR:shopID=60678:version=1"));'
string(40) "04d87d2718767ea0bef259c436ec63e3cde05be2"
$

对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接