-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_algorithm.rs
More file actions
47 lines (43 loc) · 1.4 KB
/
hash_algorithm.rs
File metadata and controls
47 lines (43 loc) · 1.4 KB
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
use base_xx::SerialiseError;
/// Supported cryptographic hash algorithms.
///
/// This enum represents the different hash algorithms that can be used
/// to create hash values in the system.
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum HashAlgorithm {
/// Keccak-256 hash algorithm (used in Ethereum)
KECCAK256,
/// SHA-256 hash algorithm from the SHA-2 family
SHA256,
/// Keccak-384 hash algorithm
KECCAK384,
/// Keccak-512 hash algorithm
KECCAK512,
/// RIPEMD-160 hash algorithm
RIPEMD160,
}
impl TryFrom<u8> for HashAlgorithm {
type Error = SerialiseError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
100 => Ok(Self::KECCAK256),
101 => Ok(Self::SHA256),
102 => Ok(Self::KECCAK384),
103 => Ok(Self::KECCAK512),
104 => Ok(Self::RIPEMD160),
_ => Err(SerialiseError::new("Invalid hash algorithm".to_string())),
}
}
}
impl TryFrom<HashAlgorithm> for u8 {
type Error = SerialiseError;
fn try_from(value: HashAlgorithm) -> Result<Self, Self::Error> {
match value {
HashAlgorithm::KECCAK256 => Ok(100),
HashAlgorithm::SHA256 => Ok(101),
HashAlgorithm::KECCAK384 => Ok(102),
HashAlgorithm::KECCAK512 => Ok(103),
HashAlgorithm::RIPEMD160 => Ok(104),
}
}
}