Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Uid] Fix UuidV7 collisions within the same ms #52367

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Symfony/Component/Uid/UuidV7.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public static function generate(\DateTimeInterface $time = null): string
self::$rand[1] &= 0x03FF;
self::$time = $time;
} else {
// Within the same ms, we increment the rand part by a random 24-bit number.
// Instead of getting this number from random_bytes(), which is slow, we get
// it by sha512-hashing self::$seed. This produces 64 bytes of entropy,
// which we need to split in a list of 24-bit numbers. unpack() first splits
// them into 16 x 32-bit numbers; we take the first byte of each of these
// numbers to get 5 extra 24-bit numbers. Then, we consume those numbers
// one-by-one and run this logic every 21 iterations.
// self::$rand holds the random part of the UUID, split into 5 x 16-bit
// numbers for x86 portability. We increment this random part by the next
// 24-bit number in the self::$seedParts list and decrement self::$seedIndex.

if (!self::$seedIndex) {
$s = unpack('l*', self::$seed = hash('sha512', self::$seed, true));
$s[] = ($s[1] >> 8 & 0xFF0000) | ($s[2] >> 16 & 0xFF00) | ($s[3] >> 24 & 0xFF);
Expand All @@ -75,7 +86,7 @@ public static function generate(\DateTimeInterface $time = null): string
self::$seedIndex = 21;
}

self::$rand[5] = 0xFFFF & $carry = self::$rand[5] + (self::$seedParts[self::$seedIndex--] & 0xFFFFFF);
self::$rand[5] = 0xFFFF & $carry = self::$rand[5] + 1 + (self::$seedParts[self::$seedIndex--] & 0xFFFFFF);
self::$rand[4] = 0xFFFF & $carry = self::$rand[4] + ($carry >> 16);
self::$rand[3] = 0xFFFF & $carry = self::$rand[3] + ($carry >> 16);
self::$rand[2] = 0xFFFF & $carry = self::$rand[2] + ($carry >> 16);
Expand Down
Loading