Debian Codegraph @

lib/3rdParty/composer/symfony/uid/UuidV6.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Uid;
13
14 use Symfony\Component\Uid\Exception\InvalidArgumentException;
15
16 /**
17 * A v6 UUID is lexicographically sortable and contains a 60-bit timestamp and 62 extra unique bits.
18 *
19 * Unlike UUIDv1, this implementation of UUIDv6 doesn't leak the MAC address of the host.
20 *
21 * @author Nicolas Grekas <[email protected]>
22 */
23 class UuidV6 extends Uuid implements TimeBasedUidInterface
24 {
25 protected const TYPE = 6;
26
27 private static string $node;
28
29 public function __construct(?string $uuid = null)
30 {
31 if (null === $uuid) {
32 $this->uid = static::generate();
33 } else {
34 parent::__construct($uuid, true);
35 }
36 }
37
38 public function getDateTime(): \DateTimeImmutable
39 {
40 return BinaryUtil::hexToDateTime('0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3));
41 }
42
43 public function getNode(): string
44 {
45 return substr($this->uid, 24);
46 }
47
48 public function toV7(): UuidV7
49 {
50 $uuid = $this->uid;
51 $time = BinaryUtil::hexToNumericString('0'.substr($uuid, 0, 8).substr($uuid, 9, 4).substr($uuid, 15, 3));
52 if ('-' === $time[0]) {
53 throw new InvalidArgumentException('Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
54 }
55
56 $ms = \strlen($time) > 4 ? substr($time, 0, -4) : '0';
57 $time = dechex(10000 * hexdec(substr($uuid, 20, 3)) + substr($time, -4));
58
59 if (\strlen($time) > 6) {
60 $uuid[29] = dechex(hexdec($uuid[29]) ^ hexdec($time[0]));
61 $time = substr($time, 1);
62 }
63
64 return new UuidV7(substr_replace(\sprintf(
65 '%012s-7%s-%s%s-%s%06s',
66 \PHP_INT_SIZE >= 8 ? dechex($ms) : bin2hex(BinaryUtil::fromBase($ms, BinaryUtil::BASE10)),
67 substr($uuid, -6, 3),
68 $uuid[19],
69 substr($uuid, -3),
70 substr($uuid, -12, 6),
71 $time
72 ), '-', 8, 0));
73 }
74
75 public static function generate(?\DateTimeInterface $time = null, ?Uuid $node = null): string
76 {
77 $uuidV1 = UuidV1::generate($time, $node);
78 $uuid = substr($uuidV1, 15, 3).substr($uuidV1, 9, 4).$uuidV1[0].'-'.substr($uuidV1, 1, 4).'-6'.substr($uuidV1, 5, 3).substr($uuidV1, 18, 6);
79
80 if ($node) {
81 return $uuid.substr($uuidV1, 24);
82 }
83
84 // uuid_create() returns a stable "node" that can leak the MAC of the host, but
85 // UUIDv6 prefers a truly random number here, let's XOR both to preserve the entropy
86
87 if (!isset(self::$node)) {
88 $seed = [random_int(0, 0xFFFFFF), random_int(0, 0xFFFFFF)];
89 $node = unpack('N2', hex2bin('00'.substr($uuidV1, 24, 6)).hex2bin('00'.substr($uuidV1, 30)));
90 self::$node = \sprintf('%06x%06x', ($seed[0] ^ $node[1]) | 0x010000, $seed[1] ^ $node[2]);
91 }
92
93 return $uuid.self::$node;
94 }
95 }