I currently have this snippet in Python -
import base64 import hashlib import hmac def hash_hmac(self, key, data): res = hmac.new(base64.b64decode(key), data.encode(), hashlib.sha512).digest() return res I am trying to replicate it in Node.js, but having difficulty getting the correct hash.
const crypto = require('crypto') const hashHmac = (key, message) => { return crypto.createHmac('sha512', Buffer.from(key, 'base64').toString('utf-8')) .update(message) .digest() .toString('base64') } Test case: Key: '7pgj8Dm6' Message: 'Test\0Message'
With the python snippet, the hash is
69H45OZkKcmR9LOszbajUUPGkGT8IqasGPAWqW/1stGC2Mex2qhIB6aDbuoy7eGfMsaZiU8Y0lO3mQxlsWNPrw== With the js snippet, the hash is
OhaJU9IibhhjIjz3R7FmodgOBUPjwhndXX8gn0r2rRzjQvCJl4T40rHXKw3o6Y2JQ5fVHTeStu8K1DRMWxMGBg== Am I going wrong with the base64 encoding?
Buffer.from(key, 'base64').toString('utf-8')withBuffer.from(key, 'base64'). The UTF-8 decoding corrupts the data.