Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
stable-hash
Advanced tools
A small (496b) lib for stable hashing a JavaScript value. Originally created for SWR.
It's similar to JSON.stringify(value)
, but:
value
can be any JavaScript valueyarn add stable-hash
import stableHash from 'stable-hash'
stableHash(anyJavaScriptValueHere) // returns a string
stableHash(1)
stableHash('foo')
stableHash(true)
stableHash(undefined)
stableHash(null)
stableHash(NaN)
BigInt:
stableHash(1) === stableHash(1n)
stableHash(1) !== stableHash(2n)
Symbol:
stableHash(Symbol.for('foo')) === stableHash(Symbol.for('foo'))
stableHash(Symbol.for('foo')) === stableHash(Symbol('foo'))
stableHash(Symbol('foo')) === stableHash(Symbol('foo'))
stableHash(Symbol('foo')) !== stableHash(Symbol('bar'))
Since Symbols cannot be serialized, stable-hash simply uses its description as the hash.
stableHash(/foo/) === stableHash(/foo/)
stableHash(/foo/) !== stableHash(/bar/)
stableHash(new Date(1)) === stableHash(new Date(1))
stableHash([1, '2', [new Date(3)]]) === stableHash([1, '2', [new Date(3)]])
stableHash([1, 2]) !== stableHash([2, 1])
Circular:
const foo = []
foo.push(foo)
stableHash(foo) === stableHash(foo)
stableHash({ foo: 'bar' }) === stableHash({ foo: 'bar' })
stableHash({ foo: { bar: 1 } }) === stableHash({ foo: { bar: 1 } })
Stable:
stableHash({ a: 1, b: 2, c: 3 }) === stableHash({ c: 3, b: 2, a: 1 })
Circular:
const foo = {}
foo.foo = foo
stableHash(foo) === stableHash(foo)
stable-hash
guarantees reference consistency (===
) for objects that the constructor isn't Object
.
const foo = () => {}
stableHash(foo) === stableHash(foo)
stableHash(foo) !== stableHash(() => {})
class Foo {}
stableHash(Foo) === stableHash(Foo)
stableHash(Foo) !== stableHash(class {})
const foo = new Set([1])
stableHash(foo) === stableHash(foo)
stableHash(foo) !== stableHash(new Set([1]))
This function does something similar to serialization. It doesn't generate a secure checksum or digest, which usually has a fixed length and is hard to be reversed. With stable-hash
it's likely possible to get the original data. Also, the output might include any charaters, not just alphabets and numbers like other hash algorithm. So:
import crypto from 'crypto'
import stableHash from 'stable-hash'
const hash = stableHash(anyJavaScriptValueHere)
const encodedHash = Buffer.from(hash).toString('base64')
const safeHash = crypto.createHash('MD5').update(hash).digest('hex')
Created by Shu Ding. Released under the MIT License.
FAQs
Stable JS value hash.
The npm package stable-hash receives a total of 1,944,243 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 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.