# @codenhub/crypto

> CryptoKit: Argon2id (RFC 9106, version 0x13) password hashing that gives the same bytes on Node.js 26 or newer, Bun, Deno, Cloudflare Workers and browsers, so a hash made on one runtime is checked on any other (barring rare NFKC differences between Unicode versions of runtimes of very different ages; see docs/runtimes.md). No runtime dependencies. Password hashing is all 0.1 does: no encryption, general hashing, bcrypt, scrypt or PBKDF2. The package is experimental.

`createPasswordHasher({ memory, passes, parallelism, limits })` returns a frozen `{ hash(password), verify(password, stored) }`. Defaults are OWASP's first configuration: `memory` 19456 KiB, `passes` 2, `parallelism` 1. `memory` and `passes` must both meet one of OWASP's configurations (47104/1, 19456/2, 12288/3, 9216/4, 7168/5) or creation throws `TypeError`; so do unknown options, non-integers, under 8 KiB per lane, and a cost above `limits`. `limits` defaults: `maxMemory` 65536 KiB, `maxPasses` 10, `maxParallelism` 16, `maxPasswordBytes` 1024.

`hash(password)` resolves to `{ ok: true, value: "$argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>" }` (16-byte random salt, 32-byte key, standard base64 without padding) or fails with `password_too_long`. `verify(password, stored)` resolves to `{ ok: true, value: { isMatch, needsRehash } }`, or fails with `malformed_hash`, `unsupported_hash` (bcrypt, Argon2i/d, `v=16`, key under 16 bytes) or `hash_over_limit`, each issue `{ code, message, params }` with `params.check` naming the failed check; issues never contain the password, salt or key. A wrong password is `isMatch: false`, not a failure; so is a password over `maxPasswordBytes`. `needsRehash` is true only on a match when the stored memory or passes is below the hasher's, or its salt is under 16 bytes or key under 32; then call `hash` and store the new string. Passwords must be strings (else `TypeError`) and are normalized to NFKC before hashing and checking; the byte limit counts UTF-8 after normalization.

Export conditions pick the implementation: `node` uses `node:crypto`'s native Argon2id when it passes a known-answer check (Node.js 26) and the embedded WebAssembly core otherwise (Bun, Deno); `workerd` imports `argon2.wasm` as a module (Wrangler uploads it); the default compiles the core from embedded base64 (browsers; CSP needs `'wasm-unsafe-eval'`). Outside Node.js the core runs on the calling thread, about 0.1 s per default hash; the Workers Free plan's 10 ms CPU limit is too small.

## Documentation

- [Documentation overview](docs/index.md): What a password hash is, the PHC string, a first hash and check, the result shape, and the reading path.
- [Hashing passwords](docs/hashing.md): `hash`, the cost options and OWASP's floor, lanes, changing the cost, NFKC normalization, and the password length bound.
- [Verifying passwords](docs/verifying.md): `verify`, its three answers, and replacing a weak hash when `needsRehash` is true.
- [Limits](docs/limits.md): The bounds checked before any work, their defaults, the fixed salt and key bounds, and raising them with the cost.
- [Issues](docs/errors.md): The failed result, every issue code with its `params.check` values, and what throws `TypeError`.
- [Runtimes](docs/runtimes.md): Which implementation each runtime gets, Cloudflare Workers setup and CPU and memory limits, browsers, the event loop, memory wiping, and Unicode versions.

## Optional

- [API reference](docs/reference/index.md): Every export with its signature.
