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
use crate::{AbstractChannel, Channel};
use sha2::{Digest, Sha256};
use std::io::{Read, Result, Write};
pub struct HashChannel<R, W> {
channel: Channel<R, W>,
hash: Sha256,
}
impl<R: Read, W: Write> HashChannel<R, W> {
pub fn new(reader: R, writer: W) -> Self {
let channel = Channel::new(reader, writer);
let hash = Sha256::new();
Self { channel, hash }
}
pub fn finish(self) -> [u8; 32] {
let mut h = [0u8; 32];
h.copy_from_slice(&self.hash.finalize());
h
}
}
impl<R: Read, W: Write> AbstractChannel for HashChannel<R, W> {
#[inline]
fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
self.hash.update(bytes);
self.channel.write_bytes(bytes)
}
#[inline]
fn read_bytes(&mut self, mut bytes: &mut [u8]) -> Result<()> {
self.channel.read_bytes(&mut bytes)?;
self.hash.update(&bytes);
Ok(())
}
#[inline]
fn flush(&mut self) -> Result<()> {
self.channel.flush()
}
#[inline]
fn clone(&self) -> Self {
Self {
channel: self.channel.clone(),
hash: self.hash.clone(),
}
}
}