hash
bun add @stopcock/hashimport { sha256, crc32, toHex, hmac } from '@stopcock/hash'
toHex(sha256('hello')) // "2cf24dba..."crc32('hello') // 907060870toHex(hmac(sha256, 'key', 'msg'))Real-world patterns
Section titled “Real-world patterns”Content-addressable cache key
Section titled “Content-addressable cache key”import { sha256, toHex } from '@stopcock/hash'
const cacheKey = (body: string) => toHex(sha256(body)).slice(0, 16)const key = cacheKey(JSON.stringify(requestBody))// "a3f2b7c9e1d04f8a" → use as Redis/cache keyHMAC signature verification
Section titled “HMAC signature verification”import { sha256, hmac, timeSafeEqual, fromHex } from '@stopcock/hash'
const verify = (payload: string, signature: string, secret: string) => timeSafeEqual(hmac(sha256, secret, payload), fromHex(signature))
// Verify a webhook from a third partyconst valid = verify(rawBody, req.headers['x-signature'], WEBHOOK_SECRET)Fast dedup with xxHash
Section titled “Fast dedup with xxHash”import { xxhash32 } from '@stopcock/hash'
const seen = new Set<number>()const deduped = items.filter(item => { const hash = xxhash32(JSON.stringify(item)) if (seen.has(hash)) return false seen.add(hash) return true})Hash functions
Section titled “Hash functions”sha256(input: string | Uint8Array): Uint8Arraymd5(input: string | Uint8Array): Uint8Arrayxxhash32(input: string | Uint8Array, seed?: number): numbercrc32(input: string | Uint8Array): numbersha256 and md5 return Uint8Array. Use toHex or toBase64 to format. xxhash32 and crc32 return numbers directly.
Utilities
Section titled “Utilities”toHex(data: Uint8Array): stringtoBase64(data: Uint8Array): stringfromHex(hex: string): Uint8Arrayhmac(hashFn: (data: Uint8Array) => Uint8Array, key: string | Uint8Array, message: string | Uint8Array): Uint8ArraytimeSafeEqual(a: Uint8Array, b: Uint8Array): boolean