python相当于perl hmac_sha1_hex

sil*_*iud 2 python perl sha1 hmac

我需要在python中重现perl的功能

  # 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
Run Code Online (Sandbox Code Playgroud)

正如你所看到的十六进制摘要不一样......我怎样才能在python中重现perl代码?

谢谢 !

hob*_*bbs 9

Python的HMAC构造函数只是以相反的顺序获取密钥和消息--Python hmac首先获取密钥,Perl Digest::HMAC获取密钥第二.

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

匹配你的Perl示例就好:)