drawkcaB | Backward Compatible logo

rants and tips about software

md5 in node.js

If you come from PHP background, you're used to PHP's global top-level functions for everything. Some people say it's PHP's curse, others praise it. I'm doing some Node.js stuff lately and needed equivalent of PHP's md5sum() function. Turns out, it's really simple and included into base Node.js install. You need to use the "crypto" module and generate md5 hash using createHash. "createHash" might sound confusing as data is not really hashed by the function. You create hash and then add data to it. After all data is in, you read the digest:

var crypto = require('crypto');
crypto.createHash('md5').update(data).digest('hex');

That's all folks. Happy noding :)

Milan Babuškov, 2011-12-01
Copyright © Milan Babuškov 2006-2014