Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
The hash-it npm package is a utility for creating unique hash values from JavaScript objects. It is particularly useful for creating hash keys for complex objects, ensuring that the same object will always produce the same hash value.
Hashing Objects
This feature allows you to generate a unique hash value for a given JavaScript object. The same object will always produce the same hash value, which is useful for caching or quick comparisons.
const { hash } = require('hash-it');
const obj = { a: 1, b: 2 };
const hashValue = hash(obj);
console.log(hashValue);
Hashing Arrays
This feature allows you to generate a unique hash value for an array. It ensures that the same array will always produce the same hash value, which can be useful for array comparisons or caching.
const { hash } = require('hash-it');
const arr = [1, 2, 3];
const hashValue = hash(arr);
console.log(hashValue);
Hashing Nested Structures
This feature allows you to generate a unique hash value for nested structures, including objects within objects and arrays within objects. It ensures consistency in hash values for complex data structures.
const { hash } = require('hash-it');
const nestedObj = { a: { b: 2 }, c: [1, 2, 3] };
const hashValue = hash(nestedObj);
console.log(hashValue);
The object-hash package provides similar functionality to hash-it by generating unique hash values for JavaScript objects. It supports a variety of hashing algorithms and can handle complex nested structures. Compared to hash-it, object-hash offers more customization options for the hashing process.
The hasha package is a versatile hashing utility that supports multiple hashing algorithms like MD5, SHA-1, and SHA-256. While it is not specifically designed for hashing objects, it can be used to hash strings, buffers, and other data types. It is more general-purpose compared to hash-it.
The built-in Node.js crypto module provides a wide range of cryptographic functionalities, including hashing. While it is more complex and requires more setup to hash objects compared to hash-it, it offers extensive options for cryptographic operations and is highly customizable.
Fast and consistent hashCode for any object type
$ npm i hash-it --save
// ES2015
import hashIt from 'hash-it';
// CommonJS
const hashIt = require('hash-it');
// script
const hashIt = window.hashIt;
const hashedObject = hashIt({
foo: 'bar'
});
console.log(hashedObject);
// 2196683918
hashIt
has a simple goal: provide a fast and consistent hashCode for any object type that is uniquely based on its values. This has a number of uses such as duplication prevention or fast equality comparisons.
Any object type?
Yes, any object type. Primitives, ES2015 classes like Symbol
, DOM elements (yes, you can even hash the window
object if you want). Any object type.
With no exceptions?
Well ... sadly, no, there are a few exceptions.
Math
WeakMap
WeakSet
In the case of Math
, it's not a constructor (really just a parent for methods), so there are no values to hash. In the case of both WeakMap
and WeakSet
, the spec explicitly forbids iteration over them, so the unique values cannot be discovered. In each of these three cases, no matter what the contents of the object, they will always yield the same hash result, which is unique to each object type.
Here is the list of object classes that have been tested and shown to produce unique hashCodes:
Array
ArrayBuffer
Boolean
Date
(based on valueOf
)Error
(based on message
)Float32Array
Float64Array
Function
(based on name
and arguments
)Int8Array
Int16Array
Int32Array
HTMLElement
(based on innerHTML
, includes all sub-types, e.g. HTMLAnchorElement
, HTMLDivElement
, etc)Map
Null
Number
Object
(can handle recursive objects)RegExp
Set
String
Symbol
Uint8Array
Uint8ClampedArray
Uint16Array
Uint32Array
Undefined
Window
This is basically all I could think of, but if I have missed an object class let me know and I will add it!
While the hashes will be consistent when calculated within the same browser environment, there is no guarantee that the hashCode will be the same across different browsers due to browser-specific implementations of features. A vast majority of the time things line up, but there are some edge cases that cause differences, so just something to be mindful of.
hashIt
has been tested on the following browsers:
There are no DOM dependencies, so hashIt
should work in Node as well.
Standard stuff, clone the repo and npm i
to get the dependencies. npm scripts available:
build
=> builds the distributed JS with NODE_ENV=development
and with sourcemapsbuild-minified
=> builds the distributed JS with NODE_ENV=production
and minifiedcompile-for-publish
=> runs the lint
, test
, transpile
, dist
scriptsdev
=> runs the webpack dev server for the playgrounddist
=> runs the build
and build-minified
lint
=> runs ESLint against files in the src
folderprepublish
=> if in publish, runs compile-for-publish
test
=> run ava with NODE_ENV=testtest:watch
=> runs test
but with persistent watchertranspile
=> runs Babel against files in src
to files in lib
1.0.0
FAQs
Hash any object based on its value
The npm package hash-it receives a total of 300,020 weekly downloads. As such, hash-it popularity was classified as popular.
We found that hash-it demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.