]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cksum: fix --check with --algorithm=sha2
authorPádraig Brady <P@draigBrady.com>
Mon, 6 Oct 2025 12:23:55 +0000 (13:23 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 7 Oct 2025 14:58:34 +0000 (15:58 +0100)
* src/digest.c (split_3): Look up the provided tag with -a sha2
because there is not a 1:1 mapping between them.
* tests/cksum/cksum-c.sh: Add a test case.
* NEWS: Mention the bug fix.

NEWS
src/digest.c
tests/cksum/cksum-c.sh

diff --git a/NEWS b/NEWS
index d5946ced93895ffba7d8260ebdde27ff52b920d1..e1c161d96c07110b550f56be3870564f0533dc6d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   for all length adjustable algorithms (blake2b, sha2, sha3).
   [bug introduced in coreutils-9.2]
 
+  'cksum --check -a sha2' now supports tagged format.
+  '-a sha2' is not required with tagged format, but should be accepted.
+  [bug introduced in coreutils-9.8]
+
   'rm -d DIR' no longer fails on Ceph snapshot directories.
   Although these directories are nonempty, 'rmdir DIR' succeeds on them.
   [bug introduced in coreutils-8.16]
index 13b1667950000b09059e131748febede198a7a0a..86119b5ab3a9ece350abd2593e74c19363b1dbb8 100644 (file)
@@ -843,16 +843,20 @@ split_3 (char *s, size_t s_len,
   /* Check for BSD-style checksum line. */
 
 #if HASH_ALGO_CKSUM
-  if (! algorithm_specified)
+  if (! algorithm_specified || cksum_algorithm == sha2)
     {
       ptrdiff_t algo_tag = algorithm_from_tag (s + i);
       if (algo_tag >= 0)
         {
           if (algo_tag <= crc32b)
             return false;  /* We don't support checking these older formats.  */
+          if (cksum_algorithm == sha2 && algo_tag != sha2
+              && algo_tag != sha224 && algo_tag != sha256
+              && algo_tag != sha384 && algo_tag != sha512)
+            return false;  /* Wrong tag for -a sha2.  */
           cksum_algorithm = algo_tag;
         }
-      else
+      else if (! algorithm_specified)
         return false;  /* We only support tagged format without -a.  */
     }
 #endif
index 95ceb4f334d107b9723da3d8d246ce1d995820d2..9e08bddeb0e759ac0e71ee69c25040fdc92bd9cb 100755 (executable)
@@ -26,10 +26,15 @@ for args in '-a sha2 -l 384' '-a blake2b' '-a blake2b -l 384' '-a sm3'; do
 done
 cksum --strict --check CHECKSUMS || fail=1
 
-# We don't output but do support SHA2-### tagged format
-cksum -a sha2 -l 384 input |
-  sed 's/^SHA/SHA2-/' > sha2-tag.sum || framework_failure_
-cksum --check sha2-tag.sum || fail=1
+# We don't output but do support SHA2-### tagged format.
+# Also ensure we check both formats with and without -a specified.
+cksum -a sha2 -l 384 input > sha384-tag.sum || framework_failure_
+sed 's/^SHA/SHA2-/' sha384-tag.sum > sha2-tag.sum || framework_failure_
+for file in sha384-tag.sum sha2-tag.sum; do
+  for spec in '' '-a sha2'; do
+    cksum --check $spec $file || fail=1
+  done
+done
 
 # Ensure leading whitespace and \ ignored
 sed 's/^/ \\/' CHECKSUMS | cksum --strict -c || fail=1