
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
stable-hash
Advanced tools
A tiny and fast (481b unpkg) lib for "stably hashing" a JavaScript value. Originally created for SWR.
It's similar to JSON.stringify(value)
, but:
yarn add stable-hash
import hash from 'stable-hash'
hash(anyJavaScriptValueHere) // returns a string
hash(1)
hash('foo')
hash(true)
hash(undefined)
hash(null)
hash(NaN)
BigInt:
hash(1) === hash(1n)
hash(1) !== hash(2n)
Symbol:
hash(Symbol.for('foo')) === hash(Symbol.for('foo'))
hash(Symbol.for('foo')) === hash(Symbol('foo'))
hash(Symbol('foo')) === hash(Symbol('foo'))
hash(Symbol('foo')) !== hash(Symbol('bar'))
Since Symbols cannot be serialized, stable-hash simply uses its description as the hash.
hash(/foo/) === hash(/foo/)
hash(/foo/) !== hash(/bar/)
hash(new Date(1)) === hash(new Date(1))
hash([1, '2', [new Date(3)]]) === hash([1, '2', [new Date(3)]])
hash([1, 2]) !== hash([2, 1])
Circular:
const foo = []
foo.push(foo)
hash(foo) === hash(foo)
hash({ foo: 'bar' }) === hash({ foo: 'bar' })
hash({ foo: { bar: 1 } }) === hash({ foo: { bar: 1 } })
Stable:
hash({ a: 1, b: 2, c: 3 }) === hash({ c: 3, b: 2, a: 1 })
Circular:
const foo = {}
foo.foo = foo
hash(foo) === hash(foo)
stable-hash
guarantees reference consistency (===
) for objects that the constructor isn't Object
.
const foo = () => {}
hash(foo) === hash(foo)
hash(foo) !== hash(() => {})
class Foo {}
hash(Foo) === hash(Foo)
hash(Foo) !== hash(class {})
const foo = new Set([1])
hash(foo) === hash(foo)
hash(foo) !== hash(new Set([1]))
This function does something similar to JSON.stringify
, but more than it. It doesn't generate a secure checksum, which usually has a fixed length and is hard to be reversed. With stable-hash
it's still possible to get the original data. Also, the output might include any charaters, not just alphabets and numbers like other hash algorithms. So:
import crypto from 'crypto'
import hash from 'stable-hash'
const weakHash = hash(anyJavaScriptValueHere)
const encodedHash = Buffer.from(weakHash).toString('base64')
const safeHash = crypto.createHash('MD5').update(weakHash).digest('hex')
Also, the consistency of this lib is sometimes guaranteed by the singularity of the WeakMap instance. So it might not generate the consistent results when running in different runtimes, e.g. server/client or parent/worker scenarios.
Created by Shu Ding. Released under the MIT License.
FAQs
Stable JS value hash.
The npm package stable-hash receives a total of 4,143,587 weekly downloads. As such, stable-hash popularity was classified as popular.
We found that stable-hash demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.