crypto in node.js

January 22, 2012

For a few node projects of mine, I depend on hashing, predominantly MD5 and HMAC-SHA1 signing. Until recently, I depended on a library called hashlib for quickly generating my digests. Its got a very quick and simple syntax, and is written in C/C++.

var hashlib = require('hashlib');

hashlib.md5('something');
hashlib.hmac_sha1('something', 'key');

This worked really well for a while, until node 0.6 hit and there were some compatibility issues (on build the extension, and the occassional segfault). I turned away, and back to the crypto library, which is part of the standard library.

var crypto = require('crypto');

var md5er = crypto.createHash('md5');
md5er.update('something');
md5er.digest('hex');

var hmacer = crypto.createHmac('sha1', 'key');
hmacer.update('something');
hmacer.digest('hex');

The syntax, while longer, feels more flexible.

My true appreciation for hashlib, — if the issues are resolved I’ll definitely go back. For the time being, crypto is an easy replacement that’s stable in node 0.6+.

  • Patrick

    You just saved me a zillion hours trying to get hashlib running. Thank you, thank you, thank you!

    • http://seejohncode.com/ John Crepezzi

      Glad I could help! :)