Skip to content

Commit

Permalink
Merge pull request #3108 from DestroyerXyz/blake3
Browse files Browse the repository at this point in the history
hashsum: Add BLAKE3 to Hashing Algorithms
  • Loading branch information
sylvestre authored Feb 11, 2022
2 parents 748e6e7 + 6391f4c commit 080cb2b
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
30 changes: 29 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/hashsum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
23 changes: 23 additions & 0 deletions src/uu/hashsum/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn is_custom_binary(program: &str) -> bool {
| "shake128sum"
| "shake256sum"
| "b2sum"
| "b3sum"
)
}

Expand All @@ -93,6 +94,11 @@ fn detect_algo(
Box::new(blake2b_simd::State::new()) as Box<dyn Digest>,
512,
),
"b3sum" => (
"BLAKE3",
Box::new(blake3::Hasher::new()) as Box<dyn Digest>,
256,
),
"sha3sum" => match matches.value_of("bits") {
Some(bits_str) => match (bits_str).parse::<usize>() {
Ok(224) => (
Expand Down Expand Up @@ -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::<usize>() {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ test_digest! {
shake128_256 shake128 256
shake256_512 shake256 512
b2sum b2sum 512
b3sum b3sum 256
}
1 change: 1 addition & 0 deletions tests/fixtures/hashsum/b3sum.checkfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a1a55887535397bf461902491c8779188a5dd1f8c3951b3d9cf6ecba194e87b0 input.txt
1 change: 1 addition & 0 deletions tests/fixtures/hashsum/b3sum.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a1a55887535397bf461902491c8779188a5dd1f8c3951b3d9cf6ecba194e87b0
2 changes: 1 addition & 1 deletion util/build-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 080cb2b

Please sign in to comment.