Skip to content

Instantly share code, notes, and snippets.

@turret-io
Created September 24, 2014 15:32
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save turret-io/e60ffbd9011a2396f79f to your computer and use it in GitHub Desktop.
Generate HMAC in NodeJS
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