X-Git-Url: https://code.octet-stream.net/hashgood/blobdiff_plain/93ceef0a11e57980c967222a6504da7005895a5b..5d1d9862b731b309b546dffdbd4fe3984210eda6:/src/calculate.rs?ds=sidebyside diff --git a/src/calculate.rs b/src/calculate.rs index 5928c52..3ba5a7d 100644 --- a/src/calculate.rs +++ b/src/calculate.rs @@ -2,6 +2,7 @@ use super::Algorithm; 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::*; @@ -57,6 +58,11 @@ pub fn create_digests(algorithms: &[Algorithm], mut input: Box) -> Cal 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 @@ -115,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::*;