master
Benoît 2018-05-01 23:13:58 +02:00 committed by Benoît Mauduit
parent 8e4b2f2597
commit cf4fb0529c
1 changed files with 45 additions and 34 deletions

View File

@ -90,6 +90,42 @@ impl Args {
}
}
struct FileToProcess<N> {
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<D: Digest + Default, R: Read>(&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::<Sha256, _>(&mut file,
&format!("[{}] - {}",
i,
entry.path().display()));
let a = FileToProcess<32> {
name: format!("[{}] - {}",
i,
entry.path().display()),
};
a.hash::<Sha256, _>(&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::<Sha256, _>(&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<D: Digest + Default, R: Read>(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);
}