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
69
70
71
72
use core::fmt::Debug;
use generic_array::typenum::marker_traits::Unsigned;
use generic_array::{ArrayLength, GenericArray};
pub trait Size: ArrayLength<u8> + Debug + Default + Eq + Send + Sync + 'static {}
impl<T: ArrayLength<u8> + Debug + Default + Eq + Send + Sync + 'static> Size for T {}
pub trait Digest<S: Size>:
    AsRef<[u8]>
    + From<GenericArray<u8, S>>
    + Into<GenericArray<u8, S>>
    + Clone
    + Debug
    + Default
    + Eq
    + Send
    + Sync
    + 'static
{
}
pub trait Hasher: Default {
    
    type Size: Size;
    
    type Digest: Digest<Self::Size>;
    
    fn update(&mut self, input: &[u8]);
    
    fn finalize(&self) -> Self::Digest;
    
    fn reset(&mut self);
    
    fn size() -> u8 {
        Self::Size::to_u8()
    }
    
    fn digest(input: &[u8]) -> Self::Digest
    where
        Self: Sized,
    {
        let mut hasher = Self::default();
        hasher.update(input);
        hasher.finalize()
    }
}
#[cfg(feature = "std")]
pub struct WriteHasher<H: Hasher>(H);
#[cfg(feature = "std")]
impl<H: Hasher> std::io::Write for WriteHasher<H> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.update(buf);
        Ok(buf.len())
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}