根据使用说明,我尝试在python中使用HMAC- try 256编码消息。
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()但我明白
回溯(最近一次调用):文件"gen.py",第13行,在digestmod=hashlib.sha256文件"/usr/lib/python2.7/hmac.py",第136行,在新返回的HMAC(key,msg,digestmod) File“/usr/lib/python2.7/hmac.py”中,第71行,在init if (Key)>块大小: TypeError:'long‘类型的对象没有len()
有谁知道为什么会撞车吗?
发布于 2016-06-30 21:45:13
您使用的是api需要字符串/字节的数字。
# python 2
import hmac
import hashlib
nonce = 1234
customer_id = 123232
api_key = 2342342348273482374343434
API_SECRET = 892374928347928347283473
message = '{} {} {}'.format(nonce, customer_id, api_key)
signature = hmac.new(
str(API_SECRET),
msg=message,
digestmod=hashlib.sha256
).hexdigest().upper()
print signature发布于 2017-10-26 13:45:31
如果要在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)https://stackoverflow.com/questions/38133665
复制相似问题