JavaScript · Hashing and Checksums

Hashing and Checksums

import { Hasher } from 'stringzilla';import { HasherConstructor } from 'stringzilla';import { Sha256 } from 'stringzilla';import { Sha256Constructor } from 'stringzilla'; 6.0.0-rc.1 · 91a27bdd

hash computes StringZilla’s fast 64-bit hash of a buffer, returned as a BigInt. An optional second argument seeds the hash and accepts a BigInt or a number, defaulting to 0. A seed that is not a non-negative integer within 64 bits throws a RangeError, here and in the Hasher constructor.

sz.hash(Buffer.from("hello"));        // => 64-bit BigInt
sz.hash(Buffer.from("hello"), 42n);   // seeded

byteSum returns the arithmetic sum of all byte values in a buffer as a BigInt, a cheap checksum.

assert.strictEqual(sz.byteSum(Buffer.from([1, 2, 3])), 6n);

The Hasher class computes the same fast hash incrementally over chunks. Construct it with an optional seed, feed data with update, read the running result with digest, and rewind to the initial seed with reset. Both update and reset return the hasher, so calls can be chained.

const h = new sz.Hasher(42n);
h.update(Buffer.from("hel")).update(Buffer.from("lo"));
assert.strictEqual(h.digest(), sz.hash(Buffer.from("hello"), 42n)); // 64-bit BigInt
h.reset(); // back to the seed state

sha256 computes the SHA-256 digest of a buffer in one call, returning a 32-byte Buffer.

sz.sha256(Buffer.from("hello")).toString("hex");

The Sha256 class streams the same digest over chunks. Use update to add data, then digest for a 32-byte Buffer, hexdigest for the 64-character lowercase hex string, or reset to start over.

const s = new sz.Sha256();
s.update(Buffer.from("hel")).update(Buffer.from("lo"));
assert.strictEqual(s.hexdigest(), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");

API

InterfaceHasherStateful hasher for streaming hash computation, for data that arrives in chunks.3
stringzilla.d.ts:53 ↗
interface Hasher
*× 2Hashes one more chunk.
stringzilla.d.ts:60 ↗
update(buffer: Buffer): this

Parameters

bufferBufferChunk to hash

Returns

This hasher, for chaining
digestThe 64-bit hash of every chunk so far.
stringzilla.d.ts:63 ↗
digest(): bigint
resetStarts over with the seed the hasher was constructed with, returning this hasher.
stringzilla.d.ts:66 ↗
reset(): this
InterfaceHasherConstructorConstructs a Hasher.1
stringzilla.d.ts:70 ↗
interface HasherConstructor
HasherConstructor
stringzilla.d.ts:72 ↗
new HasherConstructor(seed?: number | bigint): Hasher

Parameters

seednumber | bigintOptional seed, a non-negative integer that fits in 64 bits, defaulting to 0
InterfaceSha256Stateful SHA-256 hasher for streaming hash computation, for data that arrives in chunks.3
stringzilla.d.ts:76 ↗
interface Sha256
digestThe 32-byte SHA-256 digest of every chunk so far.
stringzilla.d.ts:86 ↗
digest(): Buffer
hexdigestThe SHA-256 digest of every chunk so far, as 64 lowercase hexadecimal digits.
stringzilla.d.ts:89 ↗
hexdigest(): string
resetStarts over with no chunks hashed, returning this hasher.
stringzilla.d.ts:92 ↗
reset(): this
InterfaceSha256ConstructorConstructs a Sha256.1
stringzilla.d.ts:96 ↗
interface Sha256Constructor
Sha256Constructor
stringzilla.d.ts:97 ↗
new Sha256Constructor(): Sha256