hash: hash crate for all hashing needs, password

This commit is contained in:
Philippe Loctaux 2023-03-05 23:28:14 +01:00
parent 7851fdae1e
commit 8af226cd05
5 changed files with 128 additions and 0 deletions

9
crates/hash/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "hash"
version = "0.0.0"
edition = "2021"
[dependencies]
argon2 = { version = "0.4.1" }
rand_core = { version = "0.6.4", features = ["std"] }
thiserror = { workspace = true }

11
crates/hash/src/error.rs Normal file
View file

@ -0,0 +1,11 @@
// error
#[derive(thiserror::Error)]
// the rest
#[derive(Debug)]
pub enum Error {
#[error("Failed to hash value")]
Hash,
#[error("Failed to parse hashed value")]
Parse,
}

82
crates/hash/src/hash.rs Normal file
View file

@ -0,0 +1,82 @@
use crate::error::Error;
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
pub(crate) fn hash(value: &str) -> Result<String, Error> {
let salt = SaltString::generate(&mut OsRng);
// Argon2 with default params (Argon2id v19)
let argon2 = Argon2::default();
// Hash value to PHC string ($argon2id$v=19$...)
argon2
.hash_password(value.as_bytes(), &salt)
.map(|hashed| hashed.to_string())
.map_err(|_| Error::Hash)
}
fn verify(value: &str, hashed_value: &str) -> Result<bool, Error> {
// Verify value against PHC string.
//
// NOTE: hash params from `parsed_hash` are used instead of what is configured in the
// `Argon2` instance.
let parsed_hash = PasswordHash::new(hashed_value).map_err(|_| Error::Parse)?;
let hashes_match = Argon2::default()
.verify_password(value.as_bytes(), &parsed_hash)
.is_ok();
Ok(hashes_match)
}
#[derive(Debug)]
pub struct Hash {
plain: Option<String>,
hash: String,
}
impl Hash {
pub fn from_plain(plain: impl Into<String>) -> Result<Self, Error> {
let plain = plain.into();
let hash = hash(&plain)?;
Ok(Self {
plain: Some(plain),
hash,
})
}
pub fn from_hash(hash: impl Into<String>) -> Self {
Self {
plain: None,
hash: hash.into(),
}
}
pub fn plain(&self) -> Option<&str> {
self.plain.as_deref()
}
pub fn hash(&self) -> &str {
self.hash.as_ref()
}
pub fn compare(&self, plain: &str) -> Result<bool, Error> {
verify(plain, self.hash())
}
}
#[cfg(test)]
mod tests {
use crate::hash::Hash;
#[test]
fn can_be_compared_with_hashed() {
let value = Hash::from_plain("Testing the hashing").unwrap();
// Create from hashed value
let value_from_hash = Hash::from_hash(value.hash());
// Make sure when hashing the plain text version, it can match the hashed one
let does_value_match = value_from_hash.compare(value.plain().unwrap()).unwrap();
assert!(does_value_match);
}
}

6
crates/hash/src/lib.rs Normal file
View file

@ -0,0 +1,6 @@
mod error;
mod hash;
mod password;
pub use error::Error;
pub use password::Password;

View file

@ -0,0 +1,20 @@
use crate::error::Error;
use crate::hash::{hash, Hash};
#[derive(Debug)]
pub struct Password(Hash);
impl Password {
pub fn new(plain: &str) -> Result<Self, Error> {
Ok(Self(Hash::from_hash(hash(plain)?)))
}
pub fn from_hash(hash: impl Into<String>) -> Self {
Self(Hash::from_hash(hash))
}
pub fn hash(&self) -> &str {
self.0.hash()
}
pub fn compare(&self, plain: &str) -> Result<bool, Error> {
self.0.compare(plain).map_err(Error::from)
}
}