id128: add id128_digest() helper
This helper hashes some arbitrary data and turns it into a v4 UUID.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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, \
|
||||
|
||||
Reference in New Issue
Block a user