auth::hashing

Function hash_password

Source
pub fn hash_password(
    password: &str,
    cfg: &PasswordHashConfig,
) -> Result<String, Error>
Expand description

Hashes a plain-text password using the Argon2id algorithm and the provided configuration.

A cryptographically secure random salt is generated automatically and included in the final hash. The output hash is encoded in the PHC string format and can be safely stored in a database for later verification.

§Arguments

  • password - The plain-text password to hash.
  • cfg - A reference to a PasswordHashConfig struct containing Argon2 parameters.

§Returns

A Result containing the encoded password hash on success, or a argon2::password_hash::Error on failure.

§Example

use auth::hashing::hash_password;
use auth::config::PasswordHashConfig;

let cfg = PasswordHashConfig {
    mem_cost: 65536,
    time_cost: 3,
    lanes: 4,
    hash_length: 32,
};

let password = "my_secure_password";
let hash = hash_password(password, &cfg).expect("Failed to hash password");

println!("Password hash: {}", hash);
assert!(hash.starts_with("$argon2id$"));