Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
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
/**
15
 * A v6 UUID is lexicographically sortable and contains a 60-bit timestamp and 62 extra unique bits.
16
 *
17
 * Unlike UUIDv1, this implementation of UUIDv6 doesn't leak the MAC address of the host.
18
 *
19
 * @author Nicolas Grekas <p@tchwork.com>
20
 */
21
class UuidV6 extends Uuid implements TimeBasedUidInterface
22
{
23
    protected const TYPE = 6;
24
 
25
    private static string $node;
26
 
27
    public function __construct(string $uuid = null)
28
    {
29
        if (null === $uuid) {
30
            $this->uid = static::generate();
31
        } else {
32
            parent::__construct($uuid, true);
33
        }
34
    }
35
 
36
    public function getDateTime(): \DateTimeImmutable
37
    {
38
        return BinaryUtil::hexToDateTime('0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3));
39
    }
40
 
41
    public function getNode(): string
42
    {
43
        return substr($this->uid, 24);
44
    }
45
 
46
    public static function generate(\DateTimeInterface $time = null, Uuid $node = null): string
47
    {
48
        $uuidV1 = UuidV1::generate($time, $node);
49
        $uuid = substr($uuidV1, 15, 3).substr($uuidV1, 9, 4).$uuidV1[0].'-'.substr($uuidV1, 1, 4).'-6'.substr($uuidV1, 5, 3).substr($uuidV1, 18, 6);
50
 
51
        if ($node) {
52
            return $uuid.substr($uuidV1, 24);
53
        }
54
 
55
        // uuid_create() returns a stable "node" that can leak the MAC of the host, but
56
        // UUIDv6 prefers a truly random number here, let's XOR both to preserve the entropy
57
 
58
        if (!isset(self::$node)) {
59
            $seed = [random_int(0, 0xFFFFFF), random_int(0, 0xFFFFFF)];
60
            $node = unpack('N2', hex2bin('00'.substr($uuidV1, 24, 6)).hex2bin('00'.substr($uuidV1, 30)));
61
            self::$node = sprintf('%06x%06x', ($seed[0] ^ $node[1]) | 0x010000, $seed[1] ^ $node[2]);
62
        }
63
 
64
        return $uuid.self::$node;
65
    }
66
}