keynesis_core/hash/
mod.rs

1pub use cryptoxide::digest::Digest;
2pub use cryptoxide::{blake2b::Blake2b, blake2s::Blake2s};
3
4pub trait Hash {
5    const HASH_LEN: usize;
6    const BLOCK_LEN: usize;
7
8    type HASH: AsRef<[u8]> + AsMut<[u8]> + Clone;
9    type BLOCK: AsRef<[u8]> + AsMut<[u8]> + Clone;
10
11    fn name() -> &'static str;
12
13    fn zero_hash() -> Self::HASH;
14
15    fn zero_block() -> Self::BLOCK;
16
17    fn hasher() -> Self;
18
19    fn reset(&mut self);
20
21    fn input(&mut self, data: impl AsRef<[u8]>);
22
23    fn result(&mut self, output: &mut Self::HASH);
24}
25
26impl Hash for Blake2b {
27    const HASH_LEN: usize = 64;
28    const BLOCK_LEN: usize = 128;
29
30    type HASH = [u8; 64];
31    type BLOCK = [u8; 128];
32
33    fn name() -> &'static str {
34        "BLAKE2b"
35    }
36
37    fn zero_hash() -> Self::HASH {
38        [0; Self::HASH_LEN]
39    }
40
41    fn zero_block() -> Self::BLOCK {
42        [0; Self::BLOCK_LEN]
43    }
44
45    fn hasher() -> Self {
46        Blake2b::new(Self::HASH_LEN)
47    }
48
49    fn reset(&mut self) {
50        Digest::reset(self)
51    }
52
53    fn input(&mut self, data: impl AsRef<[u8]>) {
54        Digest::input(self, data.as_ref())
55    }
56
57    fn result(&mut self, output: &mut Self::HASH) {
58        Digest::result(self, output.as_mut());
59    }
60}
61
62impl Hash for Blake2s {
63    const HASH_LEN: usize = 32;
64    const BLOCK_LEN: usize = 64;
65
66    type HASH = [u8; 32];
67    type BLOCK = [u8; 64];
68
69    fn name() -> &'static str {
70        "BLAKE2s"
71    }
72
73    fn zero_hash() -> Self::HASH {
74        [0; Self::HASH_LEN]
75    }
76
77    fn zero_block() -> Self::BLOCK {
78        [0; Self::BLOCK_LEN]
79    }
80
81    fn hasher() -> Self {
82        Blake2s::new(Self::HASH_LEN)
83    }
84
85    fn reset(&mut self) {
86        Digest::reset(self)
87    }
88
89    fn input(&mut self, data: impl AsRef<[u8]>) {
90        Digest::input(self, data.as_ref())
91    }
92
93    fn result(&mut self, output: &mut Self::HASH) {
94        Digest::result(self, output.as_mut());
95    }
96}