Hasha: A Friendly Crypto API

2015-06-12 17:12:34 +0100 by Alex R. Young

Are you a fan of Node's crypto API? Like the other core modules, it tries to strike a balance between providing the bare minimum and just enough abstraction. You shouldn't need to be a cryptography expert to use it, but at the same time it doesn't want to be too limiting. However, whenever I need to quickly md5 or sha1 something, it feels like too much work. I also find it a little difficult to explain to beginners.


Sindre Sorhus has written a module called hasha (GitHub: sindresorhus/hasha, License: MIT, npm: hasha). It provides a more friendly wrapper around crypto that uses sensible defaults. To get a hex encoding of a string, you can just use hasha(new Buffer('unicorn')). If you want to hash a file, then use hasha.fromFile with a callback. You can supply the desired hashing algorithm as well:

hasha.fromFile('unicorn.png', { algorithm: 'md5' }, function(err, hash) {
  //=> '1abcb33beeb811dca15f0ac3e47b88d9'

There's also hasha.fromFileSync(input, [options]). Sindre suggests using buffers instead of strings for speed, so the earlier example could have been hasha('unicorn').

If you need to hash a stream, then use hasha.stream:


It also has a command-line tool, so you can hash a string in the terminal with hasha unicorn or pipe something into it.

It's a handy module, and could be one of those small modules I install instinctively, like mkdirp and through2.