将 HMAC256 编码 - Java 到 Python

发布于 2025-01-12 07:04:07 字数 1127 浏览 0 评论 0原文

我需要将下面的 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}&timestamp=${timestamp}`
// "method=GET&path=/api/v2/loan/config&timestamp=1629339206369"

const sign = crypto.createHmac('sha256', accessSecret).update(signStr).digest('hex');

Java 输出:

8dafe94777e9130869ceedf9f123c5f6552efcff3cb139591834eb8a82cc7504

Python 代码:

signStr =  "method=GET&path=/api/v2/loan/config&timestamp=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2025-01-19 07:04:07

我刚刚在 node.js 上运行了你的代码,并得到了与 Python 完全相同的答案。

> const method = 'GET' 
> const timestamp = 1629339206369
> const path = '/api/v2/loan/config'     
> const signStr = `method=${method}&path=${path}×tamp=${timestamp}`
> signStr
'method=GET&path=/api/v2/loan/config×tamp=1629339206369'
> const sign = crypto.createHmac('sha256', accessSecret).update(signStr).digest('hex');
> sign
'f751679a9640cdea16a2cb625a2e797c7190cfd6901e729dcd6ca66519907ac6'

I just ran your code on node.js, and got the exact same answer as Python.

> const method = 'GET' 
> const timestamp = 1629339206369
> const path = '/api/v2/loan/config'     
> const signStr = `method=${method}&path=${path}×tamp=${timestamp}`
> signStr
'method=GET&path=/api/v2/loan/config×tamp=1629339206369'
> const sign = crypto.createHmac('sha256', accessSecret).update(signStr).digest('hex');
> sign
'f751679a9640cdea16a2cb625a2e797c7190cfd6901e729dcd6ca66519907ac6'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文