parity_multihash/
hashes.rs

1/// List of types currently supported in the multihash spec.
2///
3/// Not all hash types are supported by this library.
4#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash)]
5pub enum Hash {
6    /// Identity (Raw binary )
7    Identity,
8    /// SHA-1 (20-byte hash size)
9    SHA1,
10    /// SHA-256 (32-byte hash size)
11    SHA2256,
12    /// SHA-512 (64-byte hash size)
13    SHA2512,
14    /// SHA3-512 (64-byte hash size)
15    SHA3512,
16    /// SHA3-384 (48-byte hash size)
17    SHA3384,
18    /// SHA3-256 (32-byte hash size)
19    SHA3256,
20    /// SHA3-224 (28-byte hash size)
21    SHA3224,
22    /// Keccak-224 (28-byte hash size)
23    Keccak224,
24    /// Keccak-256 (32-byte hash size)
25    Keccak256,
26    /// Keccak-384 (48-byte hash size)
27    Keccak384,
28    /// Keccak-512 (64-byte hash size)
29    Keccak512,
30    /// BLAKE2b-512 (64-byte hash size)
31    Blake2b512,
32    /// BLAKE2b-256 (32-byte hash size)
33    Blake2b256,
34    /// BLAKE2s-256 (32-byte hash size)
35    Blake2s256,
36    /// BLAKE2s-128 (16-byte hash size)
37    Blake2s128,
38}
39
40impl Hash {
41    /// Get the corresponding hash code.
42    pub fn code(&self) -> u16 {
43        match self {
44            Hash::Identity => 0x00,
45            Hash::SHA1 => 0x11,
46            Hash::SHA2256 => 0x12,
47            Hash::SHA2512 => 0x13,
48            Hash::SHA3224 => 0x17,
49            Hash::SHA3256 => 0x16,
50            Hash::SHA3384 => 0x15,
51            Hash::SHA3512 => 0x14,
52            Hash::Keccak224 => 0x1A,
53            Hash::Keccak256 => 0x1B,
54            Hash::Keccak384 => 0x1C,
55            Hash::Keccak512 => 0x1D,
56            Hash::Blake2b512 => 0xB240,
57            Hash::Blake2b256 => 0xB220,
58            Hash::Blake2s256 => 0xB260,
59            Hash::Blake2s128 => 0xB250,
60        }
61    }
62
63    /// Get the hash length in bytes.
64    pub fn size(&self) -> u8 {
65        match self {
66            Hash::Identity => 42,
67            Hash::SHA1 => 20,
68            Hash::SHA2256 => 32,
69            Hash::SHA2512 => 64,
70            Hash::SHA3224 => 28,
71            Hash::SHA3256 => 32,
72            Hash::SHA3384 => 48,
73            Hash::SHA3512 => 64,
74            Hash::Keccak224 => 28,
75            Hash::Keccak256 => 32,
76            Hash::Keccak384 => 48,
77            Hash::Keccak512 => 64,
78            Hash::Blake2b512 => 64,
79            Hash::Blake2b256 => 32,
80            Hash::Blake2s256 => 32,
81            Hash::Blake2s128 => 16,
82        }
83    }
84
85    /// Returns the algorithm corresponding to a code, or `None` if no algorithm is matching.
86    pub fn from_code(code: u16) -> Option<Hash> {
87        Some(match code {
88            0x00 => Hash::Identity,
89            0x11 => Hash::SHA1,
90            0x12 => Hash::SHA2256,
91            0x13 => Hash::SHA2512,
92            0x14 => Hash::SHA3512,
93            0x15 => Hash::SHA3384,
94            0x16 => Hash::SHA3256,
95            0x17 => Hash::SHA3224,
96            0x1A => Hash::Keccak224,
97            0x1B => Hash::Keccak256,
98            0x1C => Hash::Keccak384,
99            0x1D => Hash::Keccak512,
100            0xB240 => Hash::Blake2b512,
101            0xB220 => Hash::Blake2b256,
102            0xB260 => Hash::Blake2s256,
103            0xB250 => Hash::Blake2s128,
104            _ => return None,
105        })
106    }
107}