1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::fmt;
use blake2::{Blake2b, Digest};
use sha2::{Sha256, Sha512};
use sha3::{Sha3_256};
use digest::DynDigest;
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug,Clone,Eq,PartialEq,Hash)]
pub enum HashType {
Blake2b512 = 0,
SHA256 = 1,
SHA512 = 2,
SHA3_256 = 3,
}
impl HashType {
pub fn default_len(&self) -> usize {
match self {
HashType::Blake2b512 => 512,
HashType::SHA256 => 256,
HashType::SHA512 => 512,
HashType::SHA3_256 => 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",
};
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(Blake2b::new()),
HashType::SHA256 => Box::new(Sha256::new()),
HashType::SHA512 => Box::new(Sha512::new()),
HashType::SHA3_256 => Box::new(Sha3_256::new()),
}
}
pub fn default_hashtype() -> HashType {
HashType::Blake2b512
}
}