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.
npm install object-hashVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic object hashing, specifying a different algorithm and encoding, hashing only object keys, and ensuring order-independent hashing for objects and arrays.
For applications relying on consistent hash outputs across minor versions, ensure you are using `object-hash@^1.1.8` or newer and strictly adhere to semver for major version updates.
For security-sensitive applications, explicitly specify a stronger algorithm like `sha256` or `sha512` using the `algorithm` option (e.g., `{ algorithm: 'sha256' }`).Understand the broad impact of `unorderedArrays`. If you only need order-independent hashing for specific types, consider using `unorderedSets` or `unorderedObjects` selectively, or implement a custom `replacer` function for fine-grained control.
When using `object-hash` in a browser environment, ensure your build process (e.g., Webpack, Rollup with appropriate plugins) provides a compatible polyfill for Node.js's `crypto` module, or use a library specifically designed for browser crypto APIs like `window.crypto.subtle`.
When comparing hashes with other systems, be aware that `object-hash` employs a specific internal serialization strategy. If interoperability is required, either both systems must use `object-hash`, or the hashing logic of `object-hash` (which involves type-prefixing) must be replicated. This behavior is not explicitly documented and may require inspecting the source code for precise replication.
If 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.
Run `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.
Change 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.
No dependency data recorded yet.