Skip to content

Instantly share code, notes, and snippets.

@buren
Created November 6, 2022 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buren/ea283867f3401daafb2bcfb1c1c0a4d3 to your computer and use it in GitHub Desktop.
Save buren/ea283867f3401daafb2bcfb1c1c0a4d3 to your computer and use it in GitHub Desktop.
HMAC SHA256
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
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