Skip to content

Instantly share code, notes, and snippets.

@turret-io
Created September 24, 2014 15:32
Show Gist options
  • Select an option

  • Save turret-io/e60ffbd9011a2396f79f to your computer and use it in GitHub Desktop.

Select an option

Save turret-io/e60ffbd9011a2396f79f to your computer and use it in GitHub Desktop.

Revisions

  1. turret-io created this gist Sep 24, 2014.
    25 changes: 25 additions & 0 deletions gen_hmac.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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);