hashing_copy/
lib.rs

1#[cfg(test)]
2#[macro_use] extern crate hex_literal;
3
4extern crate digest;
5use digest::Digest;
6use digest::generic_array::GenericArray;
7
8use std::io::{self, Read, Write, ErrorKind};
9
10const DEFAULT_BUF_SIZE: usize = 4 * 1024 * 1024;
11
12/// Copy data from `reader` to `writer`, along the way hashing using `H` which must implement
13/// Digest. Return value is the same as for `std::io::copy` except that it returns a 2 tuple of the
14/// bytes copied, and the hash value, or an `io::Error`.
15///
16/// ```rust
17/// # extern crate sha2;
18/// # extern crate hashing_copy;
19/// # use hashing_copy::copy_and_hash;
20/// use std::io;
21/// use sha2::Sha256;
22///
23/// let mut reader: &[u8] = b"hello world";
24/// let mut writer: Vec<u8> = vec![];
25///
26/// let (bytes_copied, hash) = copy_and_hash::<_, _, Sha256>(&mut reader, &mut writer)
27///                                 .expect("Couldn't copy data");
28///
29/// assert_eq!(11, bytes_copied);
30/// assert_eq!(&b"hello world"[..], &writer[..]);
31/// assert_eq!(hash[..=8], [0xb9, 0x4d, 0x27, 0xb9, 0x93, 0x4d, 0x3e, 0x08, 0xa5]);
32/// ```
33
34pub fn copy_and_hash<R: ?Sized, W: ?Sized, H>(reader: &mut R, writer: &mut W) -> io::Result<(u64, GenericArray<u8, H::OutputSize>)>
35    where R: Read, W: Write, H: Digest
36{
37    let mut buf = vec![0; DEFAULT_BUF_SIZE];
38    let mut hasher = H::new();
39
40    let mut written = 0;
41    loop {
42        let len = match reader.read(&mut buf) {
43            Ok(0) => return Ok((written, hasher.result())),
44            Ok(len) => len,
45            Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
46            Err(e) => return Err(e),
47        };
48        hasher.input(&buf[..len]);
49        writer.write_all(&buf[..len])?;
50        written += len as u64;
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::copy_and_hash;
57    extern crate sha2;
58    use std::fs::File;
59
60    #[test]
61    fn test_copies_small_things() {
62        let result = hex!("26c6394cd46693652def622def991758b4c611c4029c2e47dc8c6504f4be600f");
63        let len = 17;
64        let input = "butts butts butts";
65        let mut output = vec![];
66
67        let ret = copy_and_hash::<_, _, sha2::Sha256>(&mut input.as_bytes(), &mut output).unwrap();
68        assert_eq!(ret.0, len);
69        assert_eq!(ret.1.as_slice(), result);
70        assert_eq!(String::from_utf8(output).unwrap(), input);
71    }
72
73    #[test]
74    fn test_copies_large_things() {
75        let result = hex!("87bcb5058da1531811646857b8d5684429480ef938fd0b143408c42c2fe8e974");
76        let len = 84084;
77        let mut input = File::open("test/many_butts").unwrap();
78        let mut output = vec![];
79
80        let ret = copy_and_hash::<_, _, sha2::Sha256>(&mut input, &mut output).unwrap();
81        assert_eq!(ret.0, len);
82        assert_eq!(ret.1.as_slice(), result);
83    }
84
85    #[test]
86    fn test_copies_things_spanning_multiple_blocks() {
87        let result = hex!("4c34caef17ee3d709ea9f3c964a79722f79118cd00869a340c3bdf1bb38375c3");
88        let len = 7020351;
89        let mut input = File::open("test/extremely_many_butts").unwrap();
90        let mut output = vec![];
91
92        let ret = copy_and_hash::<_, _, sha2::Sha256>(&mut input, &mut output).unwrap();
93        assert_eq!(ret.0, len);
94        assert_eq!(ret.1.as_slice(), result);
95    }
96}