8000 [Uid] Fix UuidV7 collisions within the same ms · symfony/symfony@e3bf65b · GitHub
[go: up one dir, main page]

Skip to content

Commit e3bf65b

Browse files
[Uid] Fix UuidV7 collisions within the same ms
1 parent 6c62a62 commit e3bf65b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Symfony/Component/Uid/UuidV7.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ public static function generate(\DateTimeInterface $time = null): string
6464
self::$rand[1] &= 0x03FF;
6565
self::$time = $time;
6666
} else {
67+
// Within the same ms, we increment the rand part by a random 24-bit number.
68+
// Instead of getting this number from random_bytes(), which is slow, we get
69+
// it by sha512-hashing self::$seed. This produces 64 bytes of entropy,
70+
// which we need to split in a list of 24-bit numbers. unpack() first splits
71+
// them into 16 x 32-bit numbers; we take the first byte of each of these
72+
// numbers to get 5 extra 24-bit numbers. Then, we consume those numbers
73+
// one-by-one and run this logic every 21 iterations.
74+
// self::$rand holds the random part of the UUID, split into 5 x 16-bit
75+
// numbers for x86 portability. We increment this random part by the next
76+
// 24-bit number in the self::$seedParts list and decrement self::$seedIndex.
77+
6778
if (!self::$seedIndex) {
6879
$s = unpack('l*', self::$seed = hash('sha512', self::$seed, true));
6980
$s[] = ($s[1] >> 8 & 0xFF0000) | ($s[2] >> 16 & 0xFF00) | ($s[3] >> 24 & 0xFF);
@@ -75,7 +86,7 @@ public static function generate(\DateTimeInterface $time = null): string
7586
se 660D lf::$seedIndex = 21;
7687
}
7788

78-
self::$rand[5] = 0xFFFF & $carry = self::$rand[5] + (self::$seedParts[self::$seedIndex--] & 0xFFFFFF);
89+
self::$rand[5] = 0xFFFF & $carry = self::$rand[5] + 1 + (self::$seedParts[self::$seedIndex--] & 0xFFFFFF);
7990
self::$rand[4] = 0xFFFF & $carry = self::$rand[4] + ($carry >> 16);
8091
self::$rand[3] = 0xFFFF & $carry = self::$rand[3] + ($carry >> 16);
8192
self::$rand[2] = 0xFFFF & $carry = self::$rand[2] + ($carry >> 16);

0 commit comments

Comments
 (0)
0