auth::hashing

Function verify_password

Source
pub fn verify_password(hash: &str, password: &str) -> Result<bool, Error>
Expand description

Verifies that a plain-text password matches a previously generated Argon2 hash.

The hash must be in PHC string format, which includes information about the algorithm, salt, and parameters used. This is the format produced by hash_password.

§Arguments

  • hash - A PHC-encoded Argon2 hash string (e.g., from your database).
  • password - The plain-text password to verify.

§Returns

A Result containing true if the password matches the hash, or false if it does not. Returns an error if the hash cannot be parsed or if verification fails unexpectedly.

§Example

use auth::hashing::{hash_password, verify_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("Hashing failed");

let is_valid = verify_password(&hash, password).expect("Verification failed");
assert!(is_valid);

let wrong = "wrong_password";
let is_valid = verify_password(&hash, wrong).expect("Verification failed");
assert!(!is_valid);