在python中使用sha512对消息进行签名
我正试图通过hitbtc.com上的websocket发送消息。根据api文档,“signagure”需要是“基于哈希的消息身份验证代码:base64 hmac-sha512(消息的二进制表示)” 我收到错误消息:“str”对象没有属性“hexdigest”在python中使用sha512对消息进行签名,python,hmac,sha512,Python,Hmac,Sha512,我正试图通过hitbtc.com上的websocket发送消息。根据api文档,“signagure”需要是“基于哈希的消息身份验证代码:base64 hmac-sha512(消息的二进制表示)” 我收到错误消息:“str”对象没有属性“hexdigest” import time import json import hmac import hashlib def sign(payload): key = ""
import time
import json
import hmac
import hashlib
def sign(payload):
key = ""
secret = ""
"""
Signature method which wraps signature and nonce parameters around a
payload dictionary.
:param payload:
:return:
"""
nonce = str(int(time.time() * 1000))
package = {'apikey': key,
'message': {'nonce': nonce, 'payload': payload}}
signature = hmac.new(secret, json.dumps(payload).hexdigest,
hashlib.sha512).hexdigest()
package['signature'] = signature
return json.dumps(package)
payload = {"Login": {}}
s = sign(payload)
可能是来自json.dumps(payload.hexdigest),因为dumps返回一个字符串。这肯定是它的来源。如果我将其更改为:signature=hmac.new(secret,json.dumps(payload),hashlib.sha512).hexdigest(),那么我会得到另一个错误:key:expected bytes或bytearray,但得到了'str',我只是将代码复制到一个新的.py文件中,并按照您所描述的方式更改了行,并且它似乎没有错误()