Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6ddf07

Browse files
committed
libuuid: fix name-based UUIDs
The current version is not fully compatible with RFC4122. It incorrectly encodes UUID variant xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx where M is UUID version and N is UUID variant. $ python -c "import uuid ; print(uuid.uuid5(uuid.UUID(int=0), 'foo'))" aa752cea-8222-5bc8-acd9-555b090c0ccb ^^ Old version: $ uuidgen --namespace 00000000-0000-0000-0000-000000000000 --name 'foo' --sha1 aa752cea-8222-5bc8-8cd9-555b090c0ccb ^^ Fixed version: ./uuidgen --namespace 00000000-0000-0000-0000-000000000000 --name 'foo' --sha1; aa752cea-8222-5bc8-acd9-555b090c0ccb ^^ The patch uses uuid_unpack and uuid_pack. It makes code more readable and allow to access proper octens. The same way we already use for time and random based UUIDs. Addresses: #683 Signed-off-by: Karel Zak <[email protected]>
1 parent 9a31275 commit d6ddf07

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

libuuid/src/gen_uuid.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@
9696
#define THREAD_LOCAL static
9797
#endif
9898

99-
/* index with UUID_VARIANT_xxx and shift 5 bits */
100-
static unsigned char variant_bits[] = { 0x00, 0x04, 0x06, 0x07 };
101-
10299
#ifdef _WIN32
103100
static void gettimeofday (struct timeval *tv, void *dummy)
104101
{
@@ -566,21 +563,22 @@ void uuid_generate_md5(uuid_t out, const uuid_t ns, const char *name, size_t len
566563
{
567564
UL_MD5_CTX ctx;
568565
char hash[UL_MD5LENGTH];
566+
uuid_t buf;
567+
struct uuid uu;
569568

570569
ul_MD5Init(&ctx);
571-
/* hash concatenation of well-known UUID with name */
572570
ul_MD5Update(&ctx, ns, sizeof(uuid_t));
573571
ul_MD5Update(&ctx, (const unsigned char *)name, len);
574-
575572
ul_MD5Final((unsigned char *)hash, &ctx);
576573

577-
memcpy(out, hash, sizeof(uuid_t));
574+
assert(sizeof(buf) <= sizeof(hash));
578575

579-
out[6] &= ~(UUID_TYPE_MASK << UUID_TYPE_SHIFT);
580-
out[6] |= (UUID_TYPE_DCE_MD5 << UUID_TYPE_SHIFT);
576+
memcpy(buf, hash, sizeof(buf));
577+
uuid_unpack(buf, &uu);
581578

582-
out[8] &= ~(UUID_VARIANT_MASK << UUID_VARIANT_SHIFT);
583-
out[8] |= (variant_bits[UUID_VARIANT_DCE] << UUID_VARIANT_SHIFT);
579+
uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
580+
uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x3000;
581+
uuid_pack(&uu, out);
584582
}
585583

586584
/*
@@ -591,20 +589,20 @@ void uuid_generate_sha1(uuid_t out, const uuid_t ns, const char *name, size_t le
591589
{
592590
UL_SHA1_CTX ctx;
593591
char hash[UL_SHA1LENGTH];
592+
uuid_t buf;
593+
struct uuid uu;
594594

595595
ul_SHA1Init(&ctx);
596-
/* hash concatenation of well-known UUID with name */
597596
ul_SHA1Update(&ctx, ns, sizeof(uuid_t));
598597
ul_SHA1Update(&ctx, (const unsigned char *)name, len);
599-
600598
ul_SHA1Final((unsigned char *)hash, &ctx);
601599

602-
memcpy(out, hash, sizeof(uuid_t));
600+
assert(sizeof(buf) <= sizeof(hash));
603601

604-
out[6] &= ~(UUID_TYPE_MASK << UUID_TYPE_SHIFT);
605-
out[6] |= (UUID_TYPE_DCE_SHA1 << UUID_TYPE_SHIFT);
602+
memcpy(buf, hash, sizeof(buf));
603+
uuid_unpack(buf, &uu);
606604

607-
out[8] &= ~(UUID_VARIANT_MASK << UUID_VARIANT_SHIFT);
608-
out[8] |= (variant_bits[UUID_VARIANT_DCE] << UUID_VARIANT_SHIFT);
605+
uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
606+
uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x5000;
607+
uuid_pack(&uu, out);
609608
}
610-

0 commit comments

Comments
 (0)