What is node-object-hash?
The node-object-hash package is a utility for creating consistent hash values from JavaScript objects. It is useful for tasks such as caching, object comparison, and ensuring data integrity.
What are node-object-hash's main functionalities?
Hashing Objects
This feature allows you to create a hash from a JavaScript object. The hash can be used to uniquely identify the object.
const objectHash = require('node-object-hash');
const hasher = objectHash();
const obj = { name: 'John', age: 30 };
const hash = hasher.hash(obj);
console.log(hash);
Custom Hashing Options
This feature allows you to customize the hashing process by specifying options such as coercion and the hashing algorithm.
const objectHash = require('node-object-hash');
const hasher = objectHash({ coerce: true, alg: 'sha256' });
const obj = { name: 'John', age: 30 };
const hash = hasher.hash(obj);
console.log(hash);
Hashing Arrays
This feature allows you to create a hash from an array, ensuring that the array's contents are uniquely identified.
const objectHash = require('node-object-hash');
const hasher = objectHash();
const arr = [1, 2, 3, 4];
const hash = hasher.hash(arr);
console.log(hash);
Other packages similar to node-object-hash
object-hash
The object-hash package is another utility for creating hash values from JavaScript objects. It offers similar functionality to node-object-hash but with a different API and additional features such as support for circular references.
hash-it
The hash-it package provides a simple and fast way to hash JavaScript objects. It is designed to be lightweight and easy to use, making it a good alternative to node-object-hash for basic hashing needs.
crypto
The built-in crypto module in Node.js can also be used to create hash values from objects. While it requires more manual setup compared to node-object-hash, it offers greater flexibility and is part of the standard library.
node-object-hash
Node object hash library. Built on top of node's crypto module.
Installation
npm i node-object-hash
Features
- Supports object property sorting for constant hashes for objects with same properties, but different order.
- NOTE: object arrays should be sorted manually if needed
- Supports ES6 Maps and Sets.
- Supports type coercion (e.g. 1 and "1" will be the same)
- rules:
- numbers and strings represented without quotes;
- boolean values converted to numbers;
- Supports all hashes and encodings of crypto library
- Supports large objects and arrays
API
hash(object[, options])
Returns hash string.
object
object for calculating hash;options
object with options;options.alg
hash algorithm (default: 'sha256'
);options.enc
hash encoding (default: 'hex'
);options.coerce
if true performs type coercion (default: true
);options.sort
if true performs sorting on objects, arrays and Sets (default: true
);
sortedObjectString(object[, options])
Returns sorted string generated from object
object
object for calculating hash;options
object with options;options.coerce
if true performs type coercion (default: true
);options.sort
if true performs sorting on objects, arrays and Sets (default: true
);
Requirements
Example
const {hash} = require('node-object-hash');
const object = {
x: new Map([['a', 1], ['c', 3], ['b', 2]]),
z: new Set([4,3,2,1]),
f: 2,
e: [{a:2}, 3, {g:2, e:{d:1}}, "rt", "1", "2abc", "3d"],
d: [3,5,6,1,2,3],
c: "3ab",
b: "2",
a: 1
};
const hashString = hash(object);
const hashStringSha512 = hash(object, {alg:'sha512'});
Compared to same libraries
License
ISC