|
|
|
@ -1,9 +1,12 @@ |
|
|
|
|
use crate::{Error, Error::*, Result}; |
|
|
|
|
use crypto::{digest::Digest, md5::Md5, sha2::Sha256, sha2::Sha512Trunc256}; |
|
|
|
|
use std::fmt; |
|
|
|
|
use std::fmt::{Display, Formatter}; |
|
|
|
|
use std::str::FromStr; |
|
|
|
|
|
|
|
|
|
use digest::{Digest, DynDigest}; |
|
|
|
|
use md5::Md5; |
|
|
|
|
use sha2::{Sha256, Sha512Trunc256}; |
|
|
|
|
|
|
|
|
|
/// Algorithm type
|
|
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)] |
|
|
|
|
#[allow(non_camel_case_types)] |
|
|
|
@ -27,19 +30,19 @@ impl Algorithm { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Calculate a hash of bytes using the selected algorithm
|
|
|
|
|
pub fn hash(&self, bytes: &[u8]) -> String { |
|
|
|
|
let mut hash: Box<dyn Digest> = match self.algo { |
|
|
|
|
pub fn hash(self, bytes: &[u8]) -> String { |
|
|
|
|
let mut hash: Box<dyn DynDigest> = match self.algo { |
|
|
|
|
AlgorithmType::MD5 => Box::new(Md5::new()), |
|
|
|
|
AlgorithmType::SHA2_256 => Box::new(Sha256::new()), |
|
|
|
|
AlgorithmType::SHA2_512_256 => Box::new(Sha512Trunc256::new()), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
hash.input(bytes); |
|
|
|
|
hash.result_str() |
|
|
|
|
hex::encode(hash.result()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Calculate a hash of string's bytes using the selected algorithm
|
|
|
|
|
pub fn hash_str(&self, bytes: &str) -> String { |
|
|
|
|
pub fn hash_str(self, bytes: &str) -> String { |
|
|
|
|
self.hash(bytes.as_bytes()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|