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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use super::{AlgorithmName, UpdateCore, VariableOutputCore};
use crate::{InvalidOutputSize, Reset, Update, VariableOutput};
use block_buffer::BlockBuffer;
use core::fmt;
use generic_array::typenum::Unsigned;
#[derive(Clone)]
pub struct RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
{
core: T,
buffer: BlockBuffer<T::BlockSize>,
output_size: usize,
}
impl<T> Reset for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
{
#[inline]
fn reset(&mut self) {
if let Ok(v) = T::new(self.output_size) {
self.core = v;
} else {
debug_assert!(false);
}
self.buffer.reset();
}
}
impl<T> Update for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
{
#[inline]
fn update(&mut self, input: &[u8]) {
let Self { core, buffer, .. } = self;
buffer.digest_blocks(input, |blocks| core.update_blocks(blocks));
}
}
impl<T> VariableOutput for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
{
const MAX_OUTPUT_SIZE: usize = T::MaxOutputSize::USIZE;
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
let buffer = Default::default();
T::new(output_size).map(|core| Self {
core,
buffer,
output_size,
})
}
fn output_size(&self) -> usize {
self.output_size
}
fn finalize_variable(mut self, f: impl FnOnce(&[u8])) {
let Self {
core,
buffer,
output_size,
} = &mut self;
core.finalize_variable_core(buffer, *output_size, f);
}
fn finalize_variable_reset(&mut self, f: impl FnOnce(&[u8])) {
let Self {
core,
buffer,
output_size,
} = self;
core.finalize_variable_core(buffer, *output_size, f);
self.reset()
}
}
impl<T> fmt::Debug for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore + AlgorithmName,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
T::write_alg_name(f)?;
f.write_str(" { .. }")
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T> std::io::Write for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
{
#[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
Update::update(self, buf);
Ok(buf.len())
}
#[inline]
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}