Common.ts overview
Shared Argon2id vocabulary: the PHC algorithm identifier, the cost-parameter interface, the recommended cost profiles, the verify ceiling, and the input guards the hasher applies before running a KDF.
This module intentionally depends on nothing but effect and @thomasfosterau/effect-auth.
Exports Grouped by Category
- utils
- ARGON2ID
- Argon2Options (interface)
- Argon2Profile (interface)
- Argon2VerifyLimits (interface)
- HASH_LENGTH
- MAX_PASSWORD_BYTES
- SALT_LENGTH
- defaultProfile
- defaultVerifyLimits
- isWithinMaxLength
- requireHashablePassword
- requireNonEmptyHash
- requireNonEmptyPassword
- resolveOptions
- resolveVerifyLimits
- serverProfile
- workerdVerifyLimits
utils
ARGON2ID
PHC/parseAlgorithm identifier for the hashes this package produces.
Signature
declare const ARGON2ID: "argon2id"
Argon2Options (interface)
Argon2id cost parameters, plus the verify ceiling. The cost names match @node-rs/argon2 and previous releases of this package, so a configuration object written for either still works unchanged; the ceiling’s match @thomasfosterau/argon2’s verify options.
Unset cost fields fall back to defaultProfile, unset limits to defaultVerifyLimits. The hasher throws an Argon2ConfigError when it is built with a cost or limit that is not a positive integer, or with costs over its own ceiling, since it would write hashes it then refuses to verify.
Signature
export interface Argon2Options {
/** Memory cost in KiB. Default `19456` (19 MiB). */
readonly memoryCost?: number
/** Time cost (iterations). Default `2`. */
readonly timeCost?: number
/** Degree of parallelism (lanes). Default `1`. */
readonly parallelism?: number
/**
* Refuse to verify a stored hash whose `m` exceeds this many KiB. Default
* `131072` (128 MiB); on Cloudflare Workers `65536` (64 MiB, half the
* isolate), or `memoryCost` when that is higher.
*/
readonly maxMemoryKiB?: number
/** Refuse to verify a stored hash whose `t` exceeds this. Default `10`. */
readonly maxIterations?: number
/** Refuse to verify a stored hash whose `p` exceeds this. Default `8`. */
readonly maxParallelism?: number
/** Where salts are drawn from. Default the core's `PlatformCrypto.instance` (Web Crypto). */
readonly random?: typeof SecureRandom.SecureRandom.Service
}
Argon2Profile (interface)
A fully-resolved set of Argon2id cost parameters — an Argon2Options with no holes.
Signature
export interface Argon2Profile {
/** Memory cost in KiB. */
readonly memoryCost: number
/** Time cost (iterations). */
readonly timeCost: number
/** Degree of parallelism (lanes). */
readonly parallelism: number
}
Argon2VerifyLimits (interface)
The verify ceiling: the largest costs verify will run for a stored hash. Names and defaults match @thomasfosterau/argon2’s verify(password, hash, { maxMemoryKiB, maxIterations, maxParallelism }).
Signature
export interface Argon2VerifyLimits {
/** Largest stored `m`, in KiB. Default `131072` (128 MiB). */
readonly maxMemoryKiB: number
/** Largest stored `t`. Default `10`. */
readonly maxIterations: number
/** Largest stored `p`. Default `8`. */
readonly maxParallelism: number
}
HASH_LENGTH
Derived key length in bytes. Matches @node-rs/argon2’s default.
Signature
declare const HASH_LENGTH: 32
MAX_PASSWORD_BYTES
Anti-DoS ceiling: reject inputs longer than this before running any KDF. Same value core effect-auth uses; legitimate passwords are far below it.
Signature
declare const MAX_PASSWORD_BYTES: 1024
SALT_LENGTH
Salt length in bytes. Matches @node-rs/argon2’s default.
Signature
declare const SALT_LENGTH: 16
defaultProfile
The default profile: OWASP’s low-memory Argon2id recommendation (m = 19 MiB, t = 2, p = 1).
Chosen so the defaults are safe everywhere the package can run, including Cloudflare Workers (128 MiB isolate limit) and other constrained serverless runtimes. On a plain Node server prefer serverProfile.
Signature
declare const defaultProfile: Argon2Profile
defaultVerifyLimits
The default verify ceiling (m ≤ 128 MiB, t ≤ 10, p ≤ 8). It sits above serverProfile, so every hash this package writes with either profile verifies, and it keeps a corrupt or attacker-written stored hash from demanding gigabytes of memory or billions of iterations, which on Workers would exceed the isolate’s 128 MiB and kill it for every request it serves.
Signature
declare const defaultVerifyLimits: Argon2VerifyLimits
isWithinMaxLength
True when password is short enough to be worth running the KDF on.
Signature
declare const isWithinMaxLength: (password: string) => boolean
requireHashablePassword
Fails unless password is a non-empty string within MAX_PASSWORD_BYTES.
Signature
declare const requireHashablePassword: (password: string) => Effect.Effect<void, HashingError>
requireNonEmptyHash
Fails unless hash is a non-empty string.
Signature
declare const requireNonEmptyHash: (hash: string) => Effect.Effect<void, HashingError>
requireNonEmptyPassword
Fails unless password is a non-empty string.
Signature
declare const requireNonEmptyPassword: (password: string) => Effect.Effect<void, HashingError>
resolveOptions
Fill in any unset Argon2Options field from defaultProfile.
Signature
declare const resolveOptions: (options?: Argon2Options) => Argon2Profile
resolveVerifyLimits
Fill in any unset verify limit from defaultVerifyLimits, or on Cloudflare Workers from workerdVerifyLimits, whose maxMemoryKiB is raised to a configured memoryCost above it.
Signature
declare const resolveVerifyLimits: (options?: Argon2Options) => Argon2VerifyLimits
serverProfile
A stronger profile for plain-Node deployments with memory to spare (m = 64 MiB, t = 3, p = 1). Roughly 5× the work of defaultProfile; measure it against your own latency budget before adopting it, and do not use it on Cloudflare Workers.
Example
import { argon2HasherLayer } from "@thomasfosterau/effect-auth-argon2/Argon2"
import { serverProfile } from "@thomasfosterau/effect-auth-argon2/Common"
const layer = argon2HasherLayer(serverProfile)
Signature
declare const serverProfile: Argon2Profile
workerdVerifyLimits
The verify ceiling on Cloudflare Workers (m ≤ 64 MiB, t ≤ 10, p ≤ 8). A Workers isolate has 128 MiB in all, so a verify at the general default’s 128 MiB would exhaust it; 64 MiB still covers serverProfile and leaves the isolate half its memory. A configured memoryCost above it raises it to that cost, so the hasher’s own hashes always verify.
Signature
declare const workerdVerifyLimits: Argon2VerifyLimits