Module std.crypto.password
Password hashing and verification (Argon2id).
Hash passwords for storage and verify them later. Uses Argon2id with secure default parameters.
Examples
import std.crypto.password;
fn main() {
let hash = password.hash("my_secret").expect("password hashing succeeds");
match password.verify("my_secret", hash) {
.Ok(true) => println("password matches"),
.Ok(false) => println("password does not match"),
.Err(error) => println(f"verification failed: {error}"),
}
}
Contents
Functions
Function hash
pub fn hash(password: string) -> string fails PasswordError
Hash a password using Argon2id with default parameters.
Returns the PHC-format hash string, suitable for storage.
Examples
import std.crypto.password;
fn main() {
let hash = password.hash("hunter2").expect("password hashing succeeds");
}
Function verify
pub fn verify(password: string, hash: string) -> bool fails PasswordError
Verify a password while distinguishing malformed hashes from mismatches.
Function hash_custom
pub fn hash_custom(password: string, cost: i64) -> string fails PasswordError
Hash with a custom cost while rejecting values that cannot cross the ABI.
Types
Enum PasswordError
A password operation that could not run.
Variants
MalformedHashThe stored hash is not an Argon2id hash this module can read.
InvalidCost(i64)The cost, outside
1..=2147483647.HashFailedThe native hasher failed.