Something toybox has had for years, thought you guys might be interested. A way
to get these things to output ONLY the hash, so it's more easily scriptable.

In the gnu/dammit version -b says not to strip DOS newlines, which we already
ignored as a NOP in at least some cases...

Rob
From: Rob Landley <r...@landley.net>
Date: Thu, 31 Aug 2023 03:58:58 -0500
Subject: [PATCH] Add *sum -b (brief)

---
 coreutils/md5_sha1_sum.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index f6a21237d..2277c764a 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -39,12 +39,12 @@
 //config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
 //config:
 //config:config FEATURE_MD5_SHA1_SUM_CHECK
-//config:	bool "Enable -c, -s and -w options"
+//config:	bool "Enable -b, -c, -s and -w options"
 //config:	default y
 //config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
 //config:	help
 //config:	Enabling the -c options allows files to be checked
-//config:	against pre-calculated hash values.
+//config:	against pre-calculated hash values, -b gives just hash output,
 //config:	-s and -w are useful options when verifying checksums.
 
 //applet:IF_MD5SUM(APPLET_NOEXEC(md5sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, md5sum))
@@ -64,6 +64,7 @@
 //usage:#define md5sum_full_usage "\n\n"
 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " MD5 checksums"
 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
+//usage:     "\n	-b	Brief (hash only, no filename)"
 //usage:     "\n	-c	Check sums against list in FILEs"
 //usage:     "\n	-s	Don't output anything, status code shows success"
 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
@@ -84,6 +85,7 @@
 //usage:#define sha1sum_full_usage "\n\n"
 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA1 checksums"
 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
+//usage:     "\n	-b	Brief (hash only, no filename)"
 //usage:     "\n	-c	Check sums against list in FILEs"
 //usage:     "\n	-s	Don't output anything, status code shows success"
 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
@@ -94,6 +96,7 @@
 //usage:#define sha256sum_full_usage "\n\n"
 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA256 checksums"
 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
+//usage:     "\n	-b	Brief (hash only, no filename)"
 //usage:     "\n	-c	Check sums against list in FILEs"
 //usage:     "\n	-s	Don't output anything, status code shows success"
 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
@@ -104,6 +107,7 @@
 //usage:#define sha512sum_full_usage "\n\n"
 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA512 checksums"
 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
+//usage:     "\n	-b	Brief (hash only, no filename)"
 //usage:     "\n	-c	Check sums against list in FILEs"
 //usage:     "\n	-s	Don't output anything, status code shows success"
 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
@@ -114,6 +118,7 @@
 //usage:#define sha3sum_full_usage "\n\n"
 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA3 checksums"
 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
+//usage:     "\n	-b	Brief (hash only, no filename)"
 //usage:     "\n	-c	Check sums against list in FILEs"
 //usage:     "\n	-s	Don't output anything, status code shows success"
 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
@@ -137,9 +142,10 @@ enum {
 	HASH_SHA512 = '5',
 };
 
-#define FLAG_SILENT  1
-#define FLAG_CHECK   2
-#define FLAG_WARN    4
+#define FLAG_BRIEF   1
+#define FLAG_SILENT  2
+#define FLAG_CHECK   4
+#define FLAG_WARN    8
 
 /* This might be useful elsewhere */
 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
@@ -263,17 +269,17 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
 		/* -s and -w require -c */
 #if ENABLE_SHA3SUM
 		if (applet_name[3] == HASH_SHA3)
-			flags = getopt32(argv, "^" "scwbta:+" "\0" "s?c:w?c", &sha3_width);
+			flags = getopt32(argv, "^" "bscwta:+" "\0" "s?c:w?c", &sha3_width);
 		else
 #endif
-			flags = getopt32(argv, "^" "scwbt" "\0" "s?c:w?c");
+			flags = getopt32(argv, "^" "bscwt" "\0" "s?c:w?c");
 	} else {
 #if ENABLE_SHA3SUM
 		if (applet_name[3] == HASH_SHA3)
-			getopt32(argv, "a:+", &sha3_width);
+			flags = getopt32(argv, "ba:+", &sha3_width);
 		else
 #endif
-			getopt32(argv, "");
+			flags = getopt32(argv, "b");
 	}
 	argv += optind;
 	//argc -= optind;
@@ -351,7 +357,8 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
 			if (hash_value == NULL) {
 				return_value = EXIT_FAILURE;
 			} else {
-				printf("%s  %s\n", hash_value, *argv);
+				if (flags & FLAG_BRIEF) printf("%s\n", hash_value);
+				else printf("%s  %s\n", hash_value, *argv);
 				free(hash_value);
 			}
 		}
-- 
2.20.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to