use std::fmt;
use blake3::{Hasher as Blake3};
use blake2::{Blake2b512};
use sha2::{Sha256, Sha512};
use sha3::{Sha3_256};
use digest::{DynDigest, Digest};
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug,Clone,Eq,PartialEq,Hash)]
pub enum HashType {
Blake2b512 = 0,
SHA256 = 1,
SHA512 = 2,
SHA3_256 = 3,
Blake3 = 4,
}
impl HashType {
pub fn default_len(&self) -> usize {
match self {
HashType::Blake2b512 => 512,
HashType::SHA256 => 256,
HashType::SHA512 => 512,
HashType::SHA3_256 => 256,
HashType::Blake3 => 256,
}
}
}
impl fmt::Display for HashType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self {
HashType::Blake2b512 => "Blake2b512",
HashType::SHA256 => "SHA256",
HashType::SHA512 => "SHA512",
HashType::SHA3_256 => "Sha3_256",
HashType::Blake3 => "Blake3"
};
write!(f, "{}", msg)
}
}
#[derive(Debug,Clone)]
pub struct Hash;
impl Hash {
pub fn new_hasher(hash_type: &HashType) -> Box<dyn DynDigest> {
match hash_type {
HashType::Blake2b512 => Box::new(Blake2b512::new()),
HashType::SHA256 => Box::new(Sha256::new()),
HashType::SHA512 => Box::new(Sha512::new()),
HashType::SHA3_256 => Box::new(Sha3_256::new()),
HashType::Blake3 => Box::new(Blake3::new()),
}
}
pub fn default_hashtype() -> HashType {
HashType::Blake3
}
}