Hasha is a convenient, user-friendly wrapper for the core 'crypto' Hash class in Node.js, offering a simpler API and superior default settings for hashing processes. Perfect for generating cryptographic hash values from a buffer, string, stream, or file, Hasha utilises best-practice defaults in its operations.
Hasha is versatile, easy to use and can be installed using npm:
$ npm install hasha
Once installed, you can use Hasha to hash a string or a buffer like so:
const hasha = require('hasha');
hasha('unicorn');
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
For asynchronous operations, use hasha.async
:
const hasha = require('hasha');
(async () => {
console.log(await hasha.async('unicorn'));
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
})();
To hash a file, you can use hasha.fromFile
:
const hasha = require('hasha');
(async () => {
// Get the MD5 hash of an image
const hash = await hasha.fromFile('unicorn.png', {algorithm: 'md5'});
console.log(hash);
//=> '1abcb33beeb811dca15f0ac3e47b88d9'
})();
The most comprehensive documentation for Hasha can be found on the Node.js crypto API documentation page: https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options. Here you'll find details about hashing and the options you can use in Hasha's methods. You can also refer to the GitHub repository here for additional details.