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
#![no_std]
extern crate generic_array;
use generic_array::{GenericArray, ArrayLength};
pub trait Input {
type BlockSize: ArrayLength<u8>;
fn digest(&mut self, input: &[u8]);
}
pub trait FixedOutput {
type OutputSize: ArrayLength<u8>;
fn fixed_result(self) -> GenericArray<u8, Self::OutputSize>;
}
#[derive(Copy, Clone, Debug)]
pub struct InvalidLength;
#[must_use]
pub type VariableResult<'a> = Result<&'a [u8], InvalidLength>;
pub trait VariableOutput {
fn variable_result(self, buffer: &mut [u8]) -> VariableResult;
}
pub trait Digest: Input + FixedOutput {
type OutputSize: ArrayLength<u8>;
type BlockSize: ArrayLength<u8>;
fn input(&mut self, input: &[u8]);
fn result(self) -> GenericArray<u8, <Self as Digest>::OutputSize>;
}
impl<T: Input + FixedOutput> Digest for T {
type OutputSize = <T as FixedOutput>::OutputSize;
type BlockSize = <T as Input>::BlockSize;
fn input(&mut self, input: &[u8]) {
self.digest(input);
}
fn result(self) -> GenericArray<u8, <T as Digest>::OutputSize> {
self.fixed_result()
}
}