Last active
January 25, 2023 08:11
-
-
Save mikaturunen/ffc1a29510b52f5e8bdb5cfd2f7d08c0 to your computer and use it in GitHub Desktop.
Example of calculating hmac in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
In new versions of node.js use
Buffer.from(string)
instead ofnew Buffer(string)