use super::Cmd; use crate::{ args, hash::{self, HashType}, }; use clap::Command; use sha2::{Digest, Sha512}; use std::{io, process}; #[derive(Debug, Default)] pub struct Sha512sum; impl Cmd for Sha512sum { fn cli(&self) -> clap::Command { Command::new("sha512sum") .about("compute and check SHA1 message digest") .author("Nathan Fisher") .version(env!("CARGO_PKG_VERSION")) .args([args::check(), args::file()]) } fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { let Some(matches) = matches else { return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); }; if let Some(files) = matches.get_many::("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::Sha512, &mut erred)?; } else { let hasher = Sha512::new(); let s = hash::compute_hash(f, hasher)?; println!("{s} {f}"); } } if erred > 0 { println!("sha512sum: WARNING: {erred} computed checksum did NOT match"); process::exit(1); } } Ok(()) } fn path(&self) -> Option { Some(crate::Path::UsrBin) } }