Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BSD tagging support to b3sum #242

Open
systwi-again opened this issue Apr 13, 2022 · 5 comments · May be fixed by #430
Open

Add BSD tagging support to b3sum #242

systwi-again opened this issue Apr 13, 2022 · 5 comments · May be fixed by #430

Comments

@systwi-again
Copy link

systwi-again commented Apr 13, 2022

Title pretty much says it all. Could this feature be added to b3sum, please?

Visual example:

$ b3sum --tag -- foo.bin
BLAKE3 (foo.bin) = 6dfbfe0d4e63479d25ea18acb6897e0aa3a2a9dc4b4bc3cf38198c26f9f94c8f
@oconnor663
Copy link
Member

Seems reasonable enough. Would you be willing to submit a PR? If I understand correctly, the main change would be replacing this print statement with two different ways to print:

print!("{}", &hex_str[..2 * take_bytes as usize]);

@systwi-again
Copy link
Author

Thanks for the quick reply. I'm not familiar with programming languages outside of shell scripts and basic Python scripts, so I won't be of much help there, sorry.

Looking over digest.c in GNU coreutils' md5sum, it seems like the code around line 725 might be helpful to use for reference.

@odiferousmint
Copy link

odiferousmint commented Apr 21, 2022

Seems reasonable enough. Would you be willing to submit a PR? If I understand correctly, the main change would be replacing this print statement with two different ways to print:

print!("{}", &hex_str[..2 * take_bytes as usize]);

There is more to it. How about this?

diff --git a/b3sum/src/main.rs b/b3sum/src/main.rs
index 0b692a2..23dc098 100644
--- a/b3sum/src/main.rs
+++ b/b3sum/src/main.rs
@@ -22,4 +22,5 @@ const NUM_THREADS_ARG: &str = "num-threads";
 const RAW_ARG: &str = "raw";
 const CHECK_ARG: &str = "check";
+const TAG_ARG: &str = "tag";
 const QUIET_ARG: &str = "quiet";
 
@@ -115,4 +116,9 @@ impl Args {
                     ),
             )
+            .arg(
+                Arg::new(TAG_ARG)
+                    .long(TAG_ARG)
+                    .help("Create a BSD-style checksum."),
+            )
             // wild::args_os() is equivalent to std::env::args_os() on Unix,
             // but on Windows it adds support for globbing.
@@ -158,4 +164,8 @@ impl Args {
     }
 
+    fn tag(&self) -> bool {
+        self.inner.is_present(TAG_ARG)
+    }
+
     fn raw(&self) -> bool {
         self.inner.is_present(RAW_ARG)
@@ -315,4 +325,7 @@ fn write_hex_output(mut output: blake3::OutputReader, args: &Args) -> Result<()>
         let take_bytes = cmp::min(len, block.len() as u64);
         print!("{}", &hex_str[..2 * take_bytes as usize]);
+        if args.tag() {
+            print!("\n");
+        }
         len -= take_bytes;
     }
@@ -515,6 +528,11 @@ fn hash_one_input(path: &Path, args: &Args) -> Result<()> {
         print!("\\");
     }
-    write_hex_output(output, args)?;
-    println!("  {}", filepath_string);
+    if args.tag() {
+        print!("BLAKE3 ({}) = ", filepath_string);
+        write_hex_output(output, args)?;
+    } else {
+        write_hex_output(output, args)?;
+        println!("  {}", filepath_string);
+    }
     Ok(())
 }

Example:

$ echo "foobar" | ./target/debug/b3sum
534659321d2eea6b13aea4f4c94c3b4f624622295da31506722b47a8eb9d726c  -
$ echo "foobar" | ./target/debug/b3sum --tag
BLAKE3 (-) = 534659321d2eea6b13aea4f4c94c3b4f624622295da31506722b47a8eb9d726c
$ ./target/debug/b3sum ~/foobar
af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262  /home/user/foobar
$ ./target/debug/b3sum --tag ~/foobar
BLAKE3 (/home/user/foobar) = af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262

$ echo "foobar" | b2sum
9e2bf63e933e610efee4a8d6cd4a9387e80860edee97e27db3b37a828d226ab1eb92a9cdd8ca9ca67a753edaf8bd89a0558496f67a30af6f766943839acf0110  -
$ echo "foobar" | b2sum --tag
BLAKE2b (-) = 9e2bf63e933e610efee4a8d6cd4a9387e80860edee97e27db3b37a828d226ab1eb92a9cdd8ca9ca67a753edaf8bd89a0558496f67a30af6f766943839acf0110
$ b2sum ~/foobar
786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce  /home/user/foobar
$ b2sum --tag ~/foobar
BLAKE2b (/home/user/foobar) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce

I do not know Rust and I might have missed something. I suppose you could just pass filepath_string to write_hex_output(), but I did not want to modify it that much. Oh well. :)

@systwi-again: would this suffice?

@systwi-again
Copy link
Author

@odiferousmint Looks good, thanks for the help!

@leahneukirchen
Copy link

I came here to request the same feature, anything blocking this?

@dbohdan dbohdan linked a pull request Oct 23, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants