diff --git a/Cargo.lock b/Cargo.lock index cc7c3967b..b2a545e20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,6 +50,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "arrayvec" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" + [[package]] name = "atty" version = "0.2.14" @@ -123,10 +129,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587" dependencies = [ "arrayref", - "arrayvec", + "arrayvec 0.5.2", "constant_time_eq", ] +[[package]] +name = "blake3" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08e53fc5a564bb15bfe6fae56bd71522205f1f91893f9c0116edad6496c183f" +dependencies = [ + "arrayref", + "arrayvec 0.7.2", + "cc", + "cfg-if 1.0.0", + "constant_time_eq", + "digest", +] + [[package]] name = "block-buffer" version = "0.10.0" @@ -670,6 +690,7 @@ dependencies = [ "block-buffer", "crypto-common", "generic-array", + "subtle", ] [[package]] @@ -1850,6 +1871,12 @@ dependencies = [ "syn", ] +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + [[package]] name = "syn" version = "1.0.86" @@ -2375,6 +2402,7 @@ name = "uu_hashsum" version = "0.0.12" dependencies = [ "blake2b_simd", + "blake3", "clap 3.0.10", "digest", "hex", diff --git a/src/uu/hashsum/Cargo.toml b/src/uu/hashsum/Cargo.toml index 6032a9428..495e15972 100644 --- a/src/uu/hashsum/Cargo.toml +++ b/src/uu/hashsum/Cargo.toml @@ -27,6 +27,7 @@ sha1 = "0.6.0" sha2 = "0.10.1" sha3 = "0.10.0" blake2b_simd = "0.5.11" +blake3 = "1.3.1" uucore = { version=">=0.0.11", package="uucore", path="../../uucore" } [[bin]] diff --git a/src/uu/hashsum/src/digest.rs b/src/uu/hashsum/src/digest.rs index 23fe84e77..c06834c74 100644 --- a/src/uu/hashsum/src/digest.rs +++ b/src/uu/hashsum/src/digest.rs @@ -81,6 +81,29 @@ impl Digest for blake2b_simd::State { } } +impl Digest for blake3::Hasher { + fn new() -> Self { + Self::new() + } + + fn input(&mut self, input: &[u8]) { + self.update(input); + } + + fn result(&mut self, out: &mut [u8]) { + let hash_result = &self.finalize(); + out.copy_from_slice(hash_result.as_bytes()); + } + + fn reset(&mut self) { + *self = Self::new(); + } + + fn output_bits(&self) -> usize { + 256 + } +} + impl Digest for sha1::Sha1 { fn new() -> Self { Self::new() diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index 30c9d5b92..fe607b554 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -70,6 +70,7 @@ fn is_custom_binary(program: &str) -> bool { | "shake128sum" | "shake256sum" | "b2sum" + | "b3sum" ) } @@ -93,6 +94,11 @@ fn detect_algo( Box::new(blake2b_simd::State::new()) as Box, 512, ), + "b3sum" => ( + "BLAKE3", + Box::new(blake3::Hasher::new()) as Box, + 256, + ), "sha3sum" => match matches.value_of("bits") { Some(bits_str) => match (bits_str).parse::() { Ok(224) => ( @@ -196,6 +202,9 @@ fn detect_algo( if matches.is_present("b2sum") { set_or_crash("BLAKE2", Box::new(blake2b_simd::State::new()), 512); } + if matches.is_present("b3sum") { + set_or_crash("BLAKE3", Box::new(blake3::Hasher::new()), 256); + } if matches.is_present("sha3") { match matches.value_of("bits") { Some(bits_str) => match (bits_str).parse::() { @@ -433,6 +442,7 @@ pub fn uu_app_custom<'a>() -> App<'a> { "work with SHAKE256 using BITS for the output size", ), ("b2sum", "work with BLAKE2"), + ("b3sum", "work with BLAKE3"), ]; for (name, desc) in algorithms { diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 545b4ee78..293270a77 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -80,4 +80,5 @@ test_digest! { shake128_256 shake128 256 shake256_512 shake256 512 b2sum b2sum 512 + b3sum b3sum 256 } diff --git a/tests/fixtures/hashsum/b3sum.checkfile b/tests/fixtures/hashsum/b3sum.checkfile new file mode 100644 index 000000000..64a9bf7a4 --- /dev/null +++ b/tests/fixtures/hashsum/b3sum.checkfile @@ -0,0 +1 @@ +a1a55887535397bf461902491c8779188a5dd1f8c3951b3d9cf6ecba194e87b0 input.txt \ No newline at end of file diff --git a/tests/fixtures/hashsum/b3sum.expected b/tests/fixtures/hashsum/b3sum.expected new file mode 100644 index 000000000..a56e54432 --- /dev/null +++ b/tests/fixtures/hashsum/b3sum.expected @@ -0,0 +1 @@ +a1a55887535397bf461902491c8779188a5dd1f8c3951b3d9cf6ecba194e87b0 \ No newline at end of file