minor improvements
This commit is contained in:
parent
96f881d5ca
commit
be908e52dc
19
src/main.rs
19
src/main.rs
|
@ -1,4 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
fmt::Write,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, Read},
|
io::{self, Read},
|
||||||
};
|
};
|
||||||
|
@ -26,13 +27,13 @@ fn main() -> Result<(), std::io::Error> {
|
||||||
let mut stdin = stdin.lock();
|
let mut stdin = stdin.lock();
|
||||||
|
|
||||||
let hash = compute(&mut stdin)?;
|
let hash = compute(&mut stdin)?;
|
||||||
print(hash);
|
println!("{}", format(hash));
|
||||||
} else {
|
} else {
|
||||||
for file in files {
|
for file in files {
|
||||||
let mut f = match File::open(file.clone()) {
|
let mut f = match File::open(file.clone()) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("error in opening file: {}", e);
|
eprintln!("error in opening file: {}", e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -40,13 +41,12 @@ fn main() -> Result<(), std::io::Error> {
|
||||||
let hash = match compute(&mut f) {
|
let hash = match compute(&mut f) {
|
||||||
Ok(hash) => hash,
|
Ok(hash) => hash,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("error in computing hash of {}: {}", file, e);
|
eprintln!("error in computing hash of {}: {}", file, e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
print!("{}: ", file);
|
println!("{}: {}", file, format(hash));
|
||||||
print(hash);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,15 +67,16 @@ fn compute<R: Read>(r: &mut R) -> Result<[u32; 8], std::io::Error> {
|
||||||
Ok(s256.sum())
|
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 c in hash {
|
||||||
for b in c.to_be_bytes() {
|
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 {
|
struct Sha256 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user