Skip to content

Repository files navigation

@yuhere/js-object-hash

npm downloads npm coverage license TypeScript ESM

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.

Improvements over the original

  • 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-hash avoids npm namespace conflicts
  • Crypto abstraction — uses @yuhere/js-crypto as swappable backend instead of depending directly on Node.js crypto

import { objectHash } from '@yuhere/js-object-hash';

objectHash({foo: 'bar'}); // => 'a75c05bdca7d704bdfcd761913e5a4e4636e956b'
objectHash([1, 2, 2.718, 3.14159]); // => '136b9b88375971dff9f1af09d7356e3e04281951'

Versioning Disclaimer

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.

hash(value, options)

Generate a hash from any object or type. Defaults to sha1 with hex encoding.

  • algorithm hash 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 passthrough algorith, which will return the information that would otherwise have been hashed.
  • excludeValues {true|false} hash object keys, values ignored. default: false
  • encoding hash encoding, supports 'buffer', 'hex', 'binary', 'base64'. default: hex
  • ignoreUnknown {true|*false} ignore unknown object types. default: false
  • replacer optional function that replaces values before hashing. default: accept all values
  • respectFunctionProperties {true|false} Whether properties on functions are considered when hashing. default: true
  • respectFunctionNames {true|false} consider name property of functions for hashing. default: true
  • respectType {true|false} Whether special type attributes (.prototype, .__proto__, .constructor) are hashed. default: true
  • unorderedArrays {true|false} Sort all arrays before hashing. Note that this affects all collections, i.e. including typed arrays, Sets, Maps, etc. default: false
  • unorderedSets {true|false} Sort Set and Map instances before hashing, i.e. make 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 excluding specific key(s) from hashing, if true is returned then exclude from hash. default: include all keys

hash.sha1(value)

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.keys(value)

Hash object keys using the sha1 algorithm, values ignored.

Sugar method, equivalent to hash(value, {excludeValues: true})

hash.MD5(value)

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.keysMD5(value)

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})

hash.writeToStream(object, [options,] stream)

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,'

Installation

npm install @yuhere/js-object-hash

Example usage

import { 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==

Development

git clone https://github.com/yuhere/js-object-hash.git

Scripts

  • npm test unit tests
  • npm run test:browser browser unit tests
  • npm run test:coverage test coverage
  • npm run build build the library

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages