2023-02-04 00:56:02 -05:00
|
|
|
use super::Cmd;
|
|
|
|
use crate::{
|
|
|
|
args,
|
|
|
|
hash::{self, HashType},
|
|
|
|
};
|
|
|
|
use clap::Command;
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
use std::{io, process};
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
2023-02-04 01:06:54 -05:00
|
|
|
pub struct Sha256sum;
|
2023-02-04 00:56:02 -05:00
|
|
|
|
2023-02-04 01:06:54 -05:00
|
|
|
impl Cmd for Sha256sum {
|
2023-02-04 00:56:02 -05:00
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new("sha256sum")
|
|
|
|
.about("compute and check SHA1 message digest")
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.args([args::check(), args::file()])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-02-04 00:56:02 -05:00
|
|
|
if let Some(files) = matches.get_many::<String>("file") {
|
|
|
|
let mut erred = 0;
|
|
|
|
for f in files {
|
|
|
|
if matches.get_flag("check") {
|
|
|
|
if f == "-" {
|
|
|
|
return Err(
|
|
|
|
io::Error::new(io::ErrorKind::Other, "no file specified").into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
hash::check_sums(f, HashType::Sha256, &mut erred)?;
|
|
|
|
} else {
|
|
|
|
let hasher = Sha256::new();
|
|
|
|
let s = hash::compute_hash(f, hasher)?;
|
|
|
|
println!("{s} {f}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if erred > 0 {
|
|
|
|
println!("sha256sum: WARNING: {erred} computed checksum did NOT match");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
|
|
|
Some(crate::Path::UsrBin)
|
|
|
|
}
|
|
|
|
}
|