| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This file is part of the ramsey/uuid library
|
|
|
5 |
*
|
|
|
6 |
* For the full copyright and license information, please view the LICENSE
|
|
|
7 |
* file that was distributed with this source code.
|
|
|
8 |
*
|
|
|
9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
|
|
10 |
* @license http://opensource.org/licenses/MIT MIT
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
declare(strict_types=1);
|
|
|
14 |
|
|
|
15 |
namespace Ramsey\Uuid\Generator;
|
|
|
16 |
|
|
|
17 |
use Brick\Math\BigInteger;
|
|
|
18 |
use DateTimeImmutable;
|
|
|
19 |
use DateTimeInterface;
|
|
|
20 |
use Ramsey\Uuid\Type\Hexadecimal;
|
|
|
21 |
|
|
|
22 |
use function hash;
|
|
|
23 |
use function pack;
|
|
|
24 |
use function str_pad;
|
|
|
25 |
use function strlen;
|
|
|
26 |
use function substr;
|
|
|
27 |
use function substr_replace;
|
|
|
28 |
use function unpack;
|
|
|
29 |
|
|
|
30 |
use const PHP_INT_SIZE;
|
|
|
31 |
use const STR_PAD_LEFT;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* UnixTimeGenerator generates bytes that combine a 48-bit timestamp in
|
|
|
35 |
* milliseconds since the Unix Epoch with 80 random bits
|
|
|
36 |
*
|
|
|
37 |
* Code and concepts within this class are borrowed from the symfony/uid package
|
|
|
38 |
* and are used under the terms of the MIT license distributed with symfony/uid.
|
|
|
39 |
*
|
|
|
40 |
* symfony/uid is copyright (c) Fabien Potencier.
|
|
|
41 |
*
|
|
|
42 |
* @link https://symfony.com/components/Uid Symfony Uid component
|
|
|
43 |
* @link https://github.com/symfony/uid/blob/4f9f537e57261519808a7ce1d941490736522bbc/UuidV7.php Symfony UuidV7 class
|
|
|
44 |
* @link https://github.com/symfony/uid/blob/6.2/LICENSE MIT License
|
|
|
45 |
*/
|
|
|
46 |
class UnixTimeGenerator implements TimeGeneratorInterface
|
|
|
47 |
{
|
|
|
48 |
private static string $time = '';
|
|
|
49 |
private static ?string $seed = null;
|
|
|
50 |
private static int $seedIndex = 0;
|
|
|
51 |
|
|
|
52 |
/** @var int[] */
|
|
|
53 |
private static array $rand = [];
|
|
|
54 |
|
|
|
55 |
/** @var int[] */
|
|
|
56 |
private static array $seedParts;
|
|
|
57 |
|
|
|
58 |
public function __construct(
|
|
|
59 |
private RandomGeneratorInterface $randomGenerator,
|
|
|
60 |
private int $intSize = PHP_INT_SIZE
|
|
|
61 |
) {
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* @param Hexadecimal|int|string|null $node Unused in this generator
|
|
|
66 |
* @param int|null $clockSeq Unused in this generator
|
|
|
67 |
* @param DateTimeInterface $dateTime A date-time instance to use when
|
|
|
68 |
* generating bytes
|
|
|
69 |
*
|
|
|
70 |
* @inheritDoc
|
|
|
71 |
*/
|
|
|
72 |
public function generate($node = null, ?int $clockSeq = null, ?DateTimeInterface $dateTime = null): string
|
|
|
73 |
{
|
|
|
74 |
$time = ($dateTime ?? new DateTimeImmutable('now'))->format('Uv');
|
|
|
75 |
|
|
|
76 |
if ($time > self::$time || ($dateTime !== null && $time !== self::$time)) {
|
|
|
77 |
$this->randomize($time);
|
|
|
78 |
} else {
|
|
|
79 |
$time = $this->increment();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
if ($this->intSize >= 8) {
|
|
|
83 |
$time = substr(pack('J', (int) $time), -6);
|
|
|
84 |
} else {
|
|
|
85 |
$time = str_pad(BigInteger::of($time)->toBytes(false), 6, "\x00", STR_PAD_LEFT);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/** @var non-empty-string */
|
|
|
89 |
return $time . pack('n*', self::$rand[1], self::$rand[2], self::$rand[3], self::$rand[4], self::$rand[5]);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function randomize(string $time): void
|
|
|
93 |
{
|
|
|
94 |
if (self::$seed === null) {
|
|
|
95 |
$seed = $this->randomGenerator->generate(16);
|
|
|
96 |
self::$seed = $seed;
|
|
|
97 |
} else {
|
|
|
98 |
$seed = $this->randomGenerator->generate(10);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/** @var int[] $rand */
|
|
|
102 |
$rand = unpack('n*', $seed);
|
|
|
103 |
$rand[1] &= 0x03ff;
|
|
|
104 |
|
|
|
105 |
self::$rand = $rand;
|
|
|
106 |
self::$time = $time;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Special thanks to Nicolas Grekas for sharing the following information:
|
|
|
111 |
*
|
|
|
112 |
* Within the same ms, we increment the rand part by a random 24-bit number.
|
|
|
113 |
*
|
|
|
114 |
* Instead of getting this number from random_bytes(), which is slow, we get
|
|
|
115 |
* it by sha512-hashing self::$seed. This produces 64 bytes of entropy,
|
|
|
116 |
* which we need to split in a list of 24-bit numbers. unpack() first splits
|
|
|
117 |
* them into 16 x 32-bit numbers; we take the first byte of each of these
|
|
|
118 |
* numbers to get 5 extra 24-bit numbers. Then, we consume those numbers
|
|
|
119 |
* one-by-one and run this logic every 21 iterations.
|
|
|
120 |
*
|
|
|
121 |
* self::$rand holds the random part of the UUID, split into 5 x 16-bit
|
|
|
122 |
* numbers for x86 portability. We increment this random part by the next
|
|
|
123 |
* 24-bit number in the self::$seedParts list and decrement
|
|
|
124 |
* self::$seedIndex.
|
|
|
125 |
*
|
|
|
126 |
* @link https://twitter.com/nicolasgrekas/status/1583356938825261061 Tweet from Nicolas Grekas
|
|
|
127 |
*/
|
|
|
128 |
private function increment(): string
|
|
|
129 |
{
|
|
|
130 |
if (self::$seedIndex === 0 && self::$seed !== null) {
|
|
|
131 |
self::$seed = hash('sha512', self::$seed, true);
|
|
|
132 |
|
|
|
133 |
/** @var int[] $s */
|
|
|
134 |
$s = unpack('l*', self::$seed);
|
|
|
135 |
$s[] = ($s[1] >> 8 & 0xff0000) | ($s[2] >> 16 & 0xff00) | ($s[3] >> 24 & 0xff);
|
|
|
136 |
$s[] = ($s[4] >> 8 & 0xff0000) | ($s[5] >> 16 & 0xff00) | ($s[6] >> 24 & 0xff);
|
|
|
137 |
$s[] = ($s[7] >> 8 & 0xff0000) | ($s[8] >> 16 & 0xff00) | ($s[9] >> 24 & 0xff);
|
|
|
138 |
$s[] = ($s[10] >> 8 & 0xff0000) | ($s[11] >> 16 & 0xff00) | ($s[12] >> 24 & 0xff);
|
|
|
139 |
$s[] = ($s[13] >> 8 & 0xff0000) | ($s[14] >> 16 & 0xff00) | ($s[15] >> 24 & 0xff);
|
|
|
140 |
|
|
|
141 |
self::$seedParts = $s;
|
|
|
142 |
self::$seedIndex = 21;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
self::$rand[5] = 0xffff & $carry = self::$rand[5] + (self::$seedParts[self::$seedIndex--] & 0xffffff);
|
|
|
146 |
self::$rand[4] = 0xffff & $carry = self::$rand[4] + ($carry >> 16);
|
|
|
147 |
self::$rand[3] = 0xffff & $carry = self::$rand[3] + ($carry >> 16);
|
|
|
148 |
self::$rand[2] = 0xffff & $carry = self::$rand[2] + ($carry >> 16);
|
|
|
149 |
self::$rand[1] += $carry >> 16;
|
|
|
150 |
|
|
|
151 |
if (0xfc00 & self::$rand[1]) {
|
|
|
152 |
$time = self::$time;
|
|
|
153 |
$mtime = (int) substr($time, -9);
|
|
|
154 |
|
|
|
155 |
if ($this->intSize >= 8 || strlen($time) < 10) {
|
|
|
156 |
$time = (string) ((int) $time + 1);
|
|
|
157 |
} elseif ($mtime === 999999999) {
|
|
|
158 |
$time = (1 + (int) substr($time, 0, -9)) . '000000000';
|
|
|
159 |
} else {
|
|
|
160 |
$mtime++;
|
|
|
161 |
$time = substr_replace($time, str_pad((string) $mtime, 9, '0', STR_PAD_LEFT), -9);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
$this->randomize($time);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
return self::$time;
|
|
|
168 |
}
|
|
|
169 |
}
|