Hashing made simple. Get the hash of a buffer/string/stream/file.
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
media init May 23, 2015
.editorconfig ES2015ify and require Node.js 4 May 10, 2017
.gitattributes ES2015ify and require Node.js 4 May 10, 2017
.gitignore init May 23, 2015
.travis.yml ES2015ify and require Node.js 4 May 10, 2017
index.js ES2015ify and require Node.js 4 May 10, 2017
license init May 23, 2015
package.json 3.0.0 May 10, 2017
readme.md GitHub now natively supports SVG Apr 20, 2018
test.js ES2015ify and require Node.js 4 May 10, 2017

readme.md




hasha




Hashing made simple. Get the hash of a buffer/string/stream/file.

Build Status

Convenience wrapper around the core crypto Hash class with simpler API and better defaults.

Install

$ npm install hasha

Usage

const hasha = require('hasha');

hasha('unicorn');
//=> 'e233b19aabc7d5e53826fb734d1222f1f0444c3a3fc67ff4af370a66e7cadd2cb24009f1bc86f0bed12ca5fcb226145ad10fc5f650f6ef0959f8aadc5a594b27'
const hasha = require('hasha');

// Hash the process input and output the hash sum
process.stdin.pipe(hasha.stream()).pipe(process.stdout);
const hasha = require('hasha');

// Get the MD5 hash of an image
hasha.fromFile('unicorn.png', {algorithm: 'md5'}).then(hash => {
	console.log(hash);
	//=> '1abcb33beeb811dca15f0ac3e47b88d9'
});

API

See the Node.js crypto docs for more about hashing.

hasha(input, [options])

Returns a hash.

input

Type: Buffer string Buffer[] string[]

Buffer you want to hash.

While strings are supported you should prefer buffers as they're faster to hash. Although if you already have a string you should not convert it to a buffer.

Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation.

options

encoding

Type: string
Default: hex
Values: hex base64 buffer latin1 (Node.js 6+) binary (Node.js 4)

Encoding of the returned hash.

algorithm

Type: string
Default: sha512
Values: md5 sha1 sha256 sha512 (Platform dependent)

The md5 algorithm is good for file revving, but you should never use md5 or sha1 for anything sensitive. They're insecure.

hasha.stream([options])

Returns a hash transform stream.

hasha.fromStream(stream, [options])

Returns a Promise for a hash.

hasha.fromFile(filepath, [options])

Returns a Promise for a hash.

hasha.fromFileSync(filepath, [options])

Returns a hash.

Related

  • hasha-cli - CLI for this module
  • crypto-hash - Tiny hashing module that uses the native crypto API in Node.js and the browser
  • hash-obj - Get the hash of an object
  • md5-hex - Create a MD5 hash with hex encoding

License

MIT © Sindre Sorhus