interface Hasher 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); // seededbyteSum 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 statesha256 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
*× 2Hashes one more chunk.
update(buffer: Buffer): this Parameters
bufferBuffer | Chunk to hash |
|---|
Returns
digestThe 64-bit hash of every chunk so far.
digest(): bigint resetStarts over with the seed the hasher was constructed with, returning this hasher.
reset(): this InterfaceHasherConstructorConstructs a Hasher.1
interface HasherConstructor HasherConstructor
new HasherConstructor(seed?: number | bigint): Hasher Parameters
seednumber | bigint | Optional 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
interface Sha256 digestThe 32-byte SHA-256 digest of every chunk so far.
digest(): Buffer hexdigestThe SHA-256 digest of every chunk so far, as 64 lowercase hexadecimal digits.
hexdigest(): string resetStarts over with no chunks hashed, returning this hasher.
reset(): this InterfaceSha256ConstructorConstructs a Sha256.1
interface Sha256Constructor Sha256Constructor
new Sha256Constructor(): Sha256