diff --git a/README.md b/README.md index 97b0640..b3d9bf2 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,13 @@ code between applets, making for an overall smaller binary. - rev - rm - rmdir -- sleep +- sha1sum +- sha224sum +- sha256sum +- sha384sum +- sha512sum - shitbox +- sleep - sync - true - unlink diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 252900e..61715e5 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -47,6 +47,8 @@ mod rmdir; mod sha1sum; mod sha224sum; mod sha256sum; +mod sha384sum; +mod sha512sum; mod shitbox; mod sleep; mod sync; @@ -114,6 +116,8 @@ pub fn get(name: &str) -> Option> { "sha1sum" => Some(Box::new(sha1sum::Sha1sum::default())), "sha224sum" => Some(Box::new(sha224sum::Sha224sum::default())), "sha256sum" => Some(Box::new(sha256sum::Sha256sum::default())), + "sha384sum" => Some(Box::new(sha384sum::Sha384sum::default())), + "sha512sum" => Some(Box::new(sha512sum::Sha512sum::default())), "shitbox" => Some(Box::new(shitbox::Shitbox::default())), "sleep" => Some(Box::new(sleep::Sleep::default())), "sync" => Some(Box::new(sync::Sync::default())), @@ -127,7 +131,7 @@ pub fn get(name: &str) -> Option> { } } -pub static COMMANDS: [&str; 47] = [ +pub static COMMANDS: [&str; 49] = [ "base32", "base64", "basename", @@ -166,6 +170,8 @@ pub static COMMANDS: [&str; 47] = [ "sha1sum", "sha224sum", "sha256sum", + "sha384sum", + "sha512sum", "shitbox", "sleep", "sync", diff --git a/src/cmd/sha256sum/mod.rs b/src/cmd/sha256sum/mod.rs index cdbee95..1412516 100644 --- a/src/cmd/sha256sum/mod.rs +++ b/src/cmd/sha256sum/mod.rs @@ -8,9 +8,9 @@ use sha2::{Digest, Sha256}; use std::{io, process}; #[derive(Debug, Default)] -pub struct Sha224sum; +pub struct Sha256sum; -impl Cmd for Sha224sum { +impl Cmd for Sha256sum { fn cli(&self) -> clap::Command { Command::new("sha256sum") .about("compute and check SHA1 message digest") diff --git a/src/cmd/sha384sum/mod.rs b/src/cmd/sha384sum/mod.rs new file mode 100644 index 0000000..b7e8672 --- /dev/null +++ b/src/cmd/sha384sum/mod.rs @@ -0,0 +1,53 @@ +use super::Cmd; +use crate::{ + args, + hash::{self, HashType}, +}; +use clap::Command; +use sha2::{Digest, Sha384}; +use std::{io, process}; + +#[derive(Debug, Default)] +pub struct Sha384sum; + +impl Cmd for Sha384sum { + fn cli(&self) -> clap::Command { + Command::new("sha384sum") + .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::Sha384, &mut erred)?; + } else { + let hasher = Sha384::new(); + let s = hash::compute_hash(f, hasher)?; + println!("{s} {f}"); + } + } + if erred > 0 { + println!("sha384sum: WARNING: {erred} computed checksum did NOT match"); + process::exit(1); + } + } + Ok(()) + } + + fn path(&self) -> Option { + Some(crate::Path::UsrBin) + } +} diff --git a/src/cmd/sha512sum/mod.rs b/src/cmd/sha512sum/mod.rs new file mode 100644 index 0000000..3d63bf0 --- /dev/null +++ b/src/cmd/sha512sum/mod.rs @@ -0,0 +1,53 @@ +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) + } +}