compact-hash
v0.1.0
Published
Strip every whitespace character from a string, then hash the result (MurmurHash3 32-bit).
Downloads
16
Maintainers
Readme
compact-hash
Strip every whitespace character from a string, then hash the rest with a hash function of your choice.
- Default hash: MurmurHash3 x86 32-bit — fast, well-distributed, non-cryptographic. Zero runtime dependencies.
- Pluggable: pass any
(string) => stringclosure (sha256, xxhash, blake3, …) to swap algorithms without pulling in another lib here. - Sync API, works in Node and the browser. Dual ESM + CJS build with TypeScript types.
Install
npm install compact-hash
# or
pnpm add compact-hashUsage
import { compactHash } from 'compact-hash';
// Default: MurmurHash3 32-bit → 8-char hex
compactHash('hello world'); // 'a03719ca'
// Inputs that differ only in whitespace collapse to the same digest:
compactHash('hello world') === compactHash('helloworld'); // true
compactHash('hello\n\tworld') === compactHash('hello world'); // true
compactHash('你好 世界') === compactHash('你好世界'); // truePlug in a different hash
compactHash takes an optional hash: (s: string) => string closure. Anything that hashes a string to a string works.
import { compactHash } from 'compact-hash';
import { createHash } from 'node:crypto';
// Node's built-in sha256
const sha256 = (s: string) => createHash('sha256').update(s).digest('hex');
compactHash('hello world', sha256);
// → '936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af'
// xxhash-wasm (after `await xxhash()`):
const xxhasher = await xxhash();
compactHash('hello world', (s) => xxhasher.h64ToString(s));
// Your own keyed/scoped variant:
compactHash('hello world', (s) => myHmac(s, 'some-key'));Seeded MurmurHash3
import { compactHash, murmurHashWithSeed } from 'compact-hash';
compactHash('hello world', murmurHashWithSeed(42));API
compactHash(input: string, hash?: HashFn): string
Strips characters matched by JS \s (ASCII whitespace plus Unicode whitespace like NBSP, em-space, ideographic space), then calls hash on what remains.
hashdefaults tomurmurHash(MurmurHash3 32-bit, 8-char hex).- Returns whatever
hashreturns, unchanged.
type HashFn = (input: string) => string
murmurHash: HashFn
Default MurmurHash3 hasher (seed 0).
murmurHashWithSeed(seed: number): HashFn
MurmurHash3 hasher with a custom 32-bit seed.
murmur3_32(bytes: Uint8Array, seed?: number): number
murmur3_32Hex(bytes: Uint8Array, seed?: number): string
Raw byte-level API in case you want to hash arbitrary bytes without the whitespace-strip step.
Notes on the default algorithm
MurmurHash3 is not cryptographically secure — never use it for password hashing, signatures, or anywhere an adversary can craft collisions. It's designed for fast content fingerprinting, hash tables, and dedup. The 32-bit default reaches ~50% collision probability around 77k random inputs (birthday paradox over 2³²); fine for most fingerprint workloads. Swap in sha256 (or any other hash) via the closure parameter when you need stronger guarantees.
License
MIT
