/** * Password storage: hash passwords into self-describing PHC strings and verify * them later. * * The {@link 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$$` 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. {@link decode} and {@link encode} convert between * PHC strings and {@link 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. * - `verify` runs whatever costs the stored hash states. Stored hashes are * trusted input: to verify hashes from an untrusted source, {@link decode} * them and check the algorithm first. * - Argon2id needs a `Kdf` layer that provides it, such as * `NodeCrypto.layerKdf`. Use PBKDF2 on WebCrypto-only runtimes. * * @stability unstable * @since 4.1.5 */ import * as Context from "../Context.ts"; import * as Effect from "../Effect.ts"; import * as Layer from "../Layer.ts"; import * as Redacted from "../Redacted.ts"; import * as CryptoError from "./CryptoError.ts"; import * as Kdf from "./Kdf.ts"; import * as SecureRandom from "./SecureRandom.ts"; /** * A password hashing algorithm: Argon2id or PBKDF2. * * @stability unstable * @category models * @since 4.1.5 */ export type Algorithm = Kdf.Argon2id | Kdf.Pbkdf2; /** * A password hash with the algorithm and salt that produced it. * * @stability unstable * @category models * @since 4.1.5 */ export interface PasswordHash { readonly algorithm: Algorithm; readonly salt: Uint8Array; readonly hash: Uint8Array; } /** * Parses a PHC string into a {@link PasswordHash}. * * **Details** * * Accepts `$argon2id$v=19$m=,t=,p=$$` * and `$pbkdf2-$i=[,l=]$$`, * 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. * * **Example** (Reading the costs of a stored hash) * * ```ts import.meta.vitest * 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 // => true * ``` * * @stability unstable * @category encoding * @since 4.1.5 */ export declare const decode: (phc: string) => Effect.Effect; /** * Formats a {@link PasswordHash} as a PHC string. * * **Details** * * PBKDF2 hashes always include the `l` parameter. * * @stability unstable * @category encoding * @since 4.1.5 */ export declare const encode: (hash: PasswordHash) => string; declare const Password_base: Context.ServiceClass Effect.Effect; /** * Checks a password against a stored PHC string, using the algorithm and * salt it records. A wrong password is `false`; a malformed stored hash * fails with an `InvalidArgument` reason. */ readonly verify: (password: string | Redacted.Redacted, hash: string) => Effect.Effect; /** * Whether a stored PHC string differs from what `hash` would produce now: * another algorithm or cost, or a salt or output length other than 16 and * 32 bytes. Rehash the password after a successful `verify` when it is. */ readonly needsRehash: (hash: string) => Effect.Effect; }>; /** * Service that hashes and verifies passwords as PHC strings. * * **Example** (Hashing and verifying a password) * * ```ts import.meta.vitest * 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 * ``` * * @stability unstable * @category services * @since 4.1.5 */ export declare class Password extends Password_base { } /** * Layer that provides {@link 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. * * @stability unstable * @category layers * @since 4.1.5 */ export declare const layer: (options: { readonly algorithm: Algorithm; }) => Layer.Layer; export {}; //# sourceMappingURL=Password.d.ts.map