]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sha256: add sha256_direct_hex() helper
authorLennart Poettering <lennart@amutable.com>
Fri, 19 Jun 2026 03:12:10 +0000 (05:12 +0200)
committerLennart Poettering <lennart@amutable.com>
Mon, 22 Jun 2026 12:39:50 +0000 (14:39 +0200)
A various places we need a SHA256SUM of something as a string. Let's add
a simple helper for this that does this generically.

src/basic/sha256.c
src/basic/sha256.h
src/shared/pcrextend-util.c
src/shared/tar-util.c

index 31e535d4d291e71fd317a0b07363cbe21c1ef143..ebefdc7540955bcd0c7f095b96889e6508876ddb 100644 (file)
@@ -11,6 +11,8 @@ int sha256_fd(int fd, uint64_t max_size, uint8_t ret[static SHA256_DIGEST_SIZE])
         struct sha256_ctx ctx;
         uint64_t total_size = 0;
 
+        assert(fd >= 0);
+
         sha256_init_ctx(&ctx);
 
         for (;;) {
@@ -53,3 +55,12 @@ int parse_sha256(const char *s, uint8_t ret[static SHA256_DIGEST_SIZE]) {
 bool sha256_is_valid(const char *s) {
         return s && in_charset(s, HEXDIGITS) && (strlen(s) == SHA256_DIGEST_SIZE * 2);
 }
+
+char* sha256_direct_hex(const void *buffer, size_t sz) {
+        assert(buffer || sz == 0);
+
+        if (sz == SIZE_MAX)
+                sz = strlen(buffer);
+
+        return hexmem(SHA256_DIRECT(buffer, sz), SHA256_DIGEST_SIZE);
+}
index 74314a0cab853133f719f8e0f8ca76ec18e543eb..8d3387ea49086910bd19c9c00d61df96ca868781 100644 (file)
@@ -11,3 +11,5 @@ int sha256_fd(int fd, uint64_t max_size, uint8_t ret[static SHA256_DIGEST_SIZE])
 int parse_sha256(const char *s, uint8_t ret[static SHA256_DIGEST_SIZE]);
 
 bool sha256_is_valid(const char *s) _pure_;
+
+char* sha256_direct_hex(const void *buffer, size_t sz);
index cf6eb7d1a56f711efe02320dec8967c3701c81a8..5bd87e03c91ca068290c9dd367ce47464a0a820a 100644 (file)
@@ -358,7 +358,7 @@ int pcrextend_imds_userdata_word(const struct iovec *data, char **ret) {
         /* We include both a hash of the complete user data, and a truncated version of the data in the word
          * we measure. The former protects the actual data, the latter is useful for debugging. */
 
-        _cleanup_free_ char *hash = hexmem(SHA256_DIRECT(data->iov_base, data->iov_len), SHA256_DIGEST_SIZE);
+        _cleanup_free_ char *hash = sha256_direct_hex(data->iov_base, data->iov_len);
         if (!hash)
                 return log_oom();
 
index 97c41adba9a459d1314720d7105d912091b07f4a..e2603680bda787f1a62c938f4388964f40a9552e 100644 (file)
@@ -14,7 +14,6 @@
 #include "chattr-util.h"
 #include "fd-util.h"
 #include "fs-util.h"
-#include "hexdecoct.h"
 #include "iovec-util.h"
 #include "libarchive-util.h"
 #include "mountpoint-util.h"
@@ -1184,7 +1183,7 @@ static int hardlink_lookup(
         else
                 assert(d->have_unique_mount_id == (r > 0));
 
-        m = hexmem(SHA256_DIRECT(handle->f_handle, handle->handle_bytes), SHA256_DIGEST_SIZE);
+        m = sha256_direct_hex(handle->f_handle, handle->handle_bytes);
         if (!m)
                 return log_oom();