下面的Perl代码的Python等价物是什么?
hmac_md5_hex($login . "^" . $seq . "^" . $time . "^" . $amo . "^", $CryptoKey);Python hashlib.md5似乎不接受“加密密钥”参数。它只接受1个参数。
发布于 2009-03-30 13:16:14
您必须与md5或sha一起使用hmac module。默认情况下,它使用md5:
In [1]: import hmac, hashlib
In [2]: hmac.new('key', 'msg').hexdigest()
Out[2]: '18e3548c59ad40dd03907b7aeee71d67'
In [3]: hmac.new('key2', 'msg').hexdigest()
Out[3]: 'a4bde113179bc2a7c6ac9ad7309ea073'
In [4]: hmac.new('key', 'msg', hashlib.sha256).hexdigest()
Out[4]: '2d93cbc1be167bcb1637a4a23cbff01a7878f0c50ee833954ea5221bb1b8c628'您的示例可能如下所示:
hmac.new(CryptoKey, '^'.join([login, seq, time, amo]), hashlib.md5).hexdigest()发布于 2009-03-30 13:19:22
看一看this python library documentation about hmac
你可能想要的是:
import hmac
hmac_object = hmac.new(crypto_key)
hmac_object.update('^'.join([login, seq, time, amo, ''])
print hmac_object.hexdigest()最好使用.update(),因为您不必每次都实例化hmac类,如果您想要获得大量十六进制的消息摘要,这将大大提高性能。
发布于 2012-03-01 23:19:54
另一种解决方案,基于PyCrypto
from Crypto.Hash import HMAC
print HMAC.new(CryptoKey, '^'.join([login, seq, time, amo, ''])).hexdigest()https://stackoverflow.com/questions/697134
复制相似问题