为什么我在Python中获得错误的hmac结果,但不是Perl? [英] Why do I get wrong results for hmac in Python but not Perl?

查看:105
本文介绍了为什么我在Python中获得错误的hmac结果,但不是Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用sha-512来计算hmac。

I'm trying to compute hmac using sha-512.

Perl代码:

The Perl code:

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版本给我错误的散列?

any ideas why the Python version is giving me the wrong hash?

编辑:

版本是

Python 2.5.1(r251:54863,2009年1月13日,10:26:13)

[GCC 4.0.1(Apple Inc. build 5465)]达尔文


version is
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin

推荐答案

确实如此 - 看来Leopard versi python2.5是一个破碎的。

yes indeed -- it seems the Leopard version of python2.5 is the one that is broken.

以下是基于Penryn的MBP ...

below run on a Penryn-based 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中的这个版本

Running this version installed in 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
>>> 

然后MacPorts版本的 python2.5

And then the MacPorts version of 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
>>> 

这篇关于为什么我在Python中获得错误的hmac结果,但不是Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆