Skip to content

Instantly share code, notes, and snippets.

@mikaturunen
Last active January 25, 2023 08:11
Show Gist options
  • Save mikaturunen/ffc1a29510b52f5e8bdb5cfd2f7d08c0 to your computer and use it in GitHub Desktop.
Save mikaturunen/ffc1a29510b52f5e8bdb5cfd2f7d08c0 to your computer and use it in GitHub Desktop.
Example of calculating hmac in node.js
import * as crypto from 'crypto'
const merchantSecret = 'secret'
const hash = 'sha256'
const encoding = 'base64'
const payload = {
id: 123,
somethingElse: 'not 123'
}
const hmac = crypto
.createHmac(hash, merchantSecret)
.update(new Buffer(JSON.stringify(payload)).toString(encoding))
.digest('hex')
.toUpperCase()
@lalosh
Copy link

lalosh commented May 30, 2022

In new versions of node.js use Buffer.from(string) instead of new Buffer(string)

@advename
Copy link

@lalosh or @mikaturunen - I'm curious why nobody suggested ordering the object before conversion?
Moreover, why even go so far and convert the Buffer to a base64 string. Why not leave it at Buffer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment