X-Git-Url: https://code.octet-stream.net/hashgood/blobdiff_plain/9e69a3e75455b37ac9b9eea835e72f1fef0b12eb..5d1d9862b731b309b546dffdbd4fe3984210eda6:/src/calculate.rs diff --git a/src/calculate.rs b/src/calculate.rs index efc3986..3ba5a7d 100644 --- a/src/calculate.rs +++ b/src/calculate.rs @@ -1,14 +1,13 @@ use super::Algorithm; -use crossbeam_channel::bounded; -use crossbeam_channel::Receiver; -use crypto::digest::Digest; -use crypto::md5::Md5; -use crypto::sha1::Sha1; -use crypto::sha2::Sha256; +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; @@ -45,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 @@ -86,10 +90,9 @@ fn md5_digest(rx: Receiver>>) -> JoinHandle<(Algorithm, Vec)> { thread::spawn(move || { let mut md5 = Md5::new(); while let Ok(chunk) = rx.recv() { - md5.input(&chunk); + md5.update(&*chunk); } - let mut result = [0; 16]; - md5.result(&mut result); + let result = md5.finalize(); (Algorithm::Md5, result.to_vec()) }) } @@ -99,10 +102,9 @@ fn sha1_digest(rx: Receiver>>) -> JoinHandle<(Algorithm, Vec)> { thread::spawn(move || { let mut sha1 = Sha1::new(); while let Ok(chunk) = rx.recv() { - sha1.input(&chunk); + sha1.update(&*chunk); } - let mut result = [0; 20]; - sha1.result(&mut result); + let result = sha1.finalize(); (Algorithm::Sha1, result.to_vec()) }) } @@ -112,14 +114,25 @@ fn sha256_digest(rx: Receiver>>) -> JoinHandle<(Algorithm, Vec)> thread::spawn(move || { let mut sha256 = Sha256::new(); while let Ok(chunk) = rx.recv() { - sha256.input(&chunk); + sha256.update(&*chunk); } - let mut result = [0; 32]; - sha256.result(&mut result); + let result = sha256.finalize(); (Algorithm::Sha256, result.to_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::*;