Home > front end >  Need JS equivalent
Need JS equivalent

Time:02-12

Python Code:

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

Solutions That I have tried

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'))

Trying to validate webhook signatures of the following API

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

data is the payload received, the same thing is passed to python and JS code. But somehow, hex digest of python code is validated and hex code of JS code is entirely different.

Please refer to API link mentioned above (webhook signatures) to understand what I'm trying to achieve

CodePudding user response:

Pass directly the keybuffer instead of adding .toString() to it

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

py code

import hashlib
import hmac

key ="A1FF92";
data = "hello"

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

node code

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
  • Related