【问题标题】:Why do I get wrong results for hmac in Python but not Perl?为什么我在 Python 中得到错误的结果 hmac 而不是 Perl?
【发布时间】:2010-10-11 10:01:14
【问题描述】:

我正在尝试使用 sha-512 计算 hmac。

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:13)
[GCC 4.0.1 (Apple Inc. build 5465)] 在 darwin 上

【问题讨论】:

  • 请提供版本信息
  • 那是……一个非常奇怪的版本字符串。您的 2.5.1 比我的(过时的)2.5.2 更新 ?这确实不寻常。也许尝试更新到 2.5.4(不应该破坏任何包)?

标签: python perl hash hmac


【解决方案1】:

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

【讨论】:

  • Python 2.5.1,我相信在 OS X 10.5 上默认。 type("a") is str, type(u"a") is unicode if that say anything
【解决方案2】:

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

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

【讨论】:

  • 此处的结果相同,PHP 中的结果也相同。
【解决方案3】:

在 python 2.5.2 下我得到正确的哈希
我猜是旧版本的问题

【讨论】:

  • 请更新您的原始问题,而不是添加其他答案。 :)
【解决方案4】:

确实是的——似乎豹版的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
dpc@lizard-wifi:~$ **which python**
/usr/bin/python

运行安装在 Leopard OS 中的这个版本

dpc@lizard-wifi:~$ 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
>>> 

然后是python2.5的MacPorts版本

$ /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
>>> 

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2017-04-06
    • 2023-03-03
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2018-01-21
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多