feat(alg): crc32sum

This commit is contained in:
cscherr 2025-07-22 13:56:52 +02:00
parent 32643ad085
commit 078e55ef4a
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
4 changed files with 29 additions and 34 deletions

20
Cargo.lock generated
View file

@ -17,7 +17,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"assert_hex", "assert_hex",
"criterion", "criterion",
"getopts",
"iai", "iai",
"pretty_assertions", "pretty_assertions",
] ]
@ -116,7 +115,7 @@ checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"textwrap", "textwrap",
"unicode-width 0.1.14", "unicode-width",
] ]
[[package]] [[package]]
@ -322,15 +321,6 @@ dependencies = [
"num", "num",
] ]
[[package]]
name = "getopts"
version = "0.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cba6ae63eb948698e300f645f87c70f76630d505f23b8907cf1e193ee85048c1"
dependencies = [
"unicode-width 0.2.1",
]
[[package]] [[package]]
name = "half" name = "half"
version = "1.8.3" version = "1.8.3"
@ -789,7 +779,7 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [ dependencies = [
"unicode-width 0.1.14", "unicode-width",
] ]
[[package]] [[package]]
@ -834,12 +824,6 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "unicode-width"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c"
[[package]] [[package]]
name = "vcell" name = "vcell"
version = "0.1.3" version = "0.1.3"

View file

@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
getopts = { version = "0.2.23", optional = true }
[dev-dependencies] [dev-dependencies]
assert_hex = "0.4.1" assert_hex = "0.4.1"
@ -24,7 +23,6 @@ harness = false
default = [] default = []
std = [] std = []
show_internals = ["std"] show_internals = ["std"]
getopts = ["dep:getopts"]
[[bin]] [[bin]]
path = "src/bin/crc32sum.rs" path = "src/bin/crc32sum.rs"
@ -33,5 +31,5 @@ required-features = ["std"]
[[bin]] [[bin]]
path = "src/bin/sha256sum.rs" path = "src/bin/sha256sum.rs"
name = "sha256sum" name = "algsha256sum"
required-features = ["std"] required-features = ["std"]

View file

@ -0,0 +1,18 @@
use std::io::Read;
use algorithms::crc::Crc;
fn main() {
let args: Vec<String> = std::env::args().collect();
let use_ffi = args.contains(&"-c".to_string());
let mut input = Vec::new();
std::io::stdin()
.read_to_end(&mut input)
.expect("could not read from stdin");
let crc = if use_ffi {
algorithms::crc::ffi::Crc32::checksum(&input)
} else {
algorithms::crc::Crc32::checksum(&input)
};
println!("{crc:08x}");
}

View file

@ -1,23 +1,18 @@
use std::io::Read;
use algorithms::hash::format_digest; use algorithms::hash::format_digest;
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
let use_ffi = args.contains(&"-c".to_string()); let use_ffi = args.contains(&"-c".to_string());
if args.len() < 2 { let mut input = Vec::new();
usage(&args[0], 1) std::io::stdin()
} .read_to_end(&mut input)
let input = &args[1]; .expect("could not read from stdin");
println!("{:x?}", input.as_bytes());
let raw_in = input.as_bytes();
let hash = if use_ffi { let hash = if use_ffi {
algorithms::hash::ffi::sha2_256_oneshot(raw_in).expect("could not hash your input data") algorithms::hash::ffi::sha2_256_oneshot(&input).expect("could not hash your input data")
} else { } else {
algorithms::hash::sha2_256_oneshot(raw_in).expect("could not hash your input data") algorithms::hash::sha2_256_oneshot(&input).expect("could not hash your input data")
}; };
println!("{}", format_digest(&hash)); println!("{}", format_digest(&hash));
} }
fn usage(call: &str, code: i32) -> ! {
println!("USAGE: {call} INPUT [-c]");
std::process::exit(code)
}