哈希错误之前必须对Unicode对象进行编码 [英] Unicode Objects must be encoded before hashing error

查看:113
本文介绍了哈希错误之前必须对Unicode对象进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理类似问题的问题: SO 1 SO 2 SO 3 .

Questions dealing with similar issues: SO 1, SO 2, SO 3.

我尝试了他们的答案,几乎将任何字符串编码为 utf-8 ,但是 hmac 仍然告诉我对我的unicoe字符进行编码.最大的问题是我什至无法识别令人讨厌的变量.打印输出告诉我它们是 strings 还是 bytes ;对于前者,我附加了 .encode(),但这并没有帮助.

I've tried their answers, encoding pretty much any string to utf-8, but hmac still tells me to encode my unicoe chars. The biggest problem is that I can't even identify the offending variable; print outputs tell me they're either strings or bytes; in the case of the former I attach .encode(), but that hasn't helped.

我正在尝试查询GDAX API,同时也在使用代码在其API页面上给出.用Python2.7编写时,我发现编码和所有编码可能存在问题,但这对我来说是没有道理的.

I'm trying to query the GDAX API and am also using the code as given on their API page. Being written for Python2.7, I figured there might be issues with encoding and all, but this is just not making sense to me.

我的代码:

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

    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)
    #print(hmac_key, type(hmac_key))
    #print(message, type(message))
    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

错误:

File "F:\git\knowhere\Private\bitex-crawler\gdax_client\gdaxex\api.py", line 47, in __call__
signature = hmac.new(hmac_key, message, hashlib.sha256)
File "C:\Users\nls\Anaconda3\lib\hmac.py", line 144, in new
return HMAC(key, msg, digestmod)
File "C:\Users\nls\Anaconda3\lib\hmac.py", line 84, in __init__
self.update(msg)
File "C:\Users\nls\Anaconda3\lib\hmac.py", line 93, in update
self.inner.update(msg)
TypeError: Unicode-objects must be encoded before hashing

当我键入检查送入 hmac.new()的对象时,它告诉我我有一个 str 对象和一个 bytes 对象.

When I type check the objects I feed to my hmac.new() call, it tells me I have a str object and a bytes object.

print(type(hmac_key)) # <bytes>
print(type(message))  # <str>

自然地,我认为我也需要对那个吸盘进行编码:

Naturally, I thought I need to encode that sucker too:

signature = hmac.new(hmac_key, message.encode(), hashlib.sha256)

在此行上导致错误:

signature_b64 = signature.digest().encode('base64').rstrip('\n')

即:

File "F:/git/knowhere/Private/bitex-crawler/gdax_client/client.py",
[..]
File "F:\git\knowhere\Private\bitex-crawler\gdax_client\gdaxex\api.py", line 123, in _query
r = api_query(url, json=req, auth=auth)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\api.py", line 67, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\api.py", line 53, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\sessions.py", line 454, in request
prep = self.prepare_request(req)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\sessions.py", line 388, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\models.py", line 297, in prepare
self.prepare_auth(auth, url)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\models.py", line 490, in prepare_auth
r = auth(self)
File "F:\git\knowhere\Private\bitex-crawler\gdax_client\gdaxex\api.py", line 49, in __call__
signature_b64 = signature.digest().encode('base64').rstrip('\n')
AttributeError: 'bytes' object has no attribute 'encode'

..所以我不能有未编码的unicode对象,但是以后也不能有字节吗?我到底该如何解决?感谢所有帮助,因为我深感困惑.

..So I can't have unencoded unicode objects, but I cant have bytes later on either? How on earth do I fix this? Appreciating any help on this, because I'm profoundly confused.

推荐答案

参数msg可以是hashlib支持的任何类型." .

注意:由于哈希工作,因此不支持将字符串对象输入在字节上,而不是字符上." .

因此,您的消息必须为 bytes 类型.在消息中使用 .encode()会为您提供bytes对象.

Thus, your message must be of type bytes. Use .encode() on the message wich will give you the bytes object.

注意:这仅对python 3有用!

Note: This is only necessary for python 3!

要将摘要编码为base64,请使用 base64库.

To encode the digest to base64 use the base64 library.

import base64
signature_b64 = base64.b64encode(signature.digest())

这篇关于哈希错误之前必须对Unicode对象进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆