Created
September 24, 2014 15:32
-
-
Save turret-io/e60ffbd9011a2396f79f to your computer and use it in GitHub Desktop.
Generate HMAC in NodeJS
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
var crypto = require('crypto'); | |
var SHARED_SECRET = "sup3rs3cr3t!!"; | |
function signString(string_to_sign, shared_secret) { | |
var hmac = crypto.createHmac('sha512', shared_secret); | |
hmac.write(string_to_sign); | |
hmac.end() | |
return hmac.read(); | |
} | |
var payload = { | |
'name': 'joe smith', | |
'category': 'people', | |
'action': 'transport', | |
'where': 'pluto', | |
'timestamp': Math.round(new Date().getTime()/1000) | |
}; | |
var json_payload = JSON.stringify(payload); | |
var signature = signString(json_payload, SHARED_SECRET); | |
var encoded_json = new Buffer(json_payload).toString('base64'); | |
var encoded_signature = new Buffer(signature).toString('base64'); | |
console.log("/?data="+encoded_json+"&signature="+encoded_signature); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment