basic cli working
This commit is contained in:
@@ -3,6 +3,7 @@ mod blake256;
|
||||
mod blake3;
|
||||
mod blake512;
|
||||
mod crc32;
|
||||
pub use crc32::CRC32TYPE;
|
||||
mod md5;
|
||||
mod null_hash;
|
||||
mod sha1;
|
||||
@@ -63,6 +64,42 @@ impl HashReturn {
|
||||
HashReturn::RAW(inner) => inner,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_hex(self) -> String {
|
||||
crate::u8_array_to_lower_hex_string(&self.into_bytes()).unwrap()
|
||||
}
|
||||
|
||||
pub fn serialize(self) -> String {
|
||||
match self {
|
||||
HashReturn::CRC32(_, t) => {
|
||||
format!("crc32_{t:?}: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::SHA256(_) => {
|
||||
format!("sha256: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::SHA3_256(_) => {
|
||||
format!("sha3_256: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::BLAKE256(_) => {
|
||||
format!("blake256: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::BLAKE512(_) => {
|
||||
format!("blake512: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::BLAKE3(_) => {
|
||||
format!("blake3: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::SHA1(_) => {
|
||||
format!("sha1: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::MD5(_) => {
|
||||
format!("md5: {}", self.as_hex())
|
||||
}
|
||||
HashReturn::RAW(_) => {
|
||||
format!("raw: {}", self.as_hex())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait Hasher {
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
mod hashes;
|
||||
pub use hashes::*;
|
||||
mod hex_table;
|
||||
pub use hex_table::*;
|
||||
|
||||
74
src/main.rs
74
src/main.rs
@@ -3,9 +3,81 @@
|
||||
mod args;
|
||||
use args::Args;
|
||||
|
||||
use hashstream::{CRC32TYPE, HashType, Hashes};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use std::io;
|
||||
use std::io::{Read, stdin};
|
||||
use std::process::exit;
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
println!("Hello, world!");
|
||||
let mut hashes = Vec::new();
|
||||
|
||||
if args.sha256 || args.all {
|
||||
hashes.push(HashType::SHA256)
|
||||
}
|
||||
|
||||
if args.sha3_256 || args.all {
|
||||
hashes.push(HashType::SHA3_256)
|
||||
}
|
||||
|
||||
if args.sha1 || args.all {
|
||||
hashes.push(HashType::SHA1)
|
||||
}
|
||||
|
||||
if args.crc32_iso || args.all {
|
||||
hashes.push(HashType::CRC32(CRC32TYPE::ISO))
|
||||
}
|
||||
|
||||
if args.crc32_posix || args.all {
|
||||
hashes.push(HashType::CRC32(CRC32TYPE::CKSUM))
|
||||
}
|
||||
|
||||
if args.crc32_xfer || args.all {
|
||||
hashes.push(HashType::CRC32(CRC32TYPE::XFER))
|
||||
}
|
||||
|
||||
if args.blake256 || args.all {
|
||||
hashes.push(HashType::BLAKE256)
|
||||
}
|
||||
|
||||
if args.blake512 || args.all {
|
||||
hashes.push(HashType::BLAKE512)
|
||||
}
|
||||
|
||||
if args.blake3 || args.all {
|
||||
hashes.push(HashType::BLAKE3)
|
||||
}
|
||||
|
||||
if args.md5 || args.all {
|
||||
hashes.push(HashType::MD5)
|
||||
}
|
||||
|
||||
if hashes.is_empty() {
|
||||
eprintln!("Error: At least one hashtype or --all is required");
|
||||
exit(255);
|
||||
}
|
||||
|
||||
let mut hashers = Hashes::new(&hashes);
|
||||
|
||||
let stdin = stdin();
|
||||
let mut lock = stdin.lock();
|
||||
|
||||
let mut buf = [0u8; 1024];
|
||||
|
||||
while let Ok(count) = lock.read(&mut buf) {
|
||||
if count > 0 {
|
||||
hashers.digest(&buf[0..count])
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let ret = hashers.complete();
|
||||
|
||||
for hash in ret {
|
||||
println!("{}", hash.serialize())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user