]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
id128: add id128_digest() helper
authorLennart Poettering <lennart@poettering.net>
Mon, 13 Nov 2023 09:58:18 +0000 (10:58 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 13 Nov 2023 16:39:53 +0000 (17:39 +0100)
This helper hashes some arbitrary data and turns it into a v4 UUID.

src/libsystemd/sd-id128/id128-util.c
src/libsystemd/sd-id128/id128-util.h

index 596b8786bf6627f9683e91288e6b9b2bf09acc18..dbe4d2015050643b8a00be7192324d4cafc7793c 100644 (file)
@@ -9,6 +9,7 @@
 #include "hexdecoct.h"
 #include "id128-util.h"
 #include "io-util.h"
+#include "sha256.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "sync-util.h"
@@ -234,3 +235,21 @@ int id128_get_product(sd_id128_t *ret) {
         *ret = uuid;
         return 0;
 }
+
+sd_id128_t id128_digest(const void *data, size_t size) {
+        assert(data || size == 0);
+
+        /* Hashes a UUID from some arbitrary data */
+
+        if (size == SIZE_MAX)
+                size = strlen(data);
+
+        uint8_t h[SHA256_DIGEST_SIZE];
+        sd_id128_t id;
+
+        /* Take the first half of the SHA256 result */
+        assert_cc(sizeof(h) >= sizeof(id.bytes));
+        memcpy(id.bytes, sha256_direct(data, size, h), sizeof(id.bytes));
+
+        return id128_make_v4_uuid(id);
+}
index 80c36e5a7ba21b3f9ff009c272e97f253809bd1e..53ba50a8ace5d42554b05edad0fec0462978fc7e 100644 (file)
@@ -47,6 +47,8 @@ sd_id128_t id128_make_v4_uuid(sd_id128_t id);
 
 int id128_get_product(sd_id128_t *ret);
 
+sd_id128_t id128_digest(const void *data, size_t size);
+
 /* A helper to check for the three relevant cases of "machine ID not initialized" */
 #define ERRNO_IS_NEG_MACHINE_ID_UNSET(r)        \
         IN_SET(r,                               \