Created
November 6, 2022 11:00
-
-
Save buren/ea283867f3401daafb2bcfb1c1c0a4d3 to your computer and use it in GitHub Desktop.
HMAC SHA256
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 crypto from 'crypto' | |
// inputs | |
const key = 'notsosecret' | |
const method = 'POST' | |
const url = 'https://example.com' | |
const timestamp = '2022-11-03T01:08:19.138Z' | |
const data = '{"data":{"partnerId":"1","tokenId":47210468}}' | |
// generate signature | |
const hmac = crypto.createHmac('SHA256', key) | |
hmac.update(`${method}${url}${timestamp}${data}`) | |
const hex = hmac.digest('hex') // signature header of webhook | |
console.log('hex:', hex) | |
// hex: 7226d389df2eff12b774ec57c51bf4d0bd78d5dfe8ab7addaf90f1f13910a3aa |
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
require 'openssl' | |
# inputs | |
key = 'notsosecret' | |
data = '{"data":{"partnerId":"1","tokenId":47210468}}' | |
method = 'POST' | |
url = 'https://example.com' | |
timestamp = '2022-11-03T01:08:19.138Z' | |
# generate signature | |
digest = OpenSSL::Digest.new('sha256') | |
hex = OpenSSL::HMAC.hexdigest(digest, key, [method, url, timestamp, data].join) | |
puts "hex: #{hex}" | |
# hex: 7226d389df2eff12b774ec57c51bf4d0bd78d5dfe8ab7addaf90f1f13910a3aa |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment