Python用HMAC-SHA256编码的消息



我尝试用Python编码HMAC-SHA256消息根据[指令][1]

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(最近一次调用):文件"gen.py",第13行,indigestmod = hashlib。sha256 File "/usr/lib/python2.7/hmac.py",第136行,in new文件"/usr/lib/python2.7/HMAC .py",第71行,在init如果len(key)>blocksize: TypeError: 'long'类型的对象没有len()

有人知道为什么崩溃吗?[1]: https://www.bitstamp.net/api/帐户余额

如果你想在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)

你正在使用数字,而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

最新更新