1
0
mirror of https://github.com/actix/examples synced 2025-02-03 01:49:05 +01:00

30 lines
902 B
Rust
Raw Normal View History

2019-03-29 13:43:03 -07:00
use crate::errors::ServiceError;
use argon2::{self, Config};
lazy_static::lazy_static! {
pub static ref SECRET_KEY: String = std::env::var("SECRET_KEY").unwrap_or_else(|_| "0123".repeat(8));
}
2021-10-07 03:04:59 +01:00
const SALT: &[u8] = b"supersecuresalt";
// WARNING THIS IS ONLY FOR DEMO PLEASE DO MORE RESEARCH FOR PRODUCTION USE
pub fn hash_password(password: &str) -> Result<String, ServiceError> {
let config = Config {
secret: SECRET_KEY.as_bytes(),
..Default::default()
};
2021-10-07 03:04:59 +01:00
argon2::hash_encoded(password.as_bytes(), SALT, &config).map_err(|err| {
dbg!(err);
ServiceError::InternalServerError
})
}
pub fn verify(hash: &str, password: &str) -> Result<bool, ServiceError> {
2022-02-18 02:44:02 +00:00
argon2::verify_encoded_ext(hash, password.as_bytes(), SECRET_KEY.as_bytes(), &[]).map_err(
|err| {
dbg!(err);
ServiceError::Unauthorized
2022-02-18 02:44:02 +00:00
},
)
}