Skip to content

knotkit/hash

Computes deterministic non-cryptographic XXH64 hashes, identical across browsers, Node.js, and Bun

import {
function xxh64(seed?: bigint): ScalarHash<"xxh64", bigint, bigint>

Create an XXH64 factory with an unsigned 64-bit seed.

XXH64 is non-cryptographic and is unsuitable for passwords, signatures, MACs, secret-derived identifiers, or hostile-input collision resistance.

xxh64
} from 'knotkit/hash'
const
const hash: ScalarHash<"xxh64", bigint, bigint>
hash
=
function xxh64(seed?: bigint): ScalarHash<"xxh64", bigint, bigint>

Create an XXH64 factory with an unsigned 64-bit seed.

XXH64 is non-cryptographic and is unsuitable for passwords, signatures, MACs, secret-derived identifiers, or hostile-input collision resistance.

xxh64
()
const
const digest: bigint
digest
=
const hash: ScalarHash<"xxh64", bigint, bigint>
hash
.
ScalarHash<"xxh64", bigint, bigint>.string(input: string): bigint
string
('hello')
const digest: bigint
digest
// 0x26c7_827d_889f_6da3n
const hash: ScalarHash<"xxh64", bigint, bigint>
hash
.
ScalarHash<"xxh64", bigint, bigint>.encodeHex(output: bigint): string
encodeHex
(
const digest: bigint
digest
) // '26c7827d889f6da3'
// Uint8Array [0x26, 0xc7, 0x82, 0x7d, 0x88, 0x9f, 0x6d, 0xa3]
const hash: ScalarHash<"xxh64", bigint, bigint>
hash
.
ScalarHash<"xxh64", bigint, bigint>.encode(output: bigint): Uint8Array
encode
(
const digest: bigint
digest
)
hash.ts
const seeded = xxh64(0x0123_4567_89ab_cdefn)
seeded.bytes(Uint8Array.of(1, 2, 3))
seeded.uint32(42)
seeded.uint64(42n)
const state = seeded.create()
state.update(new TextEncoder().encode('hel'))
state.update(new TextEncoder().encode('lo'))
state.digest() // same as seeded.string('hello')
  • fingerprint bytes, text, or an unsigned integer the same way in every browser, Node.js, and Bun runtime
  • shard keys across buckets or deduplicate records by a cheap fingerprint
  • hash table keys without allocating or retaining a copy of the input
  • reproduce earlier digests in a later process from a persisted algorithm version and seed
  • hash a large payload chunk by chunk without holding it all in memory
{"v":1,"box":[58.9149,94.1597,1043,437.3555],"x":{"s":"linear","lim":[13.2221,48.7779],"inv":false,"g":0,"t":"Digest bits changed by one flipped input bit (out of 64)","tk":[[15,"15"],[24,"24"],[32,"32"],[40,"40"],[47,"47"]]},"y":{"s":"linear","lim":[0,452.485],"inv":false,"g":0,"tk":[[0,"0"],[100,"100"],[200,"200"],[300,"300"],[433,"433"]]},"ti":"4,096 one-bit flips: mean 32.0, SD 4.1 bits changed; a fair coin gives 32 and 4","a":[{"k":"bar","c":"#1d5fa6","s":"x","d":[15,1,16,0,17,0,18,0,19,2,20,6,21,5,22,29,23,34,24,59,25,78,26,153,27,184,28,235,29,280,30,358,31,372,32,433,33,388,34,363,35,314,36,254,37,206,38,122,39,91,40,57,41,34,42,22,43,7,44,4,45,3,46,1,47,1],"p":[108.12,436.6,135.8,437.36,163.48,437.36,191.15,437.36,218.83,435.84,246.51,432.8,274.19,433.56,301.86,415.36,329.54,411.57,357.22,392.61,384.89,378.19,412.57,321.31,440.25,297.8,467.93,259.12,495.6,224.98,523.28,165.82,550.96,155.21,578.63,108.94,606.31,143.07,633.99,162.03,661.67,199.2,689.34,244.7,717.02,281.11,744.7,344.82,772.38,368.33,800.05,394.12,827.73,411.57,855.41,420.67,883.08,432.05,910.76,434.32,938.44,435.08,966.12,436.6,993.79,436.6]},{"k":"line","c":"#5b6b7c","s":"x","d":[15,0.0354203333026,16,0.108474770739,17,0.306281705616,18,0.799735564665,19,1.9362018934,20,4.35645426015,21,9.12780892602,22,17.8407174463,23,32.5787014237,24,55.6552815988,25,89.0484505581,26,133.572675837,27,187.9911734,28,248.416907708,29,308.379609568,30,359.776211163,31,394.593263856,32,406.924303352,33,394.593263856,34,359.776211163,35,308.379609568,36,248.416907708,37,187.9911734,38,133.572675837,39,89.0484505581,40,55.6552815988,41,32.5787014237,42,17.8407174463,43,9.12780892602,44,4.35645426015,45,1.9362018934,46,0.799735564665,47,0.306281705616],"p":[108.12,437.33,135.8,437.27,163.48,437.12,191.15,436.75,218.83,435.89,246.51,434.05,274.19,430.43,301.86,423.82,329.54,412.65,357.22,395.14,384.89,369.82,412.57,336.04,440.25,294.77,467.93,248.94,495.6,203.46,523.28,164.48,550.96,138.07,578.63,128.72,606.31,138.07,633.99,164.48,661.67,203.46,689.34,248.94,717.02,294.77,744.7,336.04,772.38,369.82,800.05,395.14,827.73,412.65,855.41,423.82,883.08,430.43,910.76,434.05,938.44,435.89,966.12,436.75,993.79,437.12]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#fffff8"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 15 24 32 40 47 Digest bits changed by one flipped input bit (out of 64) 0 100 200 300 433 XXH64, seed 0 Binomial(64, 1/2) expectation Flips 4,096 one-bit flips: mean 32.0, SD 4.1 bits changed; a fair coin gives 32 and 4
{"v":1,"box":[58.9149,122.3921,595,437.3555],"x":{"s":"linear","lim":[13.2221,48.7779],"inv":false,"g":0,"t":"Digest bits changed by one flipped input bit (out of 64)","tk":[[15,"15"],[24,"24"],[32,"32"],[40,"40"],[47,"47"]]},"y":{"s":"linear","lim":[0,452.485],"inv":false,"g":0,"tk":[[0,"0"],[100,"100"],[200,"200"],[300,"300"],[433,"433"]]},"ti":"4,096 one-bit flips: mean 32.0, SD 4.1\nbits changed; a fair coin gives 32 and 4","a":[{"k":"bar","c":"#1d5fa6","s":"x","d":[15,1,16,0,17,0,18,0,19,2,20,6,21,5,22,29,23,34,24,59,25,78,26,153,27,184,28,235,29,280,30,358,31,372,32,433,33,388,34,363,35,314,36,254,37,206,38,122,39,91,40,57,41,34,42,22,43,7,44,4,45,3,46,1,47,1],"p":[85.72,436.66,100.8,437.36,115.88,437.36,130.95,437.36,146.03,435.96,161.11,433.18,176.18,433.88,191.26,417.17,206.34,413.69,221.42,396.29,236.49,383.06,251.57,330.86,266.65,309.28,281.73,273.78,296.8,242.45,311.88,188.16,326.96,178.42,342.03,135.96,357.11,167.28,372.19,184.68,387.27,218.79,402.34,260.55,417.42,293.96,432.5,352.43,447.58,374.01,462.65,397.68,477.73,413.69,492.81,422.04,507.88,432.48,522.96,434.57,538.04,435.27,553.12,436.66,568.19,436.66]},{"k":"line","c":"#5b6b7c","s":"x","d":[15,0.0354203333026,16,0.108474770739,17,0.306281705616,18,0.799735564665,19,1.9362018934,20,4.35645426015,21,9.12780892602,22,17.8407174463,23,32.5787014237,24,55.6552815988,25,89.0484505581,26,133.572675837,27,187.9911734,28,248.416907708,29,308.379609568,30,359.776211163,31,394.593263856,32,406.924303352,33,394.593263856,34,359.776211163,35,308.379609568,36,248.416907708,37,187.9911734,38,133.572675837,39,89.0484505581,40,55.6552815988,41,32.5787014237,42,17.8407174463,43,9.12780892602,44,4.35645426015,45,1.9362018934,46,0.799735564665,47,0.306281705616],"p":[85.72,437.33,100.8,437.28,115.88,437.14,130.95,436.8,146.03,436.01,161.11,434.32,176.18,431,191.26,424.94,206.34,414.68,221.42,398.62,236.49,375.37,251.57,344.38,266.65,306.5,281.73,264.44,296.8,222.7,311.88,186.92,326.96,162.69,342.03,154.11,357.11,162.69,372.19,186.92,387.27,222.7,402.34,264.44,417.42,306.5,432.5,344.38,447.58,375.37,462.65,398.62,477.73,414.68,492.81,424.94,507.88,431,522.96,434.32,538.04,436.01,553.12,436.8,568.19,437.14]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#fffff8"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 15 24 32 40 47 Digest bits changed by one flipped input bit (out of 64) 0 100 200 300 433 XXH64, seed 0 Binomial(64, 1/2) expectation Flips 4,096 one-bit flips: mean 32.0, SD 4.1 bits changed; a fair coin gives 32 and 4
  • XXH64 of "Knotkit!" at seed 0: c5398de0ce53a5f6.
  • Flipping each input bit in turn changed between 26 and 41 digest bits.
  • Across 4,096 one-bit flips of 64 inputs: mean 32.0 changed bits, SD 4.1. A fair coin per bit gives 32 and 4.
  • Use this deterministic fingerprint to identify bytes. Use a cryptographic hash for adversarial inputs.

Hover or focus a chart for exact values. Each change reruns demo.ts against knotkit/hash in your browser; the defaults reproduce the README figure.

ExportReturnsDoes
xxh64ScalarHash<'xxh64', bigint, bigint>Frozen one-shot XXH64 value over bytes, text, `uint32`, and `uint64`, plus `create()` for an independent incremental byte state; the seed defaults to `0n`.
hashFromIdentityScalarHash<'xxh64', bigint, bigint>Restore the current implementation matching a validated hash identity, so a persisted version and seed reproduce the same digests.

Cost per member. Open a row for every operation.

xxh64 14 members
MemberCost
kindO(1)
versionO(1)
seedO(1)
bytesO(n)
stringO(n)
uint32O(1)
uint64O(1)
bytesIntoO(n)
stringIntoO(n)
uint32IntoO(1)
uint64IntoO(1)
createO(1)
encodeO(1)
encodeHexO(1)

Every error this module throws is HashInvalidInputError or HashParseError, with a code from a closed, exported list. Branch on the code; the message is for people.

HashInvalidInputError
INVALID_SEED
HashParseError
INVALID_BYTES INVALID_STRING INVALID_UINT32 INVALID_UINT64 INVALID_CHUNK INVALID_OUTPUT INVALID_DIGEST INPUT_TOO_LONG STATE_FINALIZED INVALID_SHAPE UNKNOWN_KIND UNSUPPORTED_VERSION

Serialization lives in a separate entrypoint, knotkit/hash/wire, so code that never saves a structure never loads a parser.

Functions
serializeHashIdentity, deserializeHashIdentity
Formats
json
Compatibility
identity-versioned; deserializeHashIdentity rejects an identity whose version is not HASH_IDENTITY_VERSION, which changes only when the digest mapping changes, never with a release, envelope v
If you need to…Use instead
hash a password, sign a message, or authenticate data with a secretcrypto.subtle from Web Crypto
resist collisions from an adversary who chooses the inputSHA-256 through crypto.subtle in Web Crypto
count distinct values, or estimate their frequencies, in bounded memoryknotkit/scalar-sketch
checksum a large artifact at maximum throughputthe xxhash-wasm package, which runs the same algorithm in WebAssembly
persist a whole data structure and re-read it laterthe structure package's serialize function

Measured by the suite in packages/hash/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.

Prompt · knotkit/hash
Use knotkit/hash for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/hash'.

Exports:
- `xxh64` → `ScalarHash<'xxh64', bigint, bigint>`: Frozen one-shot XXH64 value over bytes, text, `uint32`, and `uint64`, plus `create()` for an independent incremental byte state; the seed defaults to `0n`.
- `hashFromIdentity` → `ScalarHash<'xxh64', bigint, bigint>`: Restore the current implementation matching a validated hash identity, so a persisted version and seed reproduce the same digests.

Use something else when:
- hash a password, sign a message, or authenticate data with a secret → crypto.subtle from Web Crypto
- resist collisions from an adversary who chooses the input → SHA-256 through crypto.subtle in Web Crypto
- count distinct values, or estimate their frequencies, in bounded memory → knotkit/scalar-sketch
- checksum a large artifact at maximum throughput → the xxhash-wasm package, which runs the same algorithm in WebAssembly
- persist a whole data structure and re-read it later → the structure package's serialize function

Errors are HashInvalidInputError or HashParseError with a `code` property. Branch on `error.code`, never on the message. Save and load through `knotkit/hash/wire` (serializeHashIdentity, deserializeHashIdentity).

Full page: https://knotkit.jkomyno.dev/modules/hash.md
Types: node_modules/knotkit/hash/ and https://knotkit.jkomyno.dev/reference/knotkit/hash/