auth_helper/
password.rs

1// Copyright 2020-2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4//! A module that provides password utilities.
5
6use argon2::{self, Config};
7use rand::Rng;
8
9/// Error occurring when hashing/verifying passwords.
10pub type Error = argon2::Error;
11
12/// Generates a salt to be used for password hashing.
13pub fn generate_salt() -> [u8; 32] {
14    rand::rngs::OsRng::default().gen()
15}
16
17/// Hashes a password together with a salt.
18pub fn password_hash(password: &[u8], salt: &[u8]) -> Result<Vec<u8>, Error> {
19    argon2::hash_raw(password, salt, &Config::default())
20}
21
22/// Verifies if a password/salt pair matches a password hash.
23pub fn password_verify(password: &[u8], salt: &[u8], hash: &[u8]) -> Result<bool, Error> {
24    Ok(hash == password_hash(password, salt)?)
25}