argon2_async/
verifier.rs

1use crate::Result;
2use argon2::password_hash::Error;
3use password_hash::PasswordVerifier;
4use std::convert::identity;
5
6/// Verify a password with the given hash.
7///
8/// # Returns
9/// Returns a `bool`, true if the password is valid, false if not valid.
10///
11/// # Errors
12/// This function errors if any of the following happen:
13/// * the given config is invalid
14/// * verifying the password fails
15/// * communication between threads fails
16pub async fn verify<'a>(password: String, hash: String) -> Result<bool> {
17    let hasher = crate::get_hasher().await?;
18    let res = tokio::task::spawn_blocking(move || {
19        password_hash::PasswordHash::try_from(hash.as_ref())
20            .map(|hash| hasher.verify_password(password.as_bytes(), &hash))
21            .and_then(identity)
22    })
23    .await?;
24    match res {
25        Ok(()) => Ok(true),
26        Err(Error::Password) => Ok(false),
27        Err(e) => Err(e.into()),
28    }
29}