首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用HMAC-SHA256 256编码的Python消息

用HMAC-SHA256 256编码的Python消息
EN

Stack Overflow用户
提问于 2016-06-30 21:34:33
回答 2查看 56.8K关注 0票数 35

根据使用说明,我尝试在python中使用HMAC- try 256编码消息。

代码语言:javascript
复制
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()

有谁知道为什么会撞车吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-30 21:45:13

您使用的是api需要字符串/字节的数字。

代码语言:javascript
复制
# 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
票数 30
EN

Stack Overflow用户

发布于 2017-10-26 13:45:31

如果要在python3中执行,应执行以下操作:

代码语言:javascript
复制
#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)
票数 48
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38133665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档