From cf4fb0529c06366ddd9073b8ec48c5fbf0771350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Mauduit?= Date: Tue, 1 May 2018 23:13:58 +0200 Subject: [PATCH] WIP --- src/main.rs | 79 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3a2b204..49cceb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,6 +90,42 @@ impl Args { } } +struct FileToProcess { + hash: [u8; N], + name: String, + realpath: String, +} + +impl FileToProcess { + /// From https://github.com/RustCrypto/hashes/blob/master/sha2/examples/sha256sum.rs + /// Compute digest value for given `Reader` and print it + /// On any error simply return without doing anything + fn hash(&self, reader: &mut R, name: &str) { + let mut sh = D::default(); + let mut buffer = [0u8; BUFFER_SIZE]; + + loop { + let n = match reader.read(&mut buffer) { + Ok(n) => n, + Err(_) => return, + }; + sh.input(&buffer[..n]); + if n == 0 || n < BUFFER_SIZE { + break; + } + } + self.hash = sh.result(); + self.print_result(&sh.result(), name); + } + + /// Print digest result as hex string and name pair + fn print_result(&self, sum: &[u8], name: &str) { + for byte in sum { + print!("{:02x}", byte); + } + println!("\t{}", name); + } +} fn main() { let mut t = term::stdout().unwrap(); @@ -170,10 +206,15 @@ fn main() { if ft.is_file() { if let Ok(mut file) = fs::File::open(&entry.path()) { - process::(&mut file, - &format!("[{}] - {}", - i, - entry.path().display())); + let a = FileToProcess<32> { + name: format!("[{}] - {}", + i, + entry.path().display()), + }; + a.hash::(&mut file, + &format!("[{}] - {}", + i, + entry.path().display())); i += 1; } } @@ -183,7 +224,6 @@ fn main() { // for path in inputs { // let path_str = path.unwrap().path().into_os_string().into_string().unwrap(); // println!("[I] Name: {}", path_str); - // if let Ok(mut file) = fs::File::open(&path_str) { // process::(&mut file, // &path_str); @@ -194,32 +234,3 @@ fn main() { t.fg(term::color::CYAN).unwrap(); println!("Cheers !"); } - - -/// From https://github.com/RustCrypto/hashes/blob/master/sha2/examples/sha256sum.rs -/// Compute digest value for given `Reader` and print it -/// On any error simply return without doing anything -fn process(reader: &mut R, name: &str) { - let mut sh = D::default(); - let mut buffer = [0u8; BUFFER_SIZE]; - - loop { - let n = match reader.read(&mut buffer) { - Ok(n) => n, - Err(_) => return, - }; - sh.input(&buffer[..n]); - if n == 0 || n < BUFFER_SIZE { - break; - } - } - print_result(&sh.result(), name); -} - -/// Print digest result as hex string and name pair -fn print_result(sum: &[u8], name: &str) { - for byte in sum { - print!("{:02x}", byte); - } - println!("\t{}", name); -}