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
#![no_std]
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
extern crate block_buffer;
#[macro_use]
extern crate opaque_debug;
#[macro_use]
pub extern crate digest;
#[cfg(feature = "std")]
extern crate std;
use block_buffer::byteorder::{ByteOrder, LE};
use block_buffer::BlockBuffer;
use digest::generic_array::typenum::{U40, U64};
use digest::generic_array::GenericArray;
pub use digest::Digest;
use digest::{BlockInput, FixedOutput, Input, Reset};
mod block;
use block::{process_msg_block, DIGEST_BUF_LEN, H0};
#[derive(Clone)]
pub struct Ripemd320 {
h: [u32; DIGEST_BUF_LEN],
len: u64,
buffer: BlockBuffer<U64>,
}
impl Default for Ripemd320 {
fn default() -> Self {
Ripemd320 {
h: H0,
len: 0,
buffer: Default::default(),
}
}
}
impl BlockInput for Ripemd320 {
type BlockSize = U64;
}
impl Input for Ripemd320 {
fn input<B: AsRef<[u8]>>(&mut self, input: B) {
let input = input.as_ref();
self.len += input.len() as u64;
let h = &mut self.h;
self.buffer.input(input, |b| process_msg_block(h, b));
}
}
impl FixedOutput for Ripemd320 {
type OutputSize = U40;
fn fixed_result(mut self) -> GenericArray<u8, Self::OutputSize> {
{
let h = &mut self.h;
let l = self.len << 3;
self.buffer
.len64_padding::<LE, _>(l, |b| process_msg_block(h, b));
}
let mut out = GenericArray::default();
LE::write_u32_into(&self.h, &mut out[..]);
out
}
}
impl Reset for Ripemd320 {
fn reset(&mut self) {
self.buffer.reset();
self.len = 0;
self.h = H0;
}
}
impl_opaque_debug!(Ripemd320);
impl_write!(Ripemd320);