|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Koded package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mihail Binev <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Please view the LICENSE distributed with this source code |
|
9
|
|
|
* for the full copyright and license information. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Koded\Stdlib; |
|
13
|
|
|
|
|
14
|
|
|
use AssertionError; |
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
use function base64_decode; |
|
17
|
|
|
use function base64_encode; |
|
18
|
|
|
use function chr; |
|
19
|
|
|
use function ctype_digit; |
|
20
|
|
|
use function ctype_xdigit; |
|
21
|
|
|
use function dechex; |
|
22
|
|
|
use function explode; |
|
23
|
|
|
use function gethostbyname; |
|
24
|
|
|
use function gettimeofday; |
|
25
|
|
|
use function hex2bin; |
|
26
|
|
|
use function hexdec; |
|
27
|
|
|
use function in_array; |
|
28
|
|
|
use function md5; |
|
29
|
|
|
use function mt_rand; |
|
30
|
|
|
use function preg_match; |
|
31
|
|
|
use function random_bytes; |
|
32
|
|
|
use function sha1; |
|
33
|
|
|
use function sprintf; |
|
34
|
|
|
use function str_replace; |
|
35
|
|
|
use function strlen; |
|
36
|
|
|
use function strtolower; |
|
37
|
|
|
use function substr; |
|
38
|
|
|
use function trim; |
|
39
|
|
|
use function unpack; |
|
40
|
|
|
use function vsprintf; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Class UUID generates Universally Unique Identifiers following the RFC 4122. |
|
44
|
|
|
* |
|
45
|
|
|
* The 5 fields of the UUID v1 |
|
46
|
|
|
* - 32 bit, *time_low* |
|
47
|
|
|
* - 16 bit, *time_mid* |
|
48
|
|
|
* - 16 bit, *time_high_and_version* |
|
49
|
|
|
* - 16 bit, (8 bits for *clock_seq_and_reserved* + 8 bits for *clock_seq_low*) |
|
50
|
|
|
* - 48 bit, *node* |
|
51
|
|
|
* |
|
52
|
|
|
* @link http://tools.ietf.org/html/rfc4122 |
|
53
|
|
|
* @link https://docs.python.org/2/library/uuid.html |
|
54
|
|
|
* @link https://en.wikipedia.org/wiki/Universally_unique_identifier |
|
55
|
|
|
*/ |
|
56
|
|
|
final class UUID |
|
57
|
|
|
{ |
|
58
|
|
|
/* @link http://tools.ietf.org/html/rfc4122#appendix-C */ |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* When this namespace is specified, the name string |
|
62
|
|
|
* is a fully-qualified domain name. |
|
63
|
|
|
*/ |
|
64
|
|
|
public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* When this namespace is specified, the name string is a URL. |
|
68
|
|
|
*/ |
|
69
|
|
|
public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* When this namespace is specified, the name string is an ISO OID. |
|
73
|
|
|
*/ |
|
74
|
|
|
public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* When this namespace is specified, the name string is |
|
78
|
|
|
* an X.500 DN in DER or a text output format. |
|
79
|
|
|
*/ |
|
80
|
|
|
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Regex pattern for UUIDs |
|
84
|
|
|
*/ |
|
85
|
|
|
public const PATTERN = '[a-f0-9]{8}\-[a-f0-9]{4}\-[1|3|4|5][a-f0-9]{3}\-[a-f0-9]{4}\-[a-f0-9]{12}'; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Generates a UUID based on the MD5 hash of a namespace |
|
89
|
|
|
* identifier (which is a UUID) and a name (which is a string). |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $namespace UUID namespace identifier |
|
92
|
|
|
* @param string $name A name |
|
93
|
|
|
* |
|
94
|
|
|
* @return string UUID v3 |
|
95
|
|
|
*/ |
|
96
|
5 |
|
public static function v3(string $namespace, string $name): string |
|
97
|
|
|
{ |
|
98
|
5 |
|
return UUID::fromName($namespace, $name, 3); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Version 4, pseudo-random UUID |
|
103
|
|
|
* xxxxxxxx-xxxx-4xxx-[8|9|a|b]xxx-xxxxxxxxxxxx |
|
104
|
|
|
* |
|
105
|
|
|
* @return string 128bit of pseudo-random UUID |
|
106
|
|
|
* @throws \Exception |
|
107
|
|
|
*@see http://en.wikipedia.org/wiki/UUID#Version_4_.28random.29 |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public static function v4(): string |
|
110
|
|
|
{ |
|
111
|
2 |
|
$bytes = unpack('n*', random_bytes(16)); |
|
112
|
2 |
|
$bytes[4] = $bytes[4] & 0x0fff | 0x4000; |
|
113
|
2 |
|
$bytes[5] = $bytes[5] & 0x3fff | 0x8000; |
|
114
|
2 |
|
return vsprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', $bytes); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Generates a UUID based on the SHA-1 hash of a namespace |
|
119
|
|
|
* identifier (which is a UUID) and a name (which is a string). |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $namespace UUID namespace identifier |
|
122
|
|
|
* @param string $name A name |
|
123
|
|
|
* |
|
124
|
|
|
* @return string UUID v5 |
|
125
|
|
|
*/ |
|
126
|
4 |
|
public static function v5(string $namespace, string $name): string |
|
127
|
|
|
{ |
|
128
|
4 |
|
return UUID::fromName($namespace, $name, 5); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Checks if a given UUID has valid format. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $uuid |
|
135
|
|
|
* |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
23 |
|
public static function valid(string $uuid): bool |
|
139
|
|
|
{ |
|
140
|
23 |
|
if (false === (bool)preg_match('/^' . UUID::PATTERN . '$/i', $uuid)) { |
|
141
|
5 |
|
return false; |
|
142
|
|
|
} |
|
143
|
19 |
|
if ('4' === $uuid[14]) { |
|
144
|
9 |
|
return in_array($uuid[19], ['8', '9', 'a', 'b']); |
|
145
|
|
|
} |
|
146
|
10 |
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Checks if a given UUID has valid format and matches against the version. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $uuid |
|
153
|
|
|
* @param int $version Check against the version 1, 3, 4 or 5 |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
13 |
|
public static function matches(string $uuid, int $version = 4): bool |
|
158
|
|
|
{ |
|
159
|
13 |
|
assert(in_array($version, [1, 3, 4, 5]), new AssertionError("Expected UUID version 1, 3, 4 or 5 (got $version)")); |
|
160
|
12 |
|
return UUID::valid($uuid); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* UUID v1 is generated from host (hardware) address, clock sequence and |
|
165
|
|
|
* current time. This is very slow method. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string|int|null $address [optional] 48 bit number for the hardware address. |
|
168
|
|
|
* It can be an integer or hexadecimal string |
|
169
|
|
|
* |
|
170
|
|
|
* @return string UUID v1 |
|
171
|
|
|
*/ |
|
172
|
7 |
|
public static function v1(string|int $address = null): string |
|
173
|
|
|
{ |
|
174
|
|
|
static $node, $clockSeq, $lastTimestamp; |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* If $node is not initialized, it will try to |
|
178
|
|
|
* get the network address of the machine, |
|
179
|
|
|
* or fallback to random generated hex string. |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
7 |
|
$fetchAddress = static function() use (&$node): string { |
|
183
|
1 |
|
if ($node) { |
|
184
|
|
|
return $node; |
|
185
|
|
|
} |
|
186
|
1 |
|
if ($node = `hostname -i 2> /dev/null`) { |
|
187
|
1 |
|
return $node = vsprintf('%02x%02x%02x%02x', explode('.', $node)); |
|
188
|
|
|
} |
|
189
|
|
|
if ($node = `hostname 2> /dev/null`) { |
|
190
|
|
|
$node = gethostbyname(trim($node)); |
|
191
|
|
|
return $node = vsprintf('%02x%02x%02x%02x', explode('.', $node)); |
|
192
|
|
|
} |
|
193
|
|
|
// Cannot identify IP or host, fallback as described in |
|
194
|
|
|
// http://tools.ietf.org/html/rfc4122#section-4.5 |
|
195
|
|
|
// https://en.wikipedia.org/wiki/MAC_address#Unicast_vs._multicast_(I/G_bit) |
|
196
|
|
|
// @codeCoverageIgnoreStart |
|
197
|
|
|
return $node = dechex(mt_rand(0, 1 << 48) | (1 << 40)); |
|
198
|
|
|
// @codeCoverageIgnoreEnd |
|
199
|
|
|
}; |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Transform the address into hexadecimal string |
|
203
|
|
|
* as spatially unique node identifier. |
|
204
|
|
|
* @param string|int|null $address [optional] |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
7 |
|
$nodeIdentifier = static function(string|int $address = null) use ($fetchAddress): string { |
|
208
|
7 |
|
$address = null !== $address |
|
209
|
6 |
|
? str_replace([':', '-', '.'], '', (string)$address) |
|
210
|
1 |
|
: $fetchAddress(); |
|
211
|
|
|
|
|
212
|
7 |
|
if (ctype_digit($address)) { |
|
213
|
2 |
|
return sprintf('%012x', $address); |
|
214
|
|
|
} |
|
215
|
5 |
|
if (ctype_xdigit($address) && strlen($address) <= 12) { |
|
216
|
2 |
|
return strtolower($address); |
|
217
|
|
|
} |
|
218
|
3 |
|
throw new InvalidArgumentException('UUID invalid node value'); |
|
219
|
|
|
}; |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Convert UNIX epoch in nanoseconds to Gregorian epoch |
|
223
|
|
|
* (15/10/1582 00:00:00 - 01/01/1970 00:00:00) |
|
224
|
|
|
* @return int[] |
|
225
|
|
|
*/ |
|
226
|
7 |
|
$fromUnixNano = static function() use (&$lastTimestamp) { |
|
227
|
7 |
|
$ts = gettimeofday(); |
|
228
|
7 |
|
$ts = ($ts['sec'] * 10000000) + ($ts['usec'] * 10) + 0x01b21dd213814000; |
|
229
|
7 |
|
if ($lastTimestamp && $ts <= $lastTimestamp) { |
|
230
|
|
|
$ts = $lastTimestamp + 1; |
|
231
|
|
|
} |
|
232
|
7 |
|
$lastTimestamp = $ts; |
|
233
|
|
|
return [ |
|
234
|
|
|
// timestamp low field |
|
235
|
7 |
|
$ts & 0xffffffff, |
|
236
|
|
|
// timestamp middle field |
|
237
|
7 |
|
($ts >> 32) & 0xffff, |
|
238
|
|
|
// timestamp high field with version number |
|
239
|
|
|
(($ts >> 48) & 0x0fff) | (1 << 12) |
|
240
|
|
|
]; |
|
241
|
|
|
}; |
|
242
|
|
|
|
|
243
|
7 |
|
if (!$clockSeq) { |
|
244
|
|
|
// Random 14-bit sequence number |
|
245
|
|
|
// http://tools.ietf.org/html/rfc4122#section-4.2.1.1 |
|
246
|
1 |
|
$clockSeq = mt_rand(0, 1 << 14); |
|
247
|
|
|
} |
|
248
|
7 |
|
return vsprintf('%08x-%04x-%04x-%02x%02x-%012s', [ |
|
249
|
7 |
|
...$fromUnixNano(), |
|
250
|
7 |
|
$clockSeq & 0xff, |
|
251
|
7 |
|
($clockSeq >> 8) & 0x3f, |
|
252
|
7 |
|
$nodeIdentifier($address) |
|
253
|
|
|
]); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Creates a base64 string out of the UUID. |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $uuid UUID string |
|
260
|
|
|
* |
|
261
|
|
|
* @return string base64 encoded string |
|
262
|
|
|
*/ |
|
263
|
4 |
|
public static function toBase64(string $uuid): string |
|
264
|
|
|
{ |
|
265
|
4 |
|
if (false === UUID::valid($uuid)) { |
|
266
|
1 |
|
throw new InvalidArgumentException('Invalid UUID ' . $uuid); |
|
267
|
|
|
} |
|
268
|
3 |
|
return str_replace(['/', '+', '='], ['-', '_', ''], |
|
269
|
3 |
|
base64_encode(hex2bin(str_replace('-', '', $uuid))) |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Converts a base64 string to UUID. |
|
275
|
|
|
* |
|
276
|
|
|
* @param string $base64 |
|
277
|
|
|
* |
|
278
|
|
|
* @return string UUID string |
|
279
|
|
|
*/ |
|
280
|
7 |
|
public static function fromBase64(string $base64): string |
|
281
|
|
|
{ |
|
282
|
7 |
|
$uuid = base64_decode(str_replace( |
|
283
|
7 |
|
['-', '_', '='], ['/', '+', ''], $base64) . '==' |
|
284
|
|
|
); |
|
285
|
7 |
|
if (!preg_match('//u', $uuid)) { |
|
286
|
3 |
|
$uuid = vsprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', unpack('n*', $uuid)); |
|
287
|
|
|
} |
|
288
|
7 |
|
if (UUID::valid($uuid)) { |
|
289
|
6 |
|
return $uuid; |
|
290
|
|
|
} |
|
291
|
1 |
|
throw new InvalidArgumentException( |
|
292
|
|
|
'Failed to convert base 64 string to UUID'); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Creates a v3 or v5 UUID. |
|
297
|
|
|
* |
|
298
|
|
|
* @param string $namespace UUID namespace identifier (see UUID constants) |
|
299
|
|
|
* @param string $name A name |
|
300
|
|
|
* @param int $version 3 or 5 |
|
301
|
|
|
* |
|
302
|
|
|
* @throws InvalidArgumentException |
|
303
|
|
|
* @return string UUID 3 or 5 |
|
304
|
|
|
*/ |
|
305
|
9 |
|
private static function fromName(string $namespace, string $name, int $version): string |
|
306
|
|
|
{ |
|
307
|
9 |
|
if (false === UUID::matches($namespace, $version)) { |
|
308
|
2 |
|
throw new InvalidArgumentException('Invalid UUID namespace ' . $namespace); |
|
309
|
|
|
} |
|
310
|
7 |
|
$hex = str_replace('-', '', $namespace); |
|
311
|
7 |
|
$bits = ''; |
|
312
|
7 |
|
for ($i = 0, $len = strlen($hex); $i < $len; $i += 2) { |
|
313
|
7 |
|
$bits .= chr((int)hexdec($hex[$i] . $hex[$i + 1])); |
|
314
|
|
|
} |
|
315
|
7 |
|
$hash = $bits . $name; |
|
316
|
7 |
|
$hash = (3 === $version) ? md5($hash) : sha1($hash); |
|
317
|
7 |
|
return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
318
|
7 |
|
substr($hash, 0, 8), |
|
319
|
7 |
|
substr($hash, 8, 4), |
|
320
|
7 |
|
(hexdec(substr($hash, 12, 4)) & 0x0fff) | (3 === $version ? 0x3000 : 0x5000), |
|
321
|
7 |
|
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
322
|
7 |
|
substr($hash, 20, 12) |
|
323
|
|
|
); |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
|
|
|