如何在 Python 中进行 HMAC-SHA512 签名?

jz2*_*z22 4 python api sign sha python-3.x

我想以以下格式执行获取请求:

https://bittrex.com/api/v1.1/account/getbalance?apikey=API_KEY&currency=BTC
Run Code Online (Sandbox Code Playgroud)

我确实有公开密钥和秘密密钥。但是我发现了以下声明:

对于此版本,我们使用标准的 HMAC-SHA512 签名。将 apikey 和 nonce 附加到您的请求并计算 HMAC 哈希并将其包含在 apisign 标头下

我真的不知道如何正确加密我的密钥。使用普通密钥显然会返回“NONCE_NOT_PROVIDED”。我得到的一切是这样的:

current_price = requests.get("https://bittrex.com/api/v1.1/account/getbalance?apikey=API_KEY&currency=BTC")
Run Code Online (Sandbox Code Playgroud)

如何正确签署和加密密钥?谢谢你。

编辑:

当前尝试如下所示。

def getWalletSize():
    APIkey = b'29i52wp4'
    secret = b'10k84a9e'
    s = "https://bittrex.com/api/v1.1/account/getbalance?apikey=29i52wp4&currency=BTC"
    digest = hmac.new(secret, msg=s, digestmod=hashlib.sha512).digest()
    current_balance = requests.get(digest)
    return current_balance
Run Code Online (Sandbox Code Playgroud)

但是它引发了错误 Unicode-objects must be encoded before hashing

Uma*_*har 5

import hmac
import hashlib
import base64

API_KEY = 'public_key'
s = """GET https://bittrex.com/api/v1.1/account/getbalance?apikey=%s&currency=BTC""" % API_KEY

base64.b64encode(hmac.new("1234567890", msg=s, digestmod=hashlib.sha512).digest())
Run Code Online (Sandbox Code Playgroud)

它签署请求

digest = hmac.new(secret_key, msg=thing_to_hash, digestmod=hashlib.sha512).digest()
Run Code Online (Sandbox Code Playgroud)

并将其编码为 base64

base64.b64encode(digest)
Run Code Online (Sandbox Code Playgroud)