2

I need a node.js equivalent of the following Ruby code:

    require 'openssl'
    digest = OpenSSL::Digest::Digest.new('sha1')
    signature = OpenSSL::HMAC.hexdigest(digest, 'auth secret', 'some string')

I tried the following in node.js, but the Ruby signature is different from node's

    var crypto, signature;
    crypto = require('crypto');
    signature = crypto.createHash("sha1").update('auth secret').update('some string').digest("hex");
rafidude
  • 4,496
  • 7
  • 27
  • 23
  • Check out this question: http://stackoverflow.com/questions/4497135/node-js-and-crypto-library – Chance Sep 23 '11 at 19:36

1 Answers1

1

You're on the right track. You need to use the crypto#createHmac method instead of createHash, and pass it your secret (key) when creating it. This will give you what you're looking for:

var crypto = require('crypto')
  , hmac
  , signature;

hmac = crypto.createHmac("sha1", 'auth secret');
hmac.update('some string');

signature = hmac.digest("hex");
Evan Owen
  • 155
  • 6