|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\Util; |
|
6
|
|
|
|
|
7
|
|
|
/* |
|
8
|
|
|
Layout and Byte Order |
|
9
|
|
|
http://tools.ietf.org/search/rfc4122#section-4.1.2 |
|
10
|
|
|
|
|
11
|
|
|
0 1 2 3 |
|
12
|
|
|
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
|
13
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
14
|
|
|
| time_low | |
|
15
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
16
|
|
|
| time_mid | time_hi_and_version | |
|
17
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
18
|
|
|
|clk_seq_hi_res | clk_seq_low | node (0-1) | |
|
19
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
20
|
|
|
| node (2-5) | |
|
21
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* UUID version 4. |
|
26
|
|
|
* |
|
27
|
|
|
* @see http://tools.ietf.org/search/rfc4122#section-4.4 |
|
28
|
|
|
*/ |
|
29
|
|
|
class UUIDv4 |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* UUID. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_uuid; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $uuid UUIDv4 in canonical hexadecimal format |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function __construct(string $uuid) |
|
44
|
|
|
{ |
|
45
|
|
|
// @todo Check that UUID is version 4 |
|
46
|
2 |
|
$this->_uuid = $uuid; |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function __toString(): string |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->canonical(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create new random UUIDv4. |
|
59
|
|
|
* |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public static function createRandom(): self |
|
63
|
|
|
{ |
|
64
|
|
|
/* |
|
65
|
|
|
1. Set the two most significant bits (bits 6 and 7) of |
|
66
|
|
|
the clock_seq_hi_and_reserved to zero and one, respectively. |
|
67
|
|
|
|
|
68
|
|
|
2. Set the four most significant bits (bits 12 through 15) of |
|
69
|
|
|
the time_hi_and_version field to the 4-bit version number |
|
70
|
|
|
from Section 4.1.3. |
|
71
|
|
|
|
|
72
|
|
|
3. Set all the other bits to randomly (or pseudo-randomly) |
|
73
|
|
|
chosen values. |
|
74
|
|
|
*/ |
|
75
|
1 |
|
$uuid = sprintf('%04x%04x-%04x-%04x-%02x%02x-%04x%04x%04x', |
|
76
|
|
|
// time_low |
|
77
|
1 |
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
78
|
|
|
// time_mid |
|
79
|
1 |
|
mt_rand(0, 0xffff), |
|
80
|
|
|
// time_hi_and_version |
|
81
|
1 |
|
mt_rand(0, 0x0fff) | 0x4000, |
|
82
|
|
|
// clk_seq_hi_res |
|
83
|
1 |
|
mt_rand(0, 0x3f) | 0x80, |
|
84
|
|
|
// clk_seq_low |
|
85
|
1 |
|
mt_rand(0, 0xff), |
|
86
|
|
|
// node |
|
87
|
1 |
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
88
|
1 |
|
return new self($uuid); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get UUIDv4 in canonical form. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function canonical(): string |
|
97
|
|
|
{ |
|
98
|
2 |
|
return $this->_uuid; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|