将 HMAC256 编码 - Java 到 Python
我需要将下面的 Java 代码转换为 Python,但在检索所需输出时似乎遇到了问题。我希望看看是否有人可以查明我的错误
Java 代码:
const crypto = require('crypto')
const qs = require('query-string')
const accessKey = '0a16ca22a837af8acae3b70f1952f311'
const accessSecret = 'fe266ab860ef6734192a9e90ad39405b'
const method = 'GET'
const timestamp = 1629339206369
const path = '/api/v2/loan/config'
const signStr = `method=${method}&path=${path}×tamp=${timestamp}`
// "method=GET&path=/api/v2/loan/config×tamp=1629339206369"
const sign = crypto.createHmac('sha256', accessSecret).update(signStr).digest('hex');
Java 输出:
8dafe94777e9130869ceedf9f123c5f6552efcff3cb139591834eb8a82cc7504
Python 代码:
signStr = "method=GET&path=/api/v2/loan/config×tamp=1629339206369"
accessSecret = 'fe266ab860ef6734192a9e90ad39405b'
resp = hmac.new(accessSecret.encode('utf-8'), signStr.encode('utf-8'),
hashlib.sha256).hexdigest()
Python 输出:
f751679a9640cdea16a2cb625a2e797c7190cfd6901e729dcd6ca66519907ac6
谢谢
I need to convert the below Java code into Python but seem to run into issues retrieving the required output. I was hoping to see if anyone could pinpoint my error
Java code:
const crypto = require('crypto')
const qs = require('query-string')
const accessKey = '0a16ca22a837af8acae3b70f1952f311'
const accessSecret = 'fe266ab860ef6734192a9e90ad39405b'
const method = 'GET'
const timestamp = 1629339206369
const path = '/api/v2/loan/config'
const signStr = `method=${method}&path=${path}×tamp=${timestamp}`
// "method=GET&path=/api/v2/loan/config×tamp=1629339206369"
const sign = crypto.createHmac('sha256', accessSecret).update(signStr).digest('hex');
Java output:
8dafe94777e9130869ceedf9f123c5f6552efcff3cb139591834eb8a82cc7504
Python code:
signStr = "method=GET&path=/api/v2/loan/config×tamp=1629339206369"
accessSecret = 'fe266ab860ef6734192a9e90ad39405b'
resp = hmac.new(accessSecret.encode('utf-8'), signStr.encode('utf-8'),
hashlib.sha256).hexdigest()
Python output:
f751679a9640cdea16a2cb625a2e797c7190cfd6901e729dcd6ca66519907ac6
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚在 node.js 上运行了你的代码,并得到了与 Python 完全相同的答案。
I just ran your code on node.js, and got the exact same answer as Python.