1
0
Fork 0

minor improvements

This commit is contained in:
Ishan Jain 2023-11-20 12:29:21 +05:30
parent 96f881d5ca
commit be908e52dc
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
1 changed files with 10 additions and 9 deletions

View File

@ -1,4 +1,5 @@
use std::{
fmt::Write,
fs::File,
io::{self, Read},
};
@ -26,13 +27,13 @@ fn main() -> Result<(), std::io::Error> {
let mut stdin = stdin.lock();
let hash = compute(&mut stdin)?;
print(hash);
println!("{}", format(hash));
} else {
for file in files {
let mut f = match File::open(file.clone()) {
Ok(v) => v,
Err(e) => {
println!("error in opening file: {}", e);
eprintln!("error in opening file: {}", e);
continue;
}
};
@ -40,13 +41,12 @@ fn main() -> Result<(), std::io::Error> {
let hash = match compute(&mut f) {
Ok(hash) => hash,
Err(e) => {
println!("error in computing hash of {}: {}", file, e);
eprintln!("error in computing hash of {}: {}", file, e);
continue;
}
};
print!("{}: ", file);
print(hash);
println!("{}: {}", file, format(hash));
}
}
@ -67,15 +67,16 @@ fn compute<R: Read>(r: &mut R) -> Result<[u32; 8], std::io::Error> {
Ok(s256.sum())
}
fn print(hash: [u32; 8]) {
fn format(hash: [u32; 8]) -> String {
let mut out = String::with_capacity(32 * 8);
for c in hash {
for b in c.to_be_bytes() {
print!("{:02x?}", b);
write!(out, "{:02x}", b).expect("error in converting hash u32 -> hex");
}
// print!(" ")
}
println!();
out
}
struct Sha256 {