From 30d7a4b16796e93b1715a84ccb7fba3d1adcec0b Mon Sep 17 00:00:00 2001 From: Shreyans Jain Date: Thu, 10 Feb 2022 12:46:44 +0530 Subject: [PATCH 1/3] hashsum: Add BLAKE3 to Hashing Algorithms Signed-off-by: Shreyans Jain --- Cargo.lock | 30 +++++++++++++++++++++++++- src/uu/hashsum/Cargo.toml | 1 + src/uu/hashsum/src/digest.rs | 23 ++++++++++++++++++++ src/uu/hashsum/src/hashsum.rs | 10 +++++++++ tests/by-util/test_hashsum.rs | 1 + tests/fixtures/hashsum/b3sum.checkfile | 1 + tests/fixtures/hashsum/b3sum.expected | 1 + 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/hashsum/b3sum.checkfile create mode 100644 tests/fixtures/hashsum/b3sum.expected diff --git a/Cargo.lock b/Cargo.lock index cc7c3967b7f..b2a545e20ca 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,8 +129,22 @@ 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]] @@ -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 6032a942877..495e159721f 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 23fe84e773e..c06834c744c 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 30c9d5b9298..fe607b5549b 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 545b4ee781b..293270a777d 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 00000000000..64a9bf7a4cb --- /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 00000000000..a56e54432d0 --- /dev/null +++ b/tests/fixtures/hashsum/b3sum.expected @@ -0,0 +1 @@ +a1a55887535397bf461902491c8779188a5dd1f8c3951b3d9cf6ecba194e87b0 \ No newline at end of file From 3176ad5c1b722509c295d8fe0c713414d5bcc16e Mon Sep 17 00:00:00 2001 From: Shreyans Jain Date: Thu, 10 Feb 2022 13:55:53 +0530 Subject: [PATCH 2/3] tests/hashsum: Fix missing space in checkfile --- tests/fixtures/hashsum/b3sum.checkfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/hashsum/b3sum.checkfile b/tests/fixtures/hashsum/b3sum.checkfile index 64a9bf7a4cb..f8d34d0b6f9 100644 --- a/tests/fixtures/hashsum/b3sum.checkfile +++ b/tests/fixtures/hashsum/b3sum.checkfile @@ -1 +1 @@ -a1a55887535397bf461902491c8779188a5dd1f8c3951b3d9cf6ecba194e87b0 input.txt \ No newline at end of file +a1a55887535397bf461902491c8779188a5dd1f8c3951b3d9cf6ecba194e87b0 input.txt From 6391f4c28aad3a6e384e411f213c1fd13054462c Mon Sep 17 00:00:00 2001 From: Shreyans Jain Date: Fri, 11 Feb 2022 14:18:56 +0530 Subject: [PATCH 3/3] util/build-gnu.sh: Add b3sum Signed-off-by: Shreyans Jain --- util/build-gnu.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/build-gnu.sh b/util/build-gnu.sh index a52d42107a0..8f6e431a6f0 100755 --- a/util/build-gnu.sh +++ b/util/build-gnu.sh @@ -20,7 +20,7 @@ make PROFILE=release BUILDDIR="$PWD/target/release/" cp "${BUILDDIR}/install" "${BUILDDIR}/ginstall" # The GNU tests rename this script before running, to avoid confusion with the make target # Create *sum binaries -for sum in b2sum md5sum sha1sum sha224sum sha256sum sha384sum sha512sum +for sum in b2sum b3sum md5sum sha1sum sha224sum sha256sum sha384sum sha512sum do sum_path="${BUILDDIR}/${sum}" test -f "${sum_path}" || cp "${BUILDDIR}/hashsum" "${sum_path}"