Add sha284sum and sha512sum applets

This commit is contained in:
Nathan Fisher 2023-02-04 01:06:54 -05:00
parent 5e0c1141ef
commit 5fd1bb1220
5 changed files with 121 additions and 4 deletions

View File

@ -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

View File

@ -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<Box<dyn Cmd>> {
"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<Box<dyn Cmd>> {
}
}
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",

View File

@ -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")

53
src/cmd/sha384sum/mod.rs Normal file
View File

@ -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<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
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::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<crate::Path> {
Some(crate::Path::UsrBin)
}
}

53
src/cmd/sha512sum/mod.rs Normal file
View File

@ -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<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
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::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<crate::Path> {
Some(crate::Path::UsrBin)
}
}