gpt4 book ai didi

Python HMAC 哈希值编码为 base64

转载 作者:行者123 更新时间:2023-11-30 22:51:00 24 4
gpt4 key购买 nike

我正在尝试在 django 中间件的帮助下进行 Twitter 身份验证,其中我计算这样的请求的签名( https://dev.twitter.com/oauth/overview/creating-signatures ):

    key = b"MY_KEY&"
raw_init = "POST" + "&" + quote("https://api.twitter.com/1.1/oauth/request_token", safe='')

raw_params = <some_params>
raw_params = quote(raw_params, safe='')

#byte encoding for HMAC, otherwise it returns "expected bytes or bytearray, but got 'str'"
raw_final = bytes(raw_init + "&" + raw_params, encoding='utf-8')

hashed = hmac.new(key, raw_final, sha1)

request.raw_final = hashed

# here are my problems: I need a base64 encoded string, but get the error "'bytes' object has no attribute 'encode'"
request.auth_header = hashed.digest().encode("base64").rstrip('\n')

如您所见,无法对“字节”对象进行 Base64 编码。

建议的解决方案在这里:Implementaion HMAC-SHA1 in python

最佳答案

技巧是直接使用base64模块而不是str/byte编码,它支持二进制。

You can fit it like this (untested in your context, should work):

import base64
#byte encoding for HMAC, otherwise it returns "expected bytes or bytearray, but got 'str'"
raw_final = bytes(raw_init + "&" + raw_params, encoding='utf-8')

hashed = hmac.new(key, raw_final, sha1)

request.raw_final = hashed

# here directly use base64 module, and since it returns bytes, just decode it
request.auth_header = base64.b64encode(hashed.digest()).decode()

For test purposes, find below a standalone, working example (python 3 compatible, Python 2.x users have to remove the "ascii" parameter when creating the bytes string.):

from hashlib import sha1
import hmac
import base64

# key = CONSUMER_SECRET& #If you dont have a token yet
key = bytes("CONSUMER_SECRET&TOKEN_SECRET","ascii")


# The Base String as specified here:
raw = bytes("BASE_STRING","ascii") # as specified by oauth

hashed = hmac.new(key, raw, sha1)

print(base64.b64encode(hashed.digest()).decode())

result:

Rh3xUffks487KzXXTc3n7+Hna6o=

PS:您链接到的答案不再适用于 Python 3。它仅适用于 python 2。

关于Python HMAC 哈希值编码为 base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com