X-Git-Url: https://code.octet-stream.net/hashgood/blobdiff_plain/cfcab0977a3267bfc7f52a0d26224753a491bbcb..69d73a1e6ca6798d880d79d0e5687c7a86019dd8:/src/calculate.rs diff --git a/src/calculate.rs b/src/calculate.rs index 6f81e79..955016d 100644 --- a/src/calculate.rs +++ b/src/calculate.rs @@ -1,13 +1,13 @@ use super::Algorithm; -use crossbeam_channel::bounded; -use crossbeam_channel::Receiver; use md5::{Digest, Md5}; use sha1::Sha1; use sha2::Sha256; +use sha2::Sha512; use std::error::Error; use std::fs::File; use std::io::prelude::*; use std::path::Path; +use std::sync::mpsc::{channel, Receiver}; use std::sync::Arc; use std::thread; use std::thread::JoinHandle; @@ -44,20 +44,25 @@ pub fn create_digests(algorithms: &[Algorithm], mut input: Box) -> Cal let mut handles = vec![]; if algorithms.contains(&Algorithm::Md5) { - let (s, r) = bounded::>>(1); + let (s, r) = channel(); senders.push(s); handles.push(md5_digest(r)); } if algorithms.contains(&Algorithm::Sha1) { - let (s, r) = bounded::>>(1); + let (s, r) = channel(); senders.push(s); handles.push(sha1_digest(r)); } if algorithms.contains(&Algorithm::Sha256) { - let (s, r) = bounded::>>(1); + let (s, r) = channel(); senders.push(s); handles.push(sha256_digest(r)); } + if algorithms.contains(&Algorithm::Sha512) { + let (s, r) = channel(); + senders.push(s); + handles.push(sha512_digest(r)); + } // 64 KB chunks will be read from the input at 64 KB and supplied to all hashing threads at once // Right now that could be up to three threads. If CPU-bound, the other threads will mostly block while the slowest one finishes @@ -116,6 +121,18 @@ fn sha256_digest(rx: Receiver>>) -> JoinHandle<(Algorithm, Vec)> }) } +/// Calculate the sha512 digest of some data on the given channel +fn sha512_digest(rx: Receiver>>) -> JoinHandle<(Algorithm, Vec)> { + thread::spawn(move || { + let mut sha512 = Sha512::new(); + while let Ok(chunk) = rx.recv() { + sha512.update(&*chunk); + } + let result = sha512.finalize(); + (Algorithm::Sha512, result.to_vec()) + }) +} + #[cfg(test)] mod tests { use super::*; @@ -140,7 +157,7 @@ mod tests { "b9193853f7798e92e2f6b82eda336fa7d6fc0fa90fdefe665f372b0bad8cdf8c"; fn verify_digest(alg: Algorithm, data: &'static [u8], hash: &str) { - let reader = Cursor::new(&*data); + let reader = Cursor::new(data); let digests = create_digests(&[alg], Box::new(reader)).unwrap(); assert_eq!(digests.len(), 1); assert_eq!(digests[0], (alg, hex::decode(hash).unwrap()));