【发布时间】:2021-05-13 01:39:16
【问题描述】:
首先,抱歉我的英语不好,感谢您点击这个问题。
我正在尝试将我的 python 代码更改为 Node.js 代码。 (关于base64/HMAC编码)
问题是我找不到将以下 python 代码更改为 Node.js 代码的适当方法
passphrase = base64.b64encode(
hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
我只想使用节点的 crypto 模块,但是在第一个 'sha256' 参数之后只有一个参数选项,如下所示。
crypto.createHmac('sha256', api_secret);
// There is no argument position of 'api_passphrase'
如何使用 两个 参数(如 node.js 上的 python 代码)创建 hmac?
【问题讨论】:
-
必须使用 NodeJS 代码中的
update()方法传递消息,并使用digest()方法进行终结。请参阅文档中的examples。 -
@Topaco thx,消化为“二进制”似乎是正确的,就像
.digest('binary')对吗? -
在 Python 代码中,您使用 base64 编码 (
base64.b64encode()),所以它应该是.digest('base64')。只需比较结果。
标签: python node.js encryption base64 hmac