HMAC signing requests in Python

你说的曾经没有我的故事 提交于 2019-12-04 04:22:05

You must pass in a reference to the hashlib.sha512 callable, not the result of calling it:

signing = hmac.new(secret, url, hashlib.sha512)

Alternatively, you could just use the string 'sha512':

signing = hmac.new(secret, url, 'sha512')

and hashlib.new() will be used to construct the hash object.

Demo:

>>> import hashlib
>>> import hmac
>>> secret = b'mysecret'
>>> url = b'http://someurl.com/something/'
>>> signing = hmac.new(secret, url, hashlib.sha512)
>>> signing.digest()
b'!~s2\x97\x97\xa9\xcc\xefcb\xa8\xcc\xa7\xbc\xec\xe5\xfc\xc3\xac\xfc\xbc5]\x05\x96\xc7\x83\x8b\x1b\x90\xd3\xa5\xca\x8cLsC\x17\xa0\xea\xa3\xfe\xd8M\xfda\x1epj\x90\xff}\xfa\xc2@\x92\xfb\xee\xa8\xab\x1b\x08\x8e'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!