Python hmac (sha1) 计算
Python hmac (sha1) calculation
我正在尝试计算 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())
结果:801271609151602865551107406598369208989784139177
OpenSSL
echo -n ‘ffffffffffffffffffffffffffffffff’ | xxd -r -p | openssl dgst -sha1 -mac HMAC -macopt hexkey:ffffffffffffffffffffffffffffffff
结果:8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
尝试在 HMAC 上使用 binascii.hexlify()
:
>>> from binascii import hexlify
>>> print hexlify(hmac.new(key, msg=msg, digestmod=hashlib.sha1).digest())
8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
或者您可以只使用 str.encode('hex')
:
>>> print hmac.new(key, msg=msg, digestmod=hashlib.sha1).digest().encode('hex')
8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
我正在尝试计算 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())
结果:801271609151602865551107406598369208989784139177
OpenSSL
echo -n ‘ffffffffffffffffffffffffffffffff’ | xxd -r -p | openssl dgst -sha1 -mac HMAC -macopt hexkey:ffffffffffffffffffffffffffffffff
结果:8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
尝试在 HMAC 上使用 binascii.hexlify()
:
>>> from binascii import hexlify
>>> print hexlify(hmac.new(key, msg=msg, digestmod=hashlib.sha1).digest())
8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9
或者您可以只使用 str.encode('hex')
:
>>> print hmac.new(key, msg=msg, digestmod=hashlib.sha1).digest().encode('hex')
8c5a42f91479bfbaed8dd538db8c4a76b44ee5a9