2011-08-30 89 views
2

我需要在Python重現什麼perl做蟒蛇相同功能的perl的hmac_sha1_hex

# perl 
    perl -e'use Digest::HMAC_SHA1 qw(hmac_sha1_hex); my $hmac = hmac_sha1_hex("string1", "string2"); print $hmac . "\n";' 
    25afd2da17e81972b535d15ebae464e291fb3635 


    #python 
    python -c 'import sha; import hmac; print hmac.new("string1", "string2", sha).hexdigest()' 
    3953fa89b3809b8963b514999b2d27a7cdaacc77 

正如你所看到的十六進制摘要是不一樣的...我怎樣才能重現蟒蛇Perl代碼?

謝謝!

+1

'perl的-MDigest :: HMAC_SHA1 = hmac_sha1_hex -E '說hmac_sha1_hex( 「STRING1」, 「字符串2」)'' 看起來好得多在Perl世界 – yko

回答

9

Python的HMAC構造函數只是按照相反的順序輸入密鑰和消息 - Python的hmac首先取得了關鍵,Perl的Digest::HMAC取得了第二個關鍵。

python -c 'import sha; import hmac; print hmac.new("string2", "string1", sha).hexdigest()' 
25afd2da17e81972b535d15ebae464e291fb3635 

匹配你的Perl例子就好:)