简体   繁体   English

在 python 中创建 HMAC SHA256 密钥

[英]creating a HMAC SHA256 key in python

I'm trying to properly create a new HMAC key to interact with an API.我正在尝试正确创建一个新的 HMAC 密钥以与 API 交互。 However I keep on hitting obstacle after obstacle.但是,我不断地遇到障碍。 I've copied the code from the API documents to create the authentication message but it keeps throwing errors.我从 API 文档中复制了代码来创建身份验证消息,但它不断抛出错误。

class CoinbaseExchangeAuth(AuthBase):
    def __init__(self, api_key, secret_key, passphrase):
        self.api_key = api_key
        self.secret_key = secret_key
        self.passphrase = passphrase

    def __call__(self, request):
        timestamp = str(time.time())
        message = timestamp + request.method + request.path_url + (request.body or '')
        hmac_key = base64.b64decode(self.secret_key)
        signature = hmac.new(hmac_key, message, hashlib.sha256)
        signature_b64 = signature.digest().encode('base64').rstrip('\n')

        request.headers.update({
            'CB-ACCESS-SIGN': signature_b64,
            'CB-ACCESS-TIMESTAMP': timestamp,
            'CB-ACCESS-KEY': self.api_key,
            'CB-ACCESS-PASSPHRASE': self.passphrase,
            'Content-Type': 'application/json'
        })
        return request

api_url = 'https://api.pro.coinbase.com/'
auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)

# Get accounts
r = requests.get(api_url + 'accounts', auth=auth)

This is the code directly from the webpage, when i run it with the correct values(API_KEY, API_SECRET, API_PASS) it moans saying that the key needs encoding before hashing.这是直接来自网页的代码,当我使用正确的值(API_KEY、API_SECRET、API_PASS)运行它时,它抱怨说密钥需要在散列之前进行编码。

the key itself looks something like this 36jGMCGkfpyjsnrywCz/sYL6K4nd6Iy41CTe8ovhVWh2EjforFSAkhrH7nyovzqgLWzBcvIIUjyCy3Eku+weSg==密钥本身看起来像这样 36jGMCGkfpyjsnrywCz/sYL6K4nd6Iy41CTe8ovhVWh2EjforFSAkhrH7nyovzqgLWzBcvIIUjyCy3Eku+weSg==

which i believe is base64 encoded, so when i change the variable of hmac_key to = b'36jGMCGkfpyjsnrywCz/sYL6K4nd6Iy41CTe8ovhVWh2EjforFSAkhrH7nyovzqgLWzBcvIIUjyCy3Eku+weSg=='我相信是 base64 编码的,所以当我将 hmac_key 的变量更改为 = b'36jGMCGkfpyjsnrywCz/sYL6K4nd6Iy41CTe8ovhVWh2EjforFSAkhrH7nyovzqgLWzBcvIIUjyCy3Eku+weSg=='

it still tells me that the code needs encoding first through this error message它仍然告诉我代码需要首先通过此错误消息进行编码

    Traceback (most recent call last):
  File "C:\Users\Chris\Documents\New folder\import csv.py", line 35, in <module>
    signature = hmac.new(encoded_key, message, digestmod=hashlib.sha256)
  File "C:\Users\Chris\AppData\Local\Programs\Python\Python39\lib\hmac.py", line 170, in new
    return HMAC(key, msg, digestmod)
  File "C:\Users\Chris\AppData\Local\Programs\Python\Python39\lib\hmac.py", line 93, in __init__
    self.update(msg)
  File "C:\Users\Chris\AppData\Local\Programs\Python\Python39\lib\hmac.py", line 113, in update
    self._inner.update(msg)
TypeError: Unicode-objects must be encoded before hashing

however the API documents says that i need to decode the secret key/hmac_key first which is what their code is doing.但是 API 文档说我需要首先解码密钥/hmac_key,这就是他们的代码正在做的事情。 can someone please shed some light on this before i frisby the laptop out the window在我将笔记本电脑从 window 中取出之前,有人可以解释一下吗

Most of encryption algorithms (I believe, HMAC included) expects data in binary.大多数加密算法(我相信,包括 HMAC)都需要二进制数据。 So you should .encode() key before calling hmac.new() .所以你应该在调用hmac.new()之前使用.encode()键。

Wild guess – this docs on website is from py2 times and weren't updated疯狂的猜测——网站上的这个文档来自 py2 时代并且没有更新

i have encoded both the message and the key with the.encode() method and it has worked.我已经使用 .encode() 方法对消息和密钥进行了编码,并且它已经工作了。 I now have a hmac object saved to memory我现在有一个 hmac object 保存到 memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM