开发者

python equivalent of perl hmac_sha1_hex

i need to reproduce in python what perl does

  # 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).he开发者_如何学运维xdigest()'
  3953fa89b3809b8963b514999b2d27a7cdaacc77

as you can see the hex digest is not the same ... how can I reproduce the perl code in python ?

thanks !


Python's HMAC constructor just takes the key and the message in the opposite order -- Python's hmac takes the key first, Perl's Digest::HMAC takes the key second.

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

Matches your Perl example just fine :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜