Skip to content

Latest commit

 

History

History
55 lines (38 loc) · 1.88 KB

File metadata and controls

55 lines (38 loc) · 1.88 KB
description Modern alternatives to object-hash for hashing objects and values

Replacements for object-hash

ohash

ohash is actively maintained and provides hashing, stable serialization, equality checks, and diffs. It uses stable serialization + SHA-256, returning Base64URL by default. Its serializer was originally based on object-hash.

Example:

import objectHash from 'object-hash' // [!code --]
import { hash } from 'ohash' // [!code ++]

const h = objectHash(obj) // [!code --]
const h = hash(obj) // [!code ++]

Web Crypto

Use the standard SubtleCrypto.digest available in modern runtimes. Pair it with a stable serializer (e.g., safe-stable-stringify) to ensure deterministic key ordering.

Example:

import objectHash from 'object-hash' // [!code --]
import stringify from 'safe-stable-stringify' // [!code ++]

const h = objectHash(obj, { algorithm: 'sha256' }) // [!code --]
const data = new TextEncoder().encode(stringify(obj)) // [!code ++]
const buf = await crypto.subtle.digest('SHA-256', data) // [!code ++]
const h = Array.from(new Uint8Array(buf)) // [!code ++]
  .map((b) => b.toString(16).padStart(2, '0')) // [!code ++]
  .join('') // [!code ++]

Bun CryptoHasher

Bun provides a native incremental hasher (e.g., SHA-256). Combine it with a stable serializer for object hashing. For fast non-crypto fingerprints, see Bun.hash.

Docs: https://bun.com/reference/bun/CryptoHasher

Example:

import objectHash from 'object-hash' // [!code --]
import stringify from 'safe-stable-stringify' // [!code ++]

const h = objectHash(obj, { algorithm: 'sha256' }) // [!code --]
const hasher = new CryptoHasher('sha256') // [!code ++]
hasher.update(stringify(obj)) // [!code ++]
const h = hasher.digest('hex') // [!code ++]