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
#![no_std]
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
#![deny(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]
#[cfg(feature = "std")]
extern crate std;
mod block;
pub use digest::{self, Digest};
use crate::block::{process_msg_block, DIGEST_BUF_LEN, H0};
use block_buffer::BlockBuffer;
use digest::consts::{U20, U64};
use digest::{BlockInput, FixedOutputDirty, Reset, Update};
#[derive(Clone)]
pub struct Ripemd160 {
h: [u32; DIGEST_BUF_LEN],
len: u64,
buffer: BlockBuffer<U64>,
}
impl Default for Ripemd160 {
fn default() -> Self {
Ripemd160 {
h: H0,
len: 0,
buffer: Default::default(),
}
}
}
impl BlockInput for Ripemd160 {
type BlockSize = U64;
}
impl Update for Ripemd160 {
fn update(&mut self, input: impl AsRef<[u8]>) {
let input = input.as_ref();
self.len += input.len() as u64;
let h = &mut self.h;
self.buffer.input_block(input, |b| process_msg_block(h, b));
}
}
impl FixedOutputDirty for Ripemd160 {
type OutputSize = U20;
fn finalize_into_dirty(&mut self, out: &mut digest::Output<Self>) {
let h = &mut self.h;
let l = self.len << 3;
self.buffer.len64_padding_le(l, |b| process_msg_block(h, b));
for (chunk, v) in out.chunks_exact_mut(4).zip(self.h.iter()) {
chunk.copy_from_slice(&v.to_le_bytes());
}
}
}
impl Reset for Ripemd160 {
fn reset(&mut self) {
self.buffer.reset();
self.len = 0;
self.h = H0;
}
}
opaque_debug::implement!(Ripemd160);
digest::impl_write!(Ripemd160);