2

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)] です。

4

4 に答える 4

9

はい、確かに-python2.5のLeopardバージョンが壊れているようです。

以下はペンリンベースの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

LeopardOSにインストールされているこのバージョンを実行する

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

$ /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
>>> 
于 2009-03-05T01:23:29.687 に答える
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
于 2009-03-05T00:30:30.810 に答える
0

どのバージョンの Python? 文字列は Python 3 の Unicode です。これは Unicode の問題ですか?

于 2009-03-05T00:27:08.417 に答える
0

Python 2.5.2 では、正しいハッシュが得られまし
た。古いバージョンが問題だったと思います。

于 2009-03-05T00:50:23.807 に答える