需要等效的 JS

Need JS equivalent

Python代码:

signature = hmac.new(bytearray.fromhex(key), data.encode('utf-8'), hashlib.sha256).hexdigest()

我尝试过的解决方案

var compute_hmac = crypto.createHmac('sha256', key).update(data).digest('hex');

var compute_hmac = crypto.createHmac('sha256', Buffer.from(key, 'hex').toString()).update(data).digest('hex');

const hmac = crypto.createHmac('sha256', Buffer.from(key, 'hex'))

正在尝试验证以下 API

的 webhook 签名

https://developer.close.com/topics/webhooks/

data是接收到的payload,同样的东西传给python和JS代码。但是不知何故,python 代码的十六进制摘要得到了验证,而 JS 代码的十六进制代码完全不同。

请参考上面提到的 API link(webhook 签名)以了解我要实现的目标

直接传递 keybuffer 而不是向其添加 .toString()

var compute_hmac = crypto.createHmac('sha256', Buffer.from(key, 'hex')).update(data).digest('hex');

py代码

import hashlib
import hmac

key ="A1FF92";
data = "hello"

signature = hmac.new(bytearray.fromhex(key), data.encode('utf-8'), hashlib.sha256).hexdigest()
//78a1151ddd4f298a134e4625362af2ab8ef4bd49719e17053ec1eadd4cbf1bab

节点代码

var crypto = require("crypto")
var key = "A1FF92"
var data="hello";
var compute_hmac = crypto.createHmac('sha256', Buffer.from(key, 'hex')).update(data).digest('hex');

// 78a1151ddd4f298a134e4625362af2ab8ef4bd49719e17053ec1eadd4cbf1bab