[英]Python hmac (sha1) calculation
I am trying to calculate the HMAC-SHA1 value in Python, but results don't match with the standard tool I am using as reference (openSSL): 我正在尝试在Python中计算HMAC-SHA1值,但结果与我用作参考的标准工具(openSSL)不匹配:
Python 蟒蛇
k = "ffffffffffffffffffffffffffffffff"
m = "ffffffffffffffffffffffffffffffff"
key = k.decode("hex")
msg = m.decode("hex")
print xlong(hmac.new(key, msg=msg, digestmod=hashlib.sha1).digest())
Result: 801271609151602865551107406598369208989784139177
结果:
801271609151602865551107406598369208989784139177
OpenSSL 的OpenSSL
echo -n ‘ffffffffffffffffffffffffffffffff’ | xxd -r -p | openssl dgst -sha1 -mac HMAC -macopt hexkey:ffffffffffffffffffffffffffffffff
Result: 8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
结果:
8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
Try using binascii.hexlify()
on the HMAC: 尝试在HMAC上使用
binascii.hexlify()
:
>>> from binascii import hexlify
>>> print hexlify(hmac.new(key, msg=msg, digestmod=hashlib.sha1).digest())
8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
Or you may just use str.encode('hex')
: 或者您可以只使用
str.encode('hex')
:
>>> print hmac.new(key, msg=msg, digestmod=hashlib.sha1).digest().encode('hex')
8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.