2009-03-05 55 views
2

我正在尝试使用sha-512来计算hmac。为什么我在Python中获得错误的hmac结果,但不是Perl?

Perl代码:

use Digest::SHA qw(hmac_sha512_hex); 

$key = "\x0b"x20; 
$data = "Hi There"; 

$hash = hmac_sha512_hex($data, $key); 
print "$hash\n"; 

,并给出了

87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde 
daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854 

Python版本正确的哈希:

import hashlib, hmac 

print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest() 

赋予的

9656975ee5de55e75f2976ecce9a04501060b9dc22a6eda2eaef638966280182 
477fe09f080b2bf564649cad42af8607a2bd8d02979df3a980f15e2326a0a22a 
不正确的哈希

任何想法为什么Python版本给我错误的散列?
版本
的Python 2.5.1(R251:54863,2009年1月13日,10点26分十三秒)
[GCC 4.0.1(苹果公司建立5465)对达尔文

编辑

+0

请提供版本信息 – 2009-03-05 00:33:33

+0

这是一个非常奇怪的版本字符串。你的2.5.1比*(过时的)2.5.2更新*年?这确实是不寻常的。也许尝试更新到2.5.4(这不应该打破任何包)? – kquinn 2009-03-05 00:39:51

回答

9

是真的 - 它似乎的python2.5的豹版本是一个被打破的人。

低于在基于Penryn的-MBP运行...

$ **uname -a** 
Darwin lizard-wifi 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386 
[email protected]:~$ **which python** 
/usr/bin/python 

运行此版本安装在豹操作系统

[email protected]:~$ python 
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import hashlib, hmac 
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest() 
9656975ee5de55e75f2976ecce9a04501060b9dc22a6eda2eaef638966280182477fe09f080b2bf564649cad42af8607a2bd8d02979df3a980f15e2326a0a22a 
>>> 

然后是MacPorts的版本python2.5

$ /opt/local/bin/python2.5 
Python 2.5.4 (r254:67916, Feb 3 2009, 21:40:31) 
[GCC 4.0.1 (Apple Inc. build 5488)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import hashlib, hmac 
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest() 
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854 
>>> 
0

哪个版本的Python?字符串是Python 3中的Unicode。这是Unicode的问题吗?

+0

Python 2.5.1,我相信在OS X 10.5上是默认的。类型(“a”)是str,类型(u“a”)是unicode,如果说什么的话 – cobbal 2009-03-05 00:34:50

1

我无法在此复制您的结果。在使用Python 2.5 IDLE:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. 

... 

IDLE 1.2.2  
>>> import hashlib, hmac 
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest() 
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854 
+0

这里的结果相同,在PHP中也是一样。 – flussence 2009-03-05 00:31:33

0

的python 2.5.2我得到正确的哈希
我想老版是问题

相关问题