Object Hash Generator
object-hash is a JavaScript library designed to generate consistent hashes from various JavaScript objects and primitive values in both Node.js and browser environments. It leverages Node.js's built-in `crypto` module for hashing algorithms like SHA1 (default), MD5, and others available via `crypto.getHashes()`, as well as supporting custom stream processing. The current stable version is 3.0.0. A key aspect of its API contract, established since version 1.1.8 (April 2017), is that any changes affecting the exact returned hash value are considered `semver-major`, ensuring predictability for users. It differentiates itself by offering extensive configuration options, including the ability to ignore values, sort arrays/objects/sets for order-independent hashing, and respect or ignore function properties, making it highly flexible for use cases like caching, deduplication, or content addressing where object state needs to be consistently fingerprinted. It is important to note that default algorithms like SHA-1 and MD5 are not considered cryptographically secure and should not be used for security-sensitive applications.
Common errors
-
TypeError: crypto.createHash is not a function
cause This error typically occurs in browser environments where Node.js's built-in `crypto` module is not available or properly polyfilled.fixIf bundling for the browser, configure your bundler (e.g., Webpack, Rollup) to polyfill the Node.js `crypto` module or use a browser-specific hashing library. Some bundlers automatically polyfill, but explicit configuration might be needed. -
Error: Cannot find module 'object-hash'
cause The package `object-hash` is not installed or not resolvable in the current environment.fixRun `npm install object-hash` or `yarn add object-hash` in your project directory. Ensure your module resolution paths are correctly configured if working in a monorepo or custom environment. -
TypeError: hash is not a function (when using ESM 'import { hash } from ...')cause `object-hash` provides a default export function, not a named export `hash`.fixChange your import statement to `import hash from 'object-hash';` for ESM, or use `import * as hash from 'object-hash';` for TypeScript/ESM interop with CommonJS modules.
Warnings
- breaking Prior to version 1.1.8 (released April 2017), changes to the exact hash output were not considered semver-major, meaning minor or patch updates could alter hash values. Since 1.1.8, any change affecting hash values is a breaking change and will be reflected in a major version bump.
- gotcha The default hashing algorithm, SHA-1, and the readily available MD5 algorithm are not considered cryptographically secure for modern applications. They are vulnerable to collision attacks and should not be used for security-sensitive purposes like password hashing or digital signatures.
- gotcha The `unorderedArrays` option, when set to `true`, affects the sorting of *all* iterable collections, including plain JavaScript arrays, `Set` instances, `Map` instances, and typed arrays. This might lead to unexpected behavior if users only anticipate it for basic arrays.
- gotcha In browser environments, `object-hash` relies on the Node.js `crypto` module. If not properly polyfilled or bundled for the browser, this can lead to runtime errors.
- gotcha Hash values generated by `object-hash` may differ from simple `JSON.stringify()` followed by a standard hashing algorithm (e.g., `crypto.createHash`). This is because `object-hash` prefixes values with their types and handles various JavaScript primitives and objects (e.g., functions, dates) differently.
Install
-
npm install object-hash -
yarn add object-hash -
pnpm add object-hash
Imports
- hash
import { hash } from 'object-hash';import hash from 'object-hash';
- hash
const hash = require('object-hash'); - hash.sha1
import { sha1 } from 'object-hash';const hash = require('object-hash'); const myHash = hash.sha1({ a: 1 });
Quickstart
const hash = require('object-hash');
// Basic hashing of an object
const objectHash = hash({ foo: 'bar', baz: [1, 2, { a: 3 }] });
console.log('Object Hash:', objectHash); // Example: '37a3c3d5e21d5a7b6c7a9f8d9b6e2d1f0a8c4b2e'
// Hashing with specific algorithm and encoding (e.g., SHA256)
const sha256Hash = hash({ data: 'sensitive info' }, { algorithm: 'sha256', encoding: 'base64' });
console.log('SHA256 Hash (Base64):', sha256Hash);
// Hashing an object, ignoring values (only keys contribute to hash)
const keysOnlyHash = hash({ name: 'Alice', age: 30 }, { excludeValues: true });
console.log('Keys-only Hash:', keysOnlyHash);
// Hashing with unordered objects and arrays for consistent results regardless of property/element order
const unorderedHash1 = hash({ x: 1, y: 2 }, { unorderedObjects: true, unorderedArrays: true });
const unorderedHash2 = hash({ y: 2, x: 1 }, { unorderedObjects: true, unorderedArrays: true });
console.log('Unordered Object Hash 1:', unorderedHash1);
console.log('Unordered Object Hash 2:', unorderedHash2);
console.log('Unordered hashes match:', unorderedHash1 === unorderedHash2);