hashlib_prelude/
algorithm.rs1pub trait HashOption: ::std::default::Default {}
11
12pub enum HashError {
14 GenericError,
15}
16
17pub trait HashAlgoKernel {
19 type Option: HashOption;
20
21 type Error: ::std::convert::Into<HashError> + ::std::fmt::Debug;
22
23 type Output: ::std::cmp::Eq
25 + ::std::convert::AsRef<[u8]>
26 + ::std::fmt::LowerHex
27 + ::std::fmt::UpperHex;
28
29 fn name() -> &'static str;
31
32 fn digest_size() -> usize;
34
35 fn new(opt: Self::Option) -> Self;
37
38 fn update<T>(&mut self, data: T) -> Result<(), Self::Error>
40 where
41 T: ::std::convert::AsRef<[u8]>;
42
43 fn finalize(self) -> Result<Self::Output, Self::Error>;
45}
46
47pub trait HashAlgo: ::std::default::Default {
49 type Kernel: HashAlgoKernel;
50
51 fn setup() -> <<Self as HashAlgo>::Kernel as HashAlgoKernel>::Option {
52 ::std::default::Default::default()
53 }
54
55 fn hash(
57 input: &[u8],
58 ) -> Result<
59 <<Self as HashAlgo>::Kernel as HashAlgoKernel>::Output,
60 <<Self as HashAlgo>::Kernel as HashAlgoKernel>::Error,
61 > {
62 let opt = Self::setup();
63 let mut algo = Self::Kernel::new(opt);
64 algo.update(input)?;
65 algo.finalize()
66 }
67}