Password unstable
Password storage: hash passwords into self-describing PHC strings and verify them later.
The Password service hashes with one configured algorithm, Argon2id
or PBKDF2 from Kdf, and returns a PHC string such as
$argon2id$v=19$m=65536,t=3,p=4$<salt>$<hash> that carries the algorithm,
its costs, and the salt. Verification reads them back from the string, so
hashes made under an older policy keep verifying, and needsRehash tells
you when to replace them. decode and encode convert between
PHC strings and PasswordHash values without a service.
Gotchas
- Passwords are UTF-8 encoded without Unicode normalization, matching other PHC implementations. Normalize them yourself before hashing and verifying if users type them on different platforms.
verifyruns whatever costs the stored hash states. Stored hashes are trusted input: to verify hashes from an untrusted source, decode them and check the algorithm first.- Argon2id needs a
Kdflayer that provides it, such asNodeCrypto.layerKdf. Use PBKDF2 on WebCrypto-only runtimes.
Encoding
Parses a PHC string into a PasswordHash.
Details
Accepts $argon2id$v=19$m=<memory>,t=<passes>,p=<parallelism>$<salt>$<hash>
and $pbkdf2-<sha1|sha256|sha384|sha512>$i=<iterations>[,l=<length>]$<salt>$<hash>,
where l, when present, must equal the hash length. Salt and hash are
Base64 without padding, and numbers are safe integers in decimal without
leading zeros.
Anything else, including other Argon2 versions and variants, bcrypt, and
scrypt, fails with an InvalidArgument reason. Costs are range-checked
when deriving, not here.
Signature
declare function decode(phc: string): Effect<PasswordHash, CryptoError>Example Reading the costs of a stored hash
import { Effect } from "effect"import { Password } from "effect/crypto"
const stored = "$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$CTFhFdXPJO1aFaMaO6Mm5c8y7cJHAph8ArZWb2GRPPc"const decoded = Effect.runSync(Password.decode(stored))decoded.algorithm.name // => "argon2id"Password.encode(decoded) === stored // => trueFormats a PasswordHash as a PHC string.
Details
PBKDF2 hashes always include the l parameter.
Signature
declare function encode(hash: PasswordHash): stringLayers
Layer that provides Password, hashing new passwords with
algorithm.
Details
There is no default algorithm. Kdf.argon2id() and Kdf.pbkdf2Sha256
carry recommended costs; Argon2id fails with an Unsupported reason from
Kdf when the Kdf layer does not provide it.
Signature
declare function layer(options: { readonly algorithm: Algorithm;}): Layer<Password, never, SecureRandom | Kdf>Models
A password hashing algorithm: Argon2id or PBKDF2.
Signature
type Algorithm = Kdf.Argon2id | Kdf.Pbkdf2A password hash with the algorithm and salt that produced it.
Signature
interface PasswordHash { readonly algorithm: Algorithm; readonly hash: Uint8Array; readonly salt: Uint8Array;}Services
Service that hashes and verifies passwords as PHC strings.
Signature
declare class Password extends Shape<"effect/crypto/Password", { readonly hash: (password: string | Redacted<string>) => Effect<string, CryptoError>; readonly needsRehash: (hash: string) => Effect<boolean, CryptoError>; readonly verify: (password: string | Redacted<string>, hash: string) => Effect<boolean, CryptoError>;}, this> { constructor(_: never);}Example Hashing and verifying a password
import { Effect, Layer } from "effect"import { Digest, Kdf, Password, SecureRandom } from "effect/crypto"
const program = Effect.gen(function*() { const passwords = yield* Password.Password const stored = yield* passwords.hash("hunter2") return yield* passwords.verify("hunter2", stored)})
const layer = Password.layer({ algorithm: Kdf.pbkdf2({ hash: Digest.sha256, iterations: 1_000 }) }).pipe( Layer.provide([Kdf.layerWebCrypto, SecureRandom.layerWebCrypto]))const valid = await Effect.runPromise(program.pipe(Effect.provide(layer)))valid // => true