Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/object_hash.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/object_hash_test.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var crypto = require('crypto');
* when hashing to distinguish between types
* - `unorderedArrays` {true|*false} Sort all arrays before hashing
* - `unorderedSets` {*true|false} Sort `Set` and `Map` instances before hashing
* - `hashingStream` optional stream that is used in place of the native hashing stream
* * = default
*
* @param {object} object value to hash
Expand Down Expand Up @@ -74,6 +75,7 @@ function applyDefaults(object, options){
options.unorderedObjects = options.unorderedObjects === false ? false : true; // default to true
options.replacer = options.replacer || undefined;
options.excludeKeys = options.excludeKeys || undefined;
options.hashingStream = options.hashingStream || undefined;

if(typeof object === 'undefined') {
throw new Error('Object argument required.');
Expand Down Expand Up @@ -113,7 +115,9 @@ function isNativeFunction(f) {
function hash(object, options) {
var hashingStream;

if (options.algorithm !== 'passthrough') {
if (options.hashingStream) {
hashingStream = options.hashingStream;
} else if (options.algorithm !== 'passthrough') {
hashingStream = crypto.createHash(options.algorithm);
} else {
hashingStream = new PassThrough();
Expand Down
9 changes: 9 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Generate a hash from any object or type. Defaults to sha1 with hex encoding.
`hash(new Set([1, 2])) == hash(new Set([2, 1]))` return `true`. default: true
* `unorderedObjects` {true|false} Sort objects before hashing, i.e. make `hash({ x: 1, y: 2 }) === hash({ y: 2, x: 1 })`. default: true
* `excludeKeys` optional function for exclude specific key(s) from hashing, if returns true then exclude from hash. default: include all keys
* `hashingStream` optional stream that is used in place of the native hashing stream

## hash.sha1(value);
Hash using the sha1 algorithm.
Expand Down Expand Up @@ -147,6 +148,14 @@ hash(michael, { algorithm: 'md5', encoding: 'base64' });
// djXaWpuWVJeOF8Sb6SFFNg==
hash(bob, { algorithm: 'md5', encoding: 'base64' });
// lFzkw/IJ8/12jZI0rQeS3w==

/***
* use custom hashStream
*/
const secret = 'abcdefg';
const hashStream = crypto.createHmac('sha256', secret);
hash(bob, { hashStream });
//
```

## Legacy Browser Support
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,11 @@ describe('hash', function() {
assert.equal(ha, hb, 'Hashing should not respect the order of Set entries');
});
}

it('custom hashingStream works', function() {
var ha;
ha = hash({a: 1, b: 4}, { hashingStream: crypto.createHmac('sha256', '123456') });

assert.equal(ha, '550abb83536475fc08989a28b4a9b3be220048dd0610affadf4d8829360fbea1', 'Custom hashingSteam should be used');
});
});