gpt4 book ai didi

使用 HMAC-SHA256 的 Python 编码消息

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:12 35 4
gpt4 key购买 nike

我尝试根据 instructions 在 python 中使用 HMAC-SHA256 对消息进行编码

import hmac
import hashlib

nonce = 1234
customer_id = 123232
api_key = 2342342348273482374343434
API_SECRET = 892374928347928347283473

message = nonce + customer_id + api_key
signature = hmac.new(
API_SECRET,
msg=message,
digestmod=hashlib.sha256
).hexdigest().upper()

但是我明白了

Traceback (most recent call last): File "gen.py", line 13, in digestmod=hashlib.sha256 File "/usr/lib/python2.7/hmac.py", line 136, in new return HMAC(key, msg, digestmod) File "/usr/lib/python2.7/hmac.py", line 71, in init if len(key) > blocksize: TypeError: object of type 'long' has no len()

有谁知道为什么会崩溃?

最佳答案

如果你想在 python3 中执行,你应该执行以下操作:

#python 3
import hmac
import hashlib

nonce = 1
customer_id = 123456
API_SECRET = 'thekey'
api_key = 'thapikey'

message = '{} {} {}'.format(nonce, customer_id, api_key)

signature = hmac.new(bytes(API_SECRET , 'latin-1'), msg = bytes(message , 'latin-1'), digestmod = hashlib.sha256).hexdigest().upper()
print(signature)

关于使用 HMAC-SHA256 的 Python 编码消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38133665/

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