Password.ts overview

Password Hashing Traits and Multi-Algorithm Support

Inspired by Rust’s password_hash crate, this module provides:

  • Trait-based API (PasswordHasher interface) for algorithm-agnostic hashing
  • PHC string format support (algorithm$params$salt$hash)
  • Multi-algorithm verification for seamless migration
  • Extensible design for custom algorithms

Exports Grouped by Category


utils

HashedPassword

Re-export of Types.HashedPassword for convenience.

Signature

declare const HashedPassword: Types.HashedPassword

Source

MultiAlgorithmVerifier (class)

Tag for MultiAlgorithmVerifier service

Multi-algorithm password verifier Supports verifying passwords against different hash formats, enabling seamless migration between algorithms

Signature

declare class MultiAlgorithmVerifier

Source

PasswordHasher (class)

Tag for PasswordHasher service

Core password hashing interface (trait) Implement this interface to support custom password hashing algorithms

Signature

declare class PasswordHasher

Source

multiAlgorithmVerifier

Create a multi-algorithm verifier that supports multiple password hashers This allows you to verify passwords that were hashed with different algorithms, which is useful during algorithm migration

Example

```typescript
import assert from "node:assert/strict"
import { Effect } from "effect"
import { Password } from "@thomasfosterau/effect-auth"
import { scryptHasher } from "@thomasfosterau/effect-auth/scrypt"

// During a migration, one verifier accepts hashes from both algorithms.
const verifier = Password.multiAlgorithmVerifier([Password.pbkdf2Hasher(), scryptHasher()])

const program = Effect.gen(function* () {
  // A hash written by the old algorithm still verifies.
  const legacy = yield* Password.pbkdf2Hasher().hash("correct horse battery")
  // Will automatically detect which algorithm was used.
  assert.equal(yield* verifier.verify("correct horse battery", legacy.hash), true)
  assert.equal(yield* verifier.verify("wrong password", legacy.hash), false)
})

await Effect.runPromise(program)

**Signature**

```ts
declare const multiAlgorithmVerifier: (hashers: readonly (typeof PasswordHasher.Service)[]) => typeof MultiAlgorithmVerifier.Service

Source

pbkdf2Hasher

PBKDF2 Password Hasher implementing the PasswordHasher trait. Following OWASP guidelines for secure password hashing.

Hashes are pbkdf2-sha256$iterations$salt$hash. verify reads the iteration count from the stored hash, so hashes made at any count still verify, up to the cap described below.

iterations defaults to 600,000, the OWASP figure for PBKDF2-HMAC-SHA256. Lower it only where the cost is not the point (such as in tests); the default is the production setting. On Cloudflare Workers the default costs about 75 ms of CPU per hash or verify, more than the Free plan’s 10 ms per request. It must be an integer from 1 to 2147483647. hash refuses any other value with a HashingError, so a hasher never writes a hash its own verify would refuse.

verify refuses, with a HashingError and before running PBKDF2, a stored count above max(5,000,000, the configured count), so a crafted stored hash cannot make it burn minutes of CPU. The configured count itself always verifies, even above 5,000,000. A non-finite or otherwise unusable iterations option does not raise the cap. A hash written at more than 5,000,000 iterations verifies only on a hasher configured at that count or higher, so lowering iterations below such a count makes those hashes unverifiable.

needsRehash returns true for a stored hash with fewer iterations than the configured count, so a hash made at a lower count is upgraded on the next successful sign-in, and for a hash verify would refuse.

The key is derived with effect/crypto’s Kdf (WebCrypto PBKDF2 by default, so it runs on Workers and in the browser) and the salt drawn from SecureRandom; options.kdf and options.random substitute either.

Signature

declare const pbkdf2Hasher: (options?: {
  readonly iterations?: number
  readonly kdf?: typeof Kdf.Kdf.Service
  readonly random?: typeof SecureRandom.SecureRandom.Service
}) => typeof PasswordHasher.Service

Source


This site uses Just the Docs, a documentation theme for Jekyll.