Forked from puleos/object-hash. Hash values are compatible with the original — migration requires only updating your import statements.
Generate hashes from objects and values in node and the browser.
- Hash values of any type.
- Supports a keys only option for grouping similar objects with different values.
- TypeScript — full type declarations (
.d.ts) generated from source, with all public API surface typed - ESM — native ES module (
import/export), works with modern bundlers and Node.js ESM - Vite + Vitest — fast, modern build pipeline replacing gulp + mocha; bundler-free dev experience
- Dual-environment coverage — Istanbul coverage merged across Node.js and Chromium (Playwright) test runs
- Tree-shakable — named exports allow bundlers to eliminate unused sugar methods
- Scoped package —
@yuhere/js-object-hashavoids npm namespace conflicts - Crypto abstraction — uses
@yuhere/js-cryptoas swappable backend instead of depending directly on Node.jscrypto
import { objectHash } from '@yuhere/js-object-hash';
objectHash({foo: 'bar'}); // => 'a75c05bdca7d704bdfcd761913e5a4e4636e956b'
objectHash([1, 2, 2.718, 3.14159]); // => '136b9b88375971dff9f1af09d7356e3e04281951'Starting with version 1.1.8 (released April 2017), new versions will consider
the exact returned hash part of the API contract, i.e. changes that will affect
hash values will be considered semver-major. Previous versions may violate
that expectation.
For more information, see this discussion.
Generate a hash from any object or type. Defaults to sha1 with hex encoding.
algorithmhash algo to be used: 'sha1', 'md5', 'passthrough'. default: sha1- This supports the algorithms returned by
crypto.getHashes(). Note that the default of SHA-1 is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired. - This also supports the
passthroughalgorith, which will return the information that would otherwise have been hashed.
- This supports the algorithms returned by
excludeValues{true|false} hash object keys, values ignored. default: falseencodinghash encoding, supports 'buffer', 'hex', 'binary', 'base64'. default: hexignoreUnknown{true|*false} ignore unknown object types. default: falsereplaceroptional function that replaces values before hashing. default: accept all valuesrespectFunctionProperties{true|false} Whether properties on functions are considered when hashing. default: truerespectFunctionNames{true|false} considernameproperty of functions for hashing. default: truerespectType{true|false} Whether special type attributes (.prototype,.__proto__,.constructor) are hashed. default: trueunorderedArrays{true|false} Sort all arrays before hashing. Note that this affects all collections, i.e. including typed arrays, Sets, Maps, etc. default: falseunorderedSets{true|false} SortSetandMapinstances before hashing, i.e. makehash(new Set([1, 2])) == hash(new Set([2, 1]))returntrue. default: trueunorderedObjects{true|false} Sort objects before hashing, i.e. makehash({ x: 1, y: 2 }) === hash({ y: 2, x: 1 }). default: trueexcludeKeysoptional function for excluding specific key(s) from hashing, if true is returned then exclude from hash. default: include all keys
Hash using the sha1 algorithm.
Note that SHA-1 is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
Sugar method, equivalent to hash(value, {algorithm: 'sha1'})
Hash object keys using the sha1 algorithm, values ignored.
Sugar method, equivalent to hash(value, {excludeValues: true})
Hash using the md5 algorithm.
Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
Sugar method, equivalent to hash(value, {algorithm: 'md5'})
Hash object keys using the md5 algorithm, values ignored.
Note that the MD5 algorithm is not considered secure, and a stronger algorithm should be used if a cryptographical hash is desired.
Sugar method, equivalent to hash(value, {algorithm: 'md5', excludeValues: true})
Write the information that would otherwise have been hashed to a stream, e.g.:
import { writeToStream } from '@yuhere/js-object-hash';
writeToStream({foo: 'bar', a: 42}, {respectType: false}, process.stdout);
// => e.g. 'object:2:string:1:a:number:42,string:3:foo:string:3:bar,'npm install @yuhere/js-object-hashimport { objectHash, keys, MD5, keysMD5 } from '@yuhere/js-object-hash';
var peter = { name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
var michael = { name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };
var bob = { name: 'Bob', stapler: true, friends: [] };
/***
* sha1 hex encoding (default)
*/
objectHash(peter);
// 4d73157b6a4d824733a9a4853be98643cfd4b40e
objectHash(michael);
// 6a713c08cffec5c4e8f6dab4095b8a909d1abe16
objectHash(bob);
// d47abbd616733c4bcdd88c527e9e58834a56e6a0
/***
* hash object keys, values ignored
*/
objectHash(peter, { excludeValues: true });
// 0d02a827823e7a7370a5d9a574db901b404a97e8
objectHash(michael, { excludeValues: true });
// 0d02a827823e7a7370a5d9a574db901b404a97e8
keys(bob);
// 0d02a827823e7a7370a5d9a574db901b404a97e8
/***
* hash object, ignore specific key(s)
*/
objectHash(peter, {
excludeKeys: function(key) {
if (key === 'friends') {
return true;
}
return false;
}
});
// 179f2a007f8e170f3757ffd3750c98bf4bed8e34
/***
* md5 base64 encoding
*/
objectHash(peter, { algorithm: 'md5', encoding: 'base64' })
// xBOnYGs21wSCxLuGPYAQWQ==
objectHash(michael, { algorithm: 'md5', encoding: 'base64' });
// wK8u0kzhZF4B69ujnzq6Pw==
objectHash(bob, { algorithm: 'md5', encoding: 'base64' });
// qhUQGcuobgSyfl94cnErvg==git clone https://github.com/yuhere/js-object-hash.gitnpm testunit testsnpm run test:browserbrowser unit testsnpm run test:coveragetest coveragenpm run buildbuild the library
MIT