diff options
author | khai96_ <hvksmr1996@gmail.com> | 2019-04-19 12:36:59 +0000 |
---|---|---|
committer | khai96_ <hvksmr1996@gmail.com> | 2019-04-19 12:36:59 +0000 |
commit | 090958451244c54fa0abe5791a12bedc26674c41 (patch) | |
tree | 72ad1a37f0b4a11852c1642cc2385a18dd301d21 | |
parent | Add support for hashed password (sha256) (diff) | |
download | miniserve-090958451244c54fa0abe5791a12bedc26674c41.tar.gz miniserve-090958451244c54fa0abe5791a12bedc26674c41.zip |
Add support for sha-512
-rw-r--r-- | src/args.rs | 1 | ||||
-rw-r--r-- | src/auth.rs | 18 |
2 files changed, 12 insertions, 7 deletions
diff --git a/src/args.rs b/src/args.rs index 404225a..9c96fd7 100644 --- a/src/args.rs +++ b/src/args.rs @@ -94,6 +94,7 @@ fn parse_auth(src: &str) -> Result<auth::RequiredAuth, String> { let password = match split.next() { Some(hash) => match second_part { "sha256" => auth::RequiredAuthPassword::Sha256(hash.to_owned()), + "sha512" => auth::RequiredAuthPassword::Sha512(hash.to_owned()), _ => return Err("Invalid hash method, valid methods is sha256".to_owned()) }, None => { diff --git a/src/auth.rs b/src/auth.rs index 6aed8cf..94a2fda 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,7 +1,7 @@ use actix_web::http::header; use actix_web::middleware::{Middleware, Response}; use actix_web::{HttpRequest, HttpResponse, Result}; -use sha2::{Sha256, Digest}; +use sha2::{Sha256, Sha512, Digest}; pub struct Auth; @@ -21,6 +21,7 @@ pub struct BasicAuthParams { pub enum RequiredAuthPassword { Plain(String), Sha256(String), + Sha512(String), } #[derive(Clone, Debug)] @@ -55,15 +56,18 @@ pub fn match_auth(basic_auth: BasicAuthParams, required_auth: &RequiredAuth) -> match &required_auth.password { RequiredAuthPassword::Plain(ref required_password) => basic_auth.password == *required_password, - RequiredAuthPassword::Sha256(password_hash) => { - let mut hasher = Sha256::new(); - hasher.input(basic_auth.password); - let received_hash = hex::encode(hasher.result()); - received_hash == *password_hash - } + RequiredAuthPassword::Sha256(password_hash) => compare_hash::<Sha256>(basic_auth.password, password_hash), + RequiredAuthPassword::Sha512(password_hash) => compare_hash::<Sha512>(basic_auth.password, password_hash), } } +pub fn compare_hash<T: Digest>(password: String, hash: &String) -> bool { + let mut hasher = T::new(); + hasher.input(password); + let received_hash = hex::encode(hasher.result()); + received_hash == *hash +} + impl Middleware<crate::MiniserveConfig> for Auth { fn response( &self, |