From 090958451244c54fa0abe5791a12bedc26674c41 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Fri, 19 Apr 2019 19:36:59 +0700 Subject: Add support for sha-512 --- src/args.rs | 1 + src/auth.rs | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'src') 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 { 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::(basic_auth.password, password_hash), + RequiredAuthPassword::Sha512(password_hash) => compare_hash::(basic_auth.password, password_hash), } } +pub fn compare_hash(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 for Auth { fn response( &self, -- cgit v1.2.3