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
use crate::hash::Hash;
use tiny_keccak::Keccak;

pub struct MaskedKeccak(Keccak);

impl MaskedKeccak {
    const MASK_LENGTH: usize = 20;

    pub fn new() -> Self {
        Self(Keccak::new_keccak256())
    }

    pub fn update(&mut self, input: &[u8]) {
        self.0.update(input)
    }

    pub fn hash(self) -> Hash {
        let mut result: [u8; 32] = [0; 32];
        self.0.finalize(&mut result);
        for byte in result[Self::MASK_LENGTH..].iter_mut() {
            *byte = 0;
        }
        Hash::new(result)
    }
}

impl Default for MaskedKeccak {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "std")]
impl std::fmt::Debug for MaskedKeccak {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(fmt, "MaskedKeccak(...)")
    }
}