Rev 41 | Rev 43 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * UUID utils for PHP |
||
15 | daniel-mar | 5 | * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft |
38 | daniel-mar | 6 | * Version 2023-07-13 |
2 | daniel-mar | 7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
9 | * you may not use this file except in compliance with the License. |
||
10 | * You may obtain a copy of the License at |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
21 | # This library requires either the GMP extension (or BCMath if gmp_supplement.inc.php is present) |
||
38 | daniel-mar | 22 | // TODO: If we are on 64 bit PHP (PHP_INT_SIZE > 4), then replace GMP with normal PHP operations |
2 | daniel-mar | 23 | |
42 | daniel-mar | 24 | if (file_exists(__DIR__ . '/mac_utils.inc.phps')) include_once __DIR__ . '/mac_utils.inc.phps'; |
25 | if (file_exists(__DIR__ . '/mac_utils.inc.php')) include_once __DIR__ . '/mac_utils.inc.php'; |
||
26 | |||
2 | daniel-mar | 27 | if (file_exists(__DIR__ . '/gmp_supplement.inc.php')) include_once __DIR__ . '/gmp_supplement.inc.php'; |
28 | |||
31 | daniel-mar | 29 | const UUID_NAMEBASED_NS_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; // FQDN |
30 | const UUID_NAMEBASED_NS_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
||
31 | const UUID_NAMEBASED_NS_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
||
36 | daniel-mar | 32 | const UUID_NAMEBASED_NS_X500_DN = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; // "DER or text encoding" according to RFC4122bis |
2 | daniel-mar | 33 | |
41 | daniel-mar | 34 | if (!function_exists('_random_int')) { |
35 | function _random_int($min, $max) { |
||
36 | // This function tries a CSRNG and falls back to a RNG if no CSRNG is available |
||
37 | try { |
||
38 | return random_int($min, $max); |
||
39 | } catch (Exception $e) { |
||
40 | return mt_rand($min, $max); |
||
41 | } |
||
24 | daniel-mar | 42 | } |
43 | } |
||
44 | |||
2 | daniel-mar | 45 | function uuid_valid($uuid) { |
46 | $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
||
47 | $uuid = strtoupper($uuid); |
||
48 | #$uuid = trim($uuid); |
||
49 | |||
50 | if (strlen($uuid) != 32) return false; |
||
51 | |||
29 | daniel-mar | 52 | $uuid = preg_replace('@[0-9A-F]@i', '', $uuid); |
2 | daniel-mar | 53 | |
54 | return ($uuid == ''); |
||
55 | } |
||
56 | |||
28 | daniel-mar | 57 | function uuid_info($uuid, $echo=true) { |
2 | daniel-mar | 58 | if (!uuid_valid($uuid)) return false; |
59 | |||
28 | daniel-mar | 60 | if (!$echo) ob_start(); |
61 | |||
2 | daniel-mar | 62 | #$uuid = trim($uuid); |
63 | # $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
||
29 | daniel-mar | 64 | $uuid = strtolower($uuid); |
65 | $uuid = preg_replace('@[^0-9A-F]@i', '', $uuid); |
||
2 | daniel-mar | 66 | |
67 | $x = hexdec(substr($uuid, 16, 1)); |
||
28 | daniel-mar | 68 | if ($x >= 14 /* 0b1110 */) $variant = 3; |
69 | else if ($x >= 12 /* 0b110_ */) $variant = 2; |
||
70 | else if ($x >= 8 /* 0b10__ */) $variant = 1; |
||
71 | else if ($x >= 0 /* 0b0___ */) $variant = 0; |
||
2 | daniel-mar | 72 | else $variant = -1; // should not happen |
73 | |||
35 | daniel-mar | 74 | if ($uuid == '00000000000000000000000000000000') { |
75 | echo sprintf("%-32s %s\n", "Special Use:", "Nil UUID"); |
||
76 | echo "\n"; |
||
77 | } |
||
78 | else if ($uuid == 'ffffffffffffffffffffffffffffffff') { |
||
79 | echo sprintf("%-32s %s\n", "Special Use:", "Max UUID"); |
||
80 | echo "\n"; |
||
81 | } |
||
82 | |||
2 | daniel-mar | 83 | switch ($variant) { |
84 | case 0: |
||
33 | daniel-mar | 85 | echo sprintf("%-32s %s\n", "Variant:", "[0b0__] Network Computing System (NCS)"); |
2 | daniel-mar | 86 | |
87 | /* |
||
88 | * Internal structure of variant #0 UUIDs |
||
89 | * |
||
90 | * The first 6 octets are the number of 4 usec units of time that have |
||
91 | * passed since 1/1/80 0000 GMT. The next 2 octets are reserved for |
||
92 | * future use. The next octet is an address family. The next 7 octets |
||
93 | * are a host ID in the form allowed by the specified address family. |
||
94 | * |
||
95 | * Note that while the family field (octet 8) was originally conceived |
||
96 | * of as being able to hold values in the range [0..255], only [0..13] |
||
97 | * were ever used. Thus, the 2 MSB of this field are always 0 and are |
||
98 | * used to distinguish old and current UUID forms. |
||
99 | */ |
||
100 | |||
28 | daniel-mar | 101 | /* |
102 | Variant 0 UUID |
||
103 | - 32 bit High Time |
||
104 | - 16 bit Low Time |
||
105 | - 16 bit Reserved |
||
29 | daniel-mar | 106 | - 1 bit Variant (fix 0b0) |
28 | daniel-mar | 107 | - 7 bit Family |
108 | - 56 bit Node |
||
109 | */ |
||
110 | |||
2 | daniel-mar | 111 | // Example of an UUID: 333a2276-0000-0000-0d00-00809c000000 |
112 | |||
35 | daniel-mar | 113 | // TODO: also show legacy format, e.g. 458487b55160.02.c0.64.02.03.00.00.00 |
114 | |||
28 | daniel-mar | 115 | # see also some notes at See https://github.com/cjsv/uuid/blob/master/Doc |
2 | daniel-mar | 116 | |
29 | daniel-mar | 117 | /* |
118 | NOTE: A generator is not possible, because there are no timestamps left! |
||
119 | The last possible timestamp was: |
||
30 | daniel-mar | 120 | [0xFFFFFFFFFFFF] 2015-09-05 05:58:26'210655 GMT |
29 | daniel-mar | 121 | That is in the following UUID: |
122 | ffffffff-ffff-0000-027f-000001000000 |
||
123 | Current timestamp generator: |
||
124 | echo dechex(round((microtime(true)+315532800)*250000)); |
||
125 | */ |
||
27 | daniel-mar | 126 | |
2 | daniel-mar | 127 | # Timestamp: Count of 4us intervals since 01 Jan 1980 00:00:00 GMT |
128 | # 1/0,000004 = 250000 |
||
129 | # Seconds between 1970 and 1980 : 315532800 |
||
130 | # 250000*315532800=78883200000000 |
||
131 | $timestamp = substr($uuid, 0, 12); |
||
132 | $ts = gmp_init($timestamp, 16); |
||
30 | daniel-mar | 133 | $ts = gmp_add($ts, gmp_init("78883200000000", 10)); |
134 | $ms = gmp_mod($ts, gmp_init("250000", 10)); |
||
135 | $ts = gmp_div($ts, gmp_init("250000", 10)); |
||
136 | $ts = gmp_strval($ts, 10); |
||
137 | $ms = gmp_strval($ms, 10); |
||
138 | $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 6/*us*/, '0', STR_PAD_LEFT).' GMT'; |
||
25 | daniel-mar | 139 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts"); |
2 | daniel-mar | 140 | |
141 | $reserved = substr($uuid, 12, 4); |
||
27 | daniel-mar | 142 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$reserved]"); |
2 | daniel-mar | 143 | |
144 | $family_hex = substr($uuid, 16, 2); |
||
145 | $family_dec = hexdec($family_hex); |
||
28 | daniel-mar | 146 | $nodeid_hex = substr($uuid, 18, 14); |
147 | $nodeid_dec = hexdec($nodeid_hex); |
||
37 | daniel-mar | 148 | |
149 | // Sources: |
||
150 | // - https://bitsavers.org/pdf/ibm/rs6000/aix_3.0/SC23-2206-0_AIX_Version_3_for_RS6000_Communications_Programming_Concepts_199003.pdf |
||
151 | // - (For comparison) https://github.com/uuid6/uuid6-ietf-draft/issues/26#issuecomment-1062164457 |
||
152 | // - (For comparison) https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.addressfamily?view=net-7.0 [numbers 0..13 are mostly identical] |
||
153 | |||
36 | daniel-mar | 154 | if ($family_dec == 0) { |
37 | daniel-mar | 155 | # Microsoft's AdressFamily: Unspecified 0 Unspecified address family. |
156 | # AIX 3.0 Manual: 0 unspec = Unspecified |
||
157 | $family_name = 'socket_$unspec (Unspecified)'; |
||
36 | daniel-mar | 158 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
159 | } |
||
160 | else if ($family_dec == 1) { |
||
37 | daniel-mar | 161 | # Microsoft's AdressFamily: Unix 1 Unix local to host address. |
162 | # AIX 3.0 Manual: 1 unix = Local to host (pipes, portals) |
||
163 | $family_name = 'socket_$unix (Local to host, e.g. pipes, portals)'; |
||
36 | daniel-mar | 164 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
165 | } |
||
166 | else if ($family_dec == 2) { |
||
37 | daniel-mar | 167 | # Microsoft's AdressFamily: InterNetwork 2 Address for IP version 4. |
168 | # AIX 3.0 Manual: 2 ip = Internet Protocols |
||
40 | daniel-mar | 169 | $family_name = 'socket_$internet (Internet Protocols, e.g. IPv4)'; |
28 | daniel-mar | 170 | // https://www.ibm.com/docs/en/aix/7.1?topic=u-uuid-gen-command-ncs (AIX 7.1) shows the following example output for /etc/ncs/uuid_gen -P |
171 | // := [ |
||
172 | // time_high := 16#458487df, |
||
173 | // time_low := 16#9fb2, |
||
174 | // reserved := 16#000, |
||
175 | // family := chr(16#02), |
||
176 | // host := [chr(16#c0), chr(16#64), chr(16#02), chr(16#03), |
||
177 | // chr(16#00), chr(16#00), chr(16#00)] |
||
178 | // ] |
||
179 | // This means that the IP address is 32 bits hex, and 32 bits are unused |
||
180 | $nodeid_desc = hexdec(substr($nodeid_hex,0,2)).'.'. |
||
181 | hexdec(substr($nodeid_hex,2,2)).'.'. |
||
182 | hexdec(substr($nodeid_hex,4,2)).'.'. |
||
183 | hexdec(substr($nodeid_hex,6,2)); |
||
184 | $rest = substr($nodeid_hex,8,6); |
||
185 | if ($rest != '000000') $nodeid_desc .= " + unexpected rest 0x$rest"; |
||
36 | daniel-mar | 186 | } |
187 | else if ($family_dec == 3) { |
||
37 | daniel-mar | 188 | # Microsoft's AdressFamily: ImpLink 3 ARPANET IMP address. |
189 | # AIX 3.0 Manual: 3 implink = ARPANET imp addresses |
||
190 | $family_name = 'socket_$implink (ARPANET imp addresses)'; |
||
36 | daniel-mar | 191 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
192 | } |
||
193 | else if ($family_dec == 4) { |
||
37 | daniel-mar | 194 | # Microsoft's AdressFamily: Pup 4 Address for PUP protocols. |
195 | # AIX 3.0 Manual: 4 pup = Pup protocols (for example, BSP) |
||
196 | $family_name = 'socket_$pup (Pup protocols, e.g. BSP)'; |
||
36 | daniel-mar | 197 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
198 | } |
||
199 | else if ($family_dec == 5) { |
||
37 | daniel-mar | 200 | # Microsoft's AdressFamily: Chaos 5 Address for MIT CHAOS protocols. |
201 | # AIX 3.0 Manual: 5 chaos = MIT CHAOS protocols |
||
202 | $family_name = 'socket_$chaos (MIT CHAOS protocols)'; |
||
36 | daniel-mar | 203 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
204 | } |
||
205 | else if ($family_dec == 6) { |
||
37 | daniel-mar | 206 | # Microsoft's AdressFamily: NS 6 Address for Xerox NS protocols. |
207 | # Microsoft's AdressFamily: Ipx 6 IPX or SPX address. |
||
208 | # AIX 3.0 Manual: 6 ns = XEROX NS protocols |
||
209 | $family_name = 'socket_$ns (XEROX NS protocols)'; |
||
36 | daniel-mar | 210 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
211 | } |
||
212 | else if ($family_dec == 7) { |
||
37 | daniel-mar | 213 | # Microsoft's AdressFamily: Osi 7 Address for OSI protocols. |
214 | # Microsoft's AdressFamily: Iso 7 Address for ISO protocols. |
||
215 | # AIX 3.0 Manual: 7 nbs = NBS protocols |
||
216 | $family_name = 'socket_$nbs (NBS protocols)'; |
||
36 | daniel-mar | 217 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
218 | } |
||
219 | else if ($family_dec == 8) { |
||
37 | daniel-mar | 220 | # Microsoft's AdressFamily: Ecma 8 European Computer Manufacturers Association (ECMA) address. |
221 | # AIX 3.0 Manual: 8 ecma = European computer manufacturers |
||
222 | $family_name = 'socket_$ecma (European computer manufacturers protocols)'; |
||
36 | daniel-mar | 223 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
224 | } |
||
225 | else if ($family_dec == 9) { |
||
37 | daniel-mar | 226 | # Microsoft's AdressFamily: DataKit 9 Address for Datakit protocols. |
227 | # AIX 3.0 Manual: 9 datakit = Datakit protocols |
||
228 | $family_name = 'socket_$datakit (Datakit protocols)'; |
||
36 | daniel-mar | 229 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
230 | } |
||
231 | else if ($family_dec == 10) { |
||
37 | daniel-mar | 232 | # Microsoft's AdressFamily: Ccitt 10 Addresses for CCITT protocols, such as X.25. |
233 | # AIX 3.0 Manual: A ccitt = CCITT protocols (for example, X.25) |
||
234 | $family_name = 'socket_$ccitt (CCITT protocols, e.g. X.25)'; |
||
36 | daniel-mar | 235 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
236 | } |
||
237 | else if ($family_dec == 11) { |
||
37 | daniel-mar | 238 | # Microsoft's AdressFamily: Sna 11 IBM SNA address. |
239 | # AIX 3.0 Manual: B sna = IBM SNA |
||
240 | $family_name = 'socket_$sna (IBM SNA)'; |
||
36 | daniel-mar | 241 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
242 | } |
||
243 | else if ($family_dec == 12) { |
||
37 | daniel-mar | 244 | # Microsoft's AdressFamily: DecNet 12 DECnet address. |
245 | # AIX 3.0 Manual: C unspec2 = Unspecified |
||
246 | $family_name = 'socket_$unspec2 (Unspecified)'; |
||
36 | daniel-mar | 247 | $nodeid_desc = ''; // TODO: how to interprete the Node-ID of that family? |
248 | } |
||
249 | else if ($family_dec == 13) { |
||
37 | daniel-mar | 250 | # Microsoft's AdressFamily: DataLink 13 Direct data-link interface address. |
251 | # AIX 3.0 Manual: D dds = Domain DDS protocol |
||
252 | # Some also call this "Data Link" ... Is that correct? |
||
253 | $family_name = 'socket_$dds (Domain DDS protocol)'; |
||
28 | daniel-mar | 254 | // https://www.ibm.com/docs/en/aix/7.1?topic=u-uuid-gen-command-ncs (AIX 7.1) shows the following example output for /etc/ncs/uuid_gen -C |
255 | // = { 0x34dc23af, |
||
256 | // 0xf000, |
||
257 | // 0x0000, |
||
258 | // 0x0d, |
||
259 | // {0x00, 0x00, 0x7c, 0x5f, 0x00, 0x00, 0x00} }; |
||
260 | // https://github.com/cjsv/uuid/blob/master/Doc writes: |
||
261 | // "Family 13 (dds) looks like node is 00 | nnnnnn 000000." |
||
262 | |||
263 | $nodeid_desc = ''; |
||
264 | |||
265 | $start = substr($nodeid_hex,0,2); |
||
266 | if ($start != '00') $nodeid_desc .= "unexpected start 0x$start + "; |
||
267 | |||
268 | $nodeid_desc .= ($nodeid_dec >> 24) & 0xFFFFFF; |
||
269 | |||
270 | $rest = substr($nodeid_hex,8,6); |
||
271 | if ($rest != '000000') $nodeid_desc .= " + unexpected rest 0x$rest"; |
||
2 | daniel-mar | 272 | } else { |
29 | daniel-mar | 273 | $family_name = "Unknown (Family $family_dec)"; # There are probably no more families |
274 | $nodeid_desc = "Unknown"; |
||
2 | daniel-mar | 275 | } |
29 | daniel-mar | 276 | echo sprintf("%-32s %s\n", "Family:", "[0x$family_hex] $family_name"); |
2 | daniel-mar | 277 | |
28 | daniel-mar | 278 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$nodeid_hex] $nodeid_desc"); |
2 | daniel-mar | 279 | |
280 | break; |
||
281 | case 1: |
||
35 | daniel-mar | 282 | // TODO: Show byte order: 00112233-4455-6677-8899-aabbccddeeff => 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff |
283 | |||
30 | daniel-mar | 284 | $version = hexdec(substr($uuid, 12, 1)); |
2 | daniel-mar | 285 | |
30 | daniel-mar | 286 | if ($version <= 2) { |
287 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 (Leach-Mealling-Salz) / DCE 1.1"); |
||
288 | } else if (($version >= 3) && ($version <= 5)) { |
||
289 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 (Leach-Mealling-Salz)"); |
||
290 | } else if (($version >= 6) && ($version <= 8)) { |
||
291 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122bis (Leach-Mealling-Peabody-Davis)"); |
||
292 | } else { |
||
293 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 ?"); |
||
294 | } |
||
295 | |||
2 | daniel-mar | 296 | switch ($version) { |
29 | daniel-mar | 297 | case 6: |
298 | /* |
||
299 | Variant 1, Version 6 UUID |
||
300 | - 48 bit High Time |
||
301 | - 4 bit Version (fix 0x6) |
||
302 | - 12 bit Low Time |
||
303 | - 2 bit Variant (fix 0b10) |
||
35 | daniel-mar | 304 | - 6 bit Clock Sequence High |
305 | - 8 bit Clock Sequence Low |
||
29 | daniel-mar | 306 | - 48 bit MAC Address |
307 | */ |
||
31 | daniel-mar | 308 | echo sprintf("%-32s %s\n", "Version:", "[6] Reordered Time"); |
29 | daniel-mar | 309 | $uuid = substr($uuid, 0, 8).'-'. |
310 | substr($uuid, 8, 4).'-'. |
||
311 | substr($uuid, 12, 4).'-'. |
||
312 | substr($uuid, 16, 4).'-'. |
||
313 | substr($uuid, 20, 12); |
||
314 | $uuid = uuid6_to_uuid1($uuid); |
||
315 | $uuid = str_replace('-', '', $uuid); |
||
316 | |||
317 | /* fallthrough */ |
||
2 | daniel-mar | 318 | case 1: |
27 | daniel-mar | 319 | /* |
320 | Variant 1, Version 1 UUID |
||
321 | - 32 bit Low Time |
||
322 | - 16 bit Mid Time |
||
323 | - 4 bit Version (fix 0x1) |
||
324 | - 12 bit High Time |
||
28 | daniel-mar | 325 | - 2 bit Variant (fix 0b10) |
35 | daniel-mar | 326 | - 6 bit Clock Sequence High |
327 | - 8 bit Clock Sequence Low |
||
27 | daniel-mar | 328 | - 48 bit MAC Address |
329 | */ |
||
330 | |||
31 | daniel-mar | 331 | if ($version == 1) echo sprintf("%-32s %s\n", "Version:", "[1] Time-based with unique host identifier"); |
2 | daniel-mar | 332 | |
333 | # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00 |
||
334 | # 1/0,0000001 = 10000000 |
||
335 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).substr($uuid, 0, 8); |
||
336 | $ts = gmp_init($timestamp, 16); |
||
30 | daniel-mar | 337 | $ts = gmp_sub($ts, gmp_init("122192928000000000", 10)); |
338 | $ms = gmp_mod($ts, gmp_init("10000000", 10)); |
||
339 | $ts = gmp_div($ts, gmp_init("10000000", 10)); |
||
340 | $ts = gmp_strval($ts, 10); |
||
341 | $ms = gmp_strval($ms, 10); |
||
342 | $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7/*0.1us*/, '0', STR_PAD_LEFT).' GMT'; |
||
25 | daniel-mar | 343 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts"); |
2 | daniel-mar | 344 | |
345 | $x = hexdec(substr($uuid, 16, 4)); |
||
346 | $dec = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x) |
||
347 | $hex = substr($uuid, 16, 4); |
||
25 | daniel-mar | 348 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
2 | daniel-mar | 349 | |
350 | $x = substr($uuid, 20, 12); |
||
351 | $nodeid = ''; |
||
352 | for ($i=0; $i<6; $i++) { |
||
353 | $nodeid .= substr($x, $i*2, 2); |
||
25 | daniel-mar | 354 | if ($i != 5) $nodeid .= '-'; |
2 | daniel-mar | 355 | } |
30 | daniel-mar | 356 | $nodeid = strtoupper($nodeid); |
27 | daniel-mar | 357 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
2 | daniel-mar | 358 | |
42 | daniel-mar | 359 | echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n\n"; |
360 | decode_mac(strtoupper($nodeid)); |
||
2 | daniel-mar | 361 | |
362 | break; |
||
363 | case 2: |
||
27 | daniel-mar | 364 | /* |
365 | Variant 1, Version 2 UUID |
||
366 | - 32 bit Local Domain Number |
||
367 | - 16 bit Mid Time |
||
368 | - 4 bit Version (fix 0x2) |
||
369 | - 12 bit High Time |
||
28 | daniel-mar | 370 | - 2 bit Variant (fix 0b10) |
35 | daniel-mar | 371 | - 6 bit Clock Sequence |
28 | daniel-mar | 372 | - 8 bit Local Domain |
27 | daniel-mar | 373 | - 48 bit MAC Address |
374 | */ |
||
375 | |||
28 | daniel-mar | 376 | // see also https://unicorn-utterances.com/posts/what-happened-to-uuid-v2 |
377 | |||
25 | daniel-mar | 378 | echo sprintf("%-32s %s\n", "Version:", "[2] DCE Security version"); |
2 | daniel-mar | 379 | |
27 | daniel-mar | 380 | # The clock_seq_low field (which represents an integer in the range [0, 28-1]) is interpreted as a local domain (as represented by sec_rgy_domain_t; see sec_rgy_domain_t ); that is, an identifier domain meaningful to the local host. (Note that the data type sec_rgy_domain_t can potentially hold values outside the range [0, 28-1]; however, the only values currently registered are in the range [0, 2], so this type mismatch is not significant.) In the particular case of a POSIX host, the value sec_rgy_domain_person is to be interpreted as the "POSIX UID domain", and the value sec_rgy_domain_group is to be interpreted as the "POSIX GID domain". |
381 | $x = substr($uuid, 18, 2); |
||
382 | if ($x == '00') $domain_info = 'Person (POSIX: User-ID)'; |
||
383 | else if ($x == '01') $domain_info = 'Group (POSIX: Group-ID)'; |
||
384 | else if ($x == '02') $domain_info = 'Organization'; |
||
385 | else $domain_info = 'site-defined (Domain '.hexdec($x).')'; |
||
386 | echo sprintf("%-32s %s\n", "Local Domain:", "[0x$x] $domain_info"); |
||
387 | |||
2 | daniel-mar | 388 | # The time_low field (which represents an integer in the range [0, 232-1]) is interpreted as a local-ID; that is, an identifier (within the domain specified by clock_seq_low) meaningful to the local host. In the particular case of a POSIX host, when combined with a POSIX UID or POSIX GID domain in the clock_seq_low field (above), the time_low field represents a POSIX UID or POSIX GID, respectively. |
389 | $x = substr($uuid, 0, 8); |
||
29 | daniel-mar | 390 | $dec = hexdec($x); |
391 | echo sprintf("%-32s %s\n", "Local Domain Number:", "[0x$x] $dec"); |
||
2 | daniel-mar | 392 | |
393 | # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00 |
||
394 | # 1/0,0000001 = 10000000 |
||
395 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'00000000'; |
||
396 | $ts = gmp_init($timestamp, 16); |
||
30 | daniel-mar | 397 | $ts = gmp_sub($ts, gmp_init("122192928000000000", 10)); |
398 | $ms = gmp_mod($ts, gmp_init("10000000", 10)); |
||
399 | $ts = gmp_div($ts, gmp_init("10000000", 10)); |
||
400 | $ts = gmp_strval($ts, 10); |
||
401 | $ms = gmp_strval($ms, 10); |
||
402 | $ts_min = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7/*0.1us*/, '0', STR_PAD_LEFT).' GMT'; |
||
2 | daniel-mar | 403 | |
404 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'FFFFFFFF'; |
||
405 | $ts = gmp_init($timestamp, 16); |
||
30 | daniel-mar | 406 | $ts = gmp_sub($ts, gmp_init("122192928000000000", 10)); |
407 | $ms = gmp_mod($ts, gmp_init("10000000", 10)); |
||
408 | $ts = gmp_div($ts, gmp_init("10000000", 10)); |
||
409 | $ts = gmp_strval($ts, 10); |
||
410 | $ms = gmp_strval($ms, 10); |
||
411 | $ts_max = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7/*0.1us*/, '0', STR_PAD_LEFT).' GMT'; |
||
2 | daniel-mar | 412 | |
29 | daniel-mar | 413 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4)/*.'xxxxxxxx'*/; |
25 | daniel-mar | 414 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts_min - $ts_max"); |
2 | daniel-mar | 415 | |
28 | daniel-mar | 416 | $x = hexdec(substr($uuid, 16, 2)); |
417 | $dec = $x & 0x3F; // The highest 2 bits are used by "variant" (10xx) |
||
418 | $hex = substr($uuid, 16, 2); |
||
27 | daniel-mar | 419 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
2 | daniel-mar | 420 | |
421 | $x = substr($uuid, 20, 12); |
||
422 | $nodeid = ''; |
||
423 | for ($i=0; $i<6; $i++) { |
||
424 | $nodeid .= substr($x, $i*2, 2); |
||
25 | daniel-mar | 425 | if ($i != 5) $nodeid .= '-'; |
2 | daniel-mar | 426 | } |
30 | daniel-mar | 427 | $nodeid = strtoupper($nodeid); |
27 | daniel-mar | 428 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
2 | daniel-mar | 429 | |
42 | daniel-mar | 430 | echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n\n"; |
431 | decode_mac(strtoupper($nodeid)); |
||
2 | daniel-mar | 432 | |
433 | break; |
||
434 | case 3: |
||
28 | daniel-mar | 435 | /* |
436 | Variant 1, Version 3 UUID |
||
437 | - 48 bit Hash High |
||
29 | daniel-mar | 438 | - 4 bit Version (fix 0x3) |
28 | daniel-mar | 439 | - 12 bit Hash Mid |
440 | - 2 bit Variant (fix 0b10) |
||
441 | - 62 bit Hash Low |
||
442 | */ |
||
443 | |||
25 | daniel-mar | 444 | echo sprintf("%-32s %s\n", "Version:", "[3] Name-based (MD5 hash)"); |
2 | daniel-mar | 445 | |
446 | $hash = str_replace('-', '', strtolower($uuid)); |
||
27 | daniel-mar | 447 | |
2 | daniel-mar | 448 | $hash[12] = '?'; // was overwritten by version |
27 | daniel-mar | 449 | |
38 | daniel-mar | 450 | $var16a = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b0000)); |
451 | $var16b = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b0100)); |
||
452 | $var16c = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1000)); |
||
453 | $var16d = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1100)); |
||
2 | daniel-mar | 454 | $hash[16] = '?'; // was partially overwritten by variant |
455 | |||
27 | daniel-mar | 456 | echo sprintf("%-32s %s\n", "MD5(Namespace+Subject):", "[0x$hash]"); |
457 | echo sprintf("%-32s %s\n", "", " ^"); |
||
458 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
||
2 | daniel-mar | 459 | |
460 | break; |
||
461 | case 4: |
||
28 | daniel-mar | 462 | /* |
463 | Variant 1, Version 4 UUID |
||
464 | - 48 bit Random High |
||
29 | daniel-mar | 465 | - 4 bit Version (fix 0x4) |
28 | daniel-mar | 466 | - 12 bit Random Mid |
467 | - 2 bit Variant (fix 0b10) |
||
468 | - 62 bit Random Low |
||
469 | */ |
||
470 | |||
25 | daniel-mar | 471 | echo sprintf("%-32s %s\n", "Version:", "[4] Random"); |
2 | daniel-mar | 472 | |
25 | daniel-mar | 473 | $rand_line1 = ''; |
474 | $rand_line2 = ''; |
||
2 | daniel-mar | 475 | for ($i=0; $i<16; $i++) { |
476 | $bin = base_convert(substr($uuid, $i*2, 2), 16, 2); |
||
477 | $bin = str_pad($bin, 8, "0", STR_PAD_LEFT); |
||
478 | |||
479 | if ($i == 6) { |
||
25 | daniel-mar | 480 | // was overwritten by version |
481 | $bin[0] = '?'; |
||
482 | $bin[1] = '?'; |
||
483 | $bin[2] = '?'; |
||
484 | $bin[3] = '?'; |
||
2 | daniel-mar | 485 | } else if ($i == 8) { |
25 | daniel-mar | 486 | // was partially overwritten by variant |
487 | $bin[0] = '?'; |
||
488 | $bin[1] = '?'; |
||
2 | daniel-mar | 489 | } |
490 | |||
25 | daniel-mar | 491 | if ($i<8) $rand_line1 .= "$bin "; |
492 | if ($i>=8) $rand_line2 .= "$bin "; |
||
2 | daniel-mar | 493 | } |
25 | daniel-mar | 494 | echo sprintf("%-32s %s\n", "Random bits:", trim($rand_line1)); |
495 | echo sprintf("%-32s %s\n", "", trim($rand_line2)); |
||
2 | daniel-mar | 496 | |
27 | daniel-mar | 497 | $rand_bytes = str_replace('-', '', strtolower($uuid)); |
498 | $rand_bytes[12] = '?'; // was overwritten by version |
||
38 | daniel-mar | 499 | $var16a = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b0000)); |
500 | $var16b = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b0100)); |
||
501 | $var16c = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b1000)); |
||
502 | $var16d = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b1100)); |
||
27 | daniel-mar | 503 | $rand_bytes[16] = '?'; // was partially overwritten by variant |
504 | echo sprintf("%-32s %s\n", "Random bytes:", "[0x$rand_bytes]"); |
||
505 | echo sprintf("%-32s %s\n", "", " ^"); |
||
506 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
||
507 | |||
2 | daniel-mar | 508 | break; |
509 | case 5: |
||
28 | daniel-mar | 510 | /* |
511 | Variant 1, Version 5 UUID |
||
512 | - 48 bit Hash High |
||
29 | daniel-mar | 513 | - 4 bit Version (fix 0x5) |
28 | daniel-mar | 514 | - 12 bit Hash Mid |
515 | - 2 bit Variant (fix 0b10) |
||
516 | - 62 bit Hash Low |
||
517 | */ |
||
518 | |||
25 | daniel-mar | 519 | echo sprintf("%-32s %s\n", "Version:", "[5] Name-based (SHA-1 hash)"); |
2 | daniel-mar | 520 | |
521 | $hash = str_replace('-', '', strtolower($uuid)); |
||
27 | daniel-mar | 522 | |
2 | daniel-mar | 523 | $hash[12] = '?'; // was overwritten by version |
27 | daniel-mar | 524 | |
38 | daniel-mar | 525 | $var16a = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b0000)); |
526 | $var16b = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b0100)); |
||
527 | $var16c = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1000)); |
||
528 | $var16d = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1100)); |
||
2 | daniel-mar | 529 | $hash[16] = '?'; // was partially overwritten by variant |
27 | daniel-mar | 530 | |
2 | daniel-mar | 531 | $hash .= '????????'; // was cut off |
532 | |||
27 | daniel-mar | 533 | echo sprintf("%-32s %s\n", "SHA1(Namespace+Subject):", "[0x$hash]"); |
534 | echo sprintf("%-32s %s\n", "", " ^"); |
||
535 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
||
2 | daniel-mar | 536 | |
537 | break; |
||
27 | daniel-mar | 538 | case 7: |
29 | daniel-mar | 539 | /* |
540 | Variant 1, Version 7 UUID |
||
541 | - 48 bit Unix Time in milliseconds |
||
542 | - 4 bit Version (fix 0x7) |
||
543 | - 12 bit Random |
||
544 | - 2 bit Variant (fix 0b10) |
||
545 | - 62 bit Random |
||
546 | */ |
||
547 | |||
31 | daniel-mar | 548 | echo sprintf("%-32s %s\n", "Version:", "[7] Unix Epoch Time"); |
29 | daniel-mar | 549 | |
550 | $timestamp = substr($uuid, 0, 12); |
||
30 | daniel-mar | 551 | |
552 | // Timestamp: Split into seconds and milliseconds |
||
29 | daniel-mar | 553 | $ts = gmp_init($timestamp, 16); |
30 | daniel-mar | 554 | $ms = gmp_mod($ts, gmp_init("1000", 10)); |
555 | $ts = gmp_div($ts, gmp_init("1000", 10)); |
||
556 | $ts = gmp_strval($ts, 10); |
||
557 | $ms = gmp_strval($ms, 10); |
||
558 | $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 3/*ms*/, '0', STR_PAD_LEFT).' GMT'; |
||
29 | daniel-mar | 559 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts"); |
560 | |||
561 | $rand = ''; |
||
562 | for ($i=6; $i<16; $i++) { |
||
563 | $bin = base_convert(substr($uuid, $i*2, 2), 16, 2); |
||
564 | $bin = str_pad($bin, 8, "0", STR_PAD_LEFT); |
||
565 | |||
566 | if ($i == 6) { |
||
567 | // was overwritten by version |
||
568 | $bin[0] = '?'; |
||
569 | $bin[1] = '?'; |
||
570 | $bin[2] = '?'; |
||
571 | $bin[3] = '?'; |
||
572 | } else if ($i == 8) { |
||
573 | // was partially overwritten by variant |
||
574 | $bin[0] = '?'; |
||
575 | $bin[1] = '?'; |
||
576 | } |
||
577 | |||
578 | $rand .= "$bin "; |
||
579 | } |
||
580 | echo sprintf("%-32s %s\n", "Random bits:", trim($rand)); |
||
581 | |||
582 | $rand_bytes = substr(str_replace('-', '', strtolower($uuid)),13); |
||
38 | daniel-mar | 583 | $var16a = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b0000)); |
584 | $var16b = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b0100)); |
||
585 | $var16c = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b1000)); |
||
586 | $var16d = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b1100)); |
||
29 | daniel-mar | 587 | $rand_bytes[3] = '?'; // was partially overwritten by variant |
588 | echo sprintf("%-32s %s\n", "Random bytes:", "[0x$rand_bytes]"); |
||
589 | echo sprintf("%-32s %s\n", "", " ^"); |
||
590 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
||
591 | |||
592 | // TODO: convert to and from Base32 CROCKFORD ULID (make 2 methods in uuid_utils.inc.php) |
||
593 | // e.g. ULID: 01GCZ05N3JFRKBRWKNGCQZGP44 |
||
594 | // "Be aware that all version 7 UUIDs may be converted to ULIDs but not all ULIDs may be converted to UUIDs." |
||
595 | |||
27 | daniel-mar | 596 | break; |
597 | case 8: |
||
29 | daniel-mar | 598 | /* |
599 | Variant 1, Version 8 UUID |
||
35 | daniel-mar | 600 | - 48 bit Custom data |
29 | daniel-mar | 601 | - 4 bit Version (fix 0x8) |
35 | daniel-mar | 602 | - 12 bit Custom data |
29 | daniel-mar | 603 | - 2 bit Variant (fix 0b10) |
35 | daniel-mar | 604 | - 62 bit Custom data |
29 | daniel-mar | 605 | */ |
606 | |||
31 | daniel-mar | 607 | echo sprintf("%-32s %s\n", "Version:", "[8] Custom implementation"); |
29 | daniel-mar | 608 | |
609 | $custom_data = substr($uuid,0,12).substr($uuid,13); // exclude version nibble |
||
38 | daniel-mar | 610 | $custom_data[15] = dechex(hexdec($custom_data[15]) & 0b0011); // nibble was partially overwritten by variant |
29 | daniel-mar | 611 | $custom_data = strtolower($custom_data); |
612 | |||
34 | daniel-mar | 613 | $custom_block1 = substr($uuid, 0, 8); |
614 | $custom_block2 = substr($uuid, 8, 4); |
||
615 | $custom_block3 = substr($uuid, 12, 4); |
||
616 | $custom_block4 = substr($uuid, 16, 4); |
||
617 | $custom_block5 = substr($uuid, 20); |
||
618 | |||
619 | $custom_block3 = substr($custom_block3, 1); // remove version |
||
38 | daniel-mar | 620 | $custom_block4[0] = dechex(hexdec($custom_block4[0]) & 0b0011); // remove variant |
34 | daniel-mar | 621 | |
29 | daniel-mar | 622 | echo sprintf("%-32s %s\n", "Custom data:", "[0x$custom_data]"); |
34 | daniel-mar | 623 | echo sprintf("%-32s %s\n", "Custom block1 (32 bit):", "[0x$custom_block1]"); |
624 | echo sprintf("%-32s %s\n", "Custom block2 (16 bit):", "[0x$custom_block2]"); |
||
625 | echo sprintf("%-32s %s\n", "Custom block3 (12 bit):", "[0x$custom_block3]"); |
||
626 | echo sprintf("%-32s %s\n", "Custom block4 (14 bit):", "[0x$custom_block4]"); |
||
627 | echo sprintf("%-32s %s\n", "Custom block5 (48 bit):", "[0x$custom_block5]"); |
||
29 | daniel-mar | 628 | |
27 | daniel-mar | 629 | break; |
2 | daniel-mar | 630 | default: |
25 | daniel-mar | 631 | echo sprintf("%-32s %s\n", "Version:", "[$version] Unknown"); |
2 | daniel-mar | 632 | break; |
633 | } |
||
634 | |||
635 | break; |
||
636 | case 2: |
||
35 | daniel-mar | 637 | // TODO: Show byte order: 00112233-4455-6677-8899-aabbccddeeff => 33 22 11 00 55 44 77 66 88 99 aa bb cc dd ee ff |
638 | |||
31 | daniel-mar | 639 | // TODO: Is there any scheme in that legacy Microsoft GUIDs? |
27 | daniel-mar | 640 | echo sprintf("%-32s %s\n", "Variant:", "[0b110] Reserved for Microsoft Corporation"); |
2 | daniel-mar | 641 | break; |
642 | case 3: |
||
27 | daniel-mar | 643 | echo sprintf("%-32s %s\n", "Variant:", "[0b111] Reserved for future use"); |
2 | daniel-mar | 644 | break; |
645 | } |
||
28 | daniel-mar | 646 | |
647 | if (!$echo) { |
||
648 | $out = ob_get_contents(); |
||
649 | ob_end_clean(); |
||
650 | return $out; |
||
31 | daniel-mar | 651 | } else { |
652 | return true; |
||
28 | daniel-mar | 653 | } |
2 | daniel-mar | 654 | } |
655 | |||
656 | function uuid_canonize($uuid) { |
||
657 | if (!uuid_valid($uuid)) return false; |
||
658 | return oid_to_uuid(uuid_to_oid($uuid)); |
||
659 | } |
||
660 | |||
661 | function oid_to_uuid($oid) { |
||
662 | if (!is_uuid_oid($oid)) return false; |
||
663 | |||
8 | daniel-mar | 664 | if (substr($oid,0,1) == '.') { |
2 | daniel-mar | 665 | $oid = substr($oid, 1); |
666 | } |
||
667 | $ary = explode('.', $oid); |
||
668 | |||
669 | if (!isset($ary[2])) return false; |
||
670 | |||
671 | $val = $ary[2]; |
||
672 | |||
673 | $x = gmp_init($val, 10); |
||
674 | $y = gmp_strval($x, 16); |
||
675 | $y = str_pad($y, 32, "0", STR_PAD_LEFT); |
||
676 | return substr($y, 0, 8).'-'. |
||
677 | substr($y, 8, 4).'-'. |
||
678 | substr($y, 12, 4).'-'. |
||
679 | substr($y, 16, 4).'-'. |
||
680 | substr($y, 20, 12); |
||
681 | } |
||
682 | |||
683 | function is_uuid_oid($oid, $only_allow_root=false) { |
||
9 | daniel-mar | 684 | if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot |
2 | daniel-mar | 685 | |
686 | $ary = explode('.', $oid); |
||
687 | |||
688 | if ($only_allow_root) { |
||
689 | if (count($ary) != 3) return false; |
||
690 | } else { |
||
691 | if (count($ary) < 3) return false; |
||
692 | } |
||
693 | |||
694 | if ($ary[0] != '2') return false; |
||
695 | if ($ary[1] != '25') return false; |
||
696 | for ($i=2; $i<count($ary); $i++) { |
||
697 | $v = $ary[$i]; |
||
698 | if (!is_numeric($v)) return false; |
||
699 | if ($i == 2) { |
||
700 | // Must be in the range of 128 bit UUID |
||
701 | $test = gmp_init($v, 10); |
||
702 | if (strlen(gmp_strval($test, 16)) > 32) return false; |
||
703 | } |
||
704 | if ($v < 0) return false; |
||
705 | } |
||
706 | |||
707 | return true; |
||
708 | } |
||
709 | |||
710 | function uuid_to_oid($uuid) { |
||
711 | if (!uuid_valid($uuid)) return false; |
||
712 | |||
713 | $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
||
714 | $x = gmp_init($uuid, 16); |
||
29 | daniel-mar | 715 | return '2.25.'.gmp_strval($x, 10); |
2 | daniel-mar | 716 | } |
717 | |||
31 | daniel-mar | 718 | function uuid_numeric_value($uuid) { |
719 | $oid = uuid_to_oid($uuid); |
||
720 | if (!$oid) return false; |
||
721 | return substr($oid, strlen('2.25.')); |
||
722 | } |
||
723 | |||
724 | function uuid_c_syntax($uuid) { |
||
725 | $uuid = str_replace('{', '', $uuid); |
||
726 | return '{ 0x' . substr($uuid, 0, 8) . |
||
727 | ', 0x' . substr($uuid, 9, 4) . |
||
728 | ', 0x' . substr($uuid, 14, 4) . |
||
729 | ', { 0x' . substr($uuid, 19, 2). |
||
730 | ', 0x' . substr($uuid, 21, 2) . |
||
731 | ', 0x' . substr($uuid, 24, 2) . |
||
732 | ', 0x' . substr($uuid, 26, 2) . |
||
733 | ', 0x' . substr($uuid, 28, 2) . |
||
734 | ', 0x' . substr($uuid, 30, 2) . |
||
735 | ', 0x' . substr($uuid, 32, 2) . |
||
736 | ', 0x' . substr($uuid, 34, 2) . ' } }'; |
||
737 | } |
||
738 | |||
30 | daniel-mar | 739 | function gen_uuid($prefer_mac_address_based = true) { |
740 | $uuid = $prefer_mac_address_based ? gen_uuid_reordered()/*UUIDv6*/ : false; |
||
741 | if ($uuid === false) $uuid = gen_uuid_unix_epoch()/*UUIDv7*/; |
||
2 | daniel-mar | 742 | return $uuid; |
743 | } |
||
744 | |||
30 | daniel-mar | 745 | # -------------------------------------- |
746 | // Variant 1, Version 1 (Time based) UUID |
||
747 | # -------------------------------------- |
||
28 | daniel-mar | 748 | |
30 | daniel-mar | 749 | function gen_uuid_v1() { |
750 | return gen_uuid_timebased(); |
||
751 | } |
||
39 | daniel-mar | 752 | function gen_uuid_timebased($force_php_implementation=false) { |
2 | daniel-mar | 753 | # On Debian: apt-get install php-uuid |
754 | # extension_loaded('uuid') |
||
39 | daniel-mar | 755 | if (!$force_php_implementation && function_exists('uuid_create')) { |
2 | daniel-mar | 756 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
757 | /* |
||
758 | $x = uuid_create($context); |
||
759 | uuid_make($context, UUID_MAKE_V1); |
||
760 | uuid_export($context, UUID_FMT_STR, $uuid); |
||
761 | return trim($uuid); |
||
762 | */ |
||
763 | |||
764 | # PECL uuid extension like seen in php-uuid at Debian 9 |
||
765 | return trim(uuid_create(UUID_TYPE_TIME)); |
||
766 | } |
||
767 | |||
768 | # On Debian: apt-get install uuid-runtime |
||
39 | daniel-mar | 769 | if (!$force_php_implementation && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
2 | daniel-mar | 770 | $out = array(); |
771 | $ec = -1; |
||
772 | exec('uuidgen -t 2>/dev/null', $out, $ec); |
||
773 | if ($ec == 0) return trim($out[0]); |
||
774 | } |
||
775 | |||
776 | # If we hadn't any success yet, then implement the time based generation routine ourselves! |
||
777 | # Based on https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php |
||
778 | |||
779 | $uuid = array( |
||
780 | 'time_low' => 0, /* 32-bit */ |
||
781 | 'time_mid' => 0, /* 16-bit */ |
||
782 | 'time_hi' => 0, /* 16-bit */ |
||
783 | 'clock_seq_hi' => 0, /* 8-bit */ |
||
784 | 'clock_seq_low' => 0, /* 8-bit */ |
||
785 | 'node' => array() /* 48-bit */ |
||
786 | ); |
||
787 | |||
788 | /* |
||
789 | * Get current time in 100 ns intervals. The magic value |
||
790 | * is the offset between UNIX epoch and the UUID UTC |
||
791 | * time base October 15, 1582. |
||
792 | */ |
||
38 | daniel-mar | 793 | if (time_nanosleep(0,100) !== true) usleep(1); // Wait 100ns, to make sure that the time part changes if multiple UUIDs are generated |
2 | daniel-mar | 794 | $tp = gettimeofday(); |
38 | daniel-mar | 795 | if (PHP_INT_SIZE == 4) { |
796 | $tp['sec'] = gmp_init($tp['sec'],10); |
||
797 | $tp['usec'] = gmp_init($tp['usec'],10); |
||
798 | $time = gmp_add(gmp_add(gmp_mul($tp['sec'], gmp_init('10000000',10)),gmp_mul($tp['usec'], gmp_init('10',10))),gmp_init('01B21DD213814000',16)); |
||
799 | $uuid['time_low'] = gmp_and($time, gmp_init('ffffffff',16)); |
||
800 | $high = gmp_shiftr($time,32); |
||
801 | $uuid['time_mid'] = gmp_and($high, gmp_init('ffff',16)); |
||
802 | $uuid['time_hi'] = intval(gmp_and(gmp_shiftr($high,16),gmp_init('fff',16)),10) | (1/*TimeBased*/ << 12); |
||
803 | } else { |
||
804 | $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) + 0x01B21DD213814000; |
||
805 | $uuid['time_low'] = $time & 0xffffffff; |
||
806 | /* Work around PHP 32-bit bit-operation limits */ |
||
807 | $high = intval($time / 0xffffffff); |
||
808 | $uuid['time_mid'] = $high & 0xffff; |
||
809 | $uuid['time_hi'] = (($high >> 16) & 0xfff) | (1/*TimeBased*/ << 12); |
||
810 | } |
||
2 | daniel-mar | 811 | |
812 | /* |
||
813 | * We don't support saved state information and generate |
||
814 | * a random clock sequence each time. |
||
815 | */ |
||
40 | daniel-mar | 816 | $uuid['clock_seq_hi'] = _random_int(0, 255) & 0b00111111 | 0b10000000; // set variant to 0b10__ (RFC 4122) |
24 | daniel-mar | 817 | $uuid['clock_seq_low'] = _random_int(0, 255); |
2 | daniel-mar | 818 | |
819 | /* |
||
820 | * Node should be set to the 48-bit IEEE node identifier |
||
821 | */ |
||
822 | $mac = get_mac_address(); |
||
823 | if ($mac) { |
||
25 | daniel-mar | 824 | $node = str_replace('-','',str_replace(':','',$mac)); |
2 | daniel-mar | 825 | for ($i = 0; $i < 6; $i++) { |
826 | $uuid['node'][$i] = hexdec(substr($node, $i*2, 2)); |
||
827 | } |
||
39 | daniel-mar | 828 | } else { |
829 | // If we cannot get a MAC address, then generate a random AAI |
||
41 | daniel-mar | 830 | $uuid['node'] = explode('-', gen_aai(48, false)); |
831 | $uuid['node'] = array_map('hexdec', $uuid['node']); |
||
2 | daniel-mar | 832 | } |
833 | |||
39 | daniel-mar | 834 | /* |
835 | * Now output the UUID |
||
836 | */ |
||
837 | return sprintf( |
||
838 | '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x', |
||
839 | ($uuid['time_low']), ($uuid['time_mid']), ($uuid['time_hi']), |
||
840 | $uuid['clock_seq_hi'], $uuid['clock_seq_low'], |
||
841 | $uuid['node'][0], $uuid['node'][1], $uuid['node'][2], |
||
842 | $uuid['node'][3], $uuid['node'][4], $uuid['node'][5]); |
||
2 | daniel-mar | 843 | } |
844 | |||
30 | daniel-mar | 845 | # -------------------------------------- |
28 | daniel-mar | 846 | // Variant 1, Version 2 (DCE Security) UUID |
30 | daniel-mar | 847 | # -------------------------------------- |
848 | |||
27 | daniel-mar | 849 | define('DCE_DOMAIN_PERSON', 0); |
850 | define('DCE_DOMAIN_GROUP', 1); |
||
851 | define('DCE_DOMAIN_ORG', 2); |
||
30 | daniel-mar | 852 | function gen_uuid_v2($domain, $id) { |
853 | return gen_uuid_dce($domain, $id); |
||
854 | } |
||
2 | daniel-mar | 855 | function gen_uuid_dce($domain, $id) { |
31 | daniel-mar | 856 | if (($domain ?? '') === '') throw new Exception("Domain ID missing"); |
857 | if (!is_numeric($domain)) throw new Exception("Invalid Domain ID"); |
||
858 | if (($domain < 0) || ($domain > 255)) throw new Exception("Domain ID must be in range 0..255"); |
||
859 | |||
860 | if (($id ?? '') === '') throw new Exception("ID value missing"); |
||
861 | if (!is_numeric($id)) throw new Exception("Invalid ID value"); |
||
862 | if (($id < 0) || ($id > 4294967295)) throw new Exception("ID value must be in range 0..4294967295"); |
||
863 | |||
2 | daniel-mar | 864 | # Start with a version 1 UUID |
865 | $uuid = gen_uuid_timebased(); |
||
866 | |||
27 | daniel-mar | 867 | # Add Domain Number |
2 | daniel-mar | 868 | $uuid = str_pad(dechex($id), 8, '0', STR_PAD_LEFT) . substr($uuid, 8); |
869 | |||
27 | daniel-mar | 870 | # Add Domain (this overwrites part of the clock sequence) |
2 | daniel-mar | 871 | $uuid = substr($uuid,0,21) . str_pad(dechex($domain), 2, '0', STR_PAD_LEFT) . substr($uuid, 23); |
872 | |||
873 | # Change version to 2 |
||
874 | $uuid[14] = '2'; |
||
875 | |||
876 | return $uuid; |
||
877 | } |
||
878 | |||
30 | daniel-mar | 879 | # -------------------------------------- |
28 | daniel-mar | 880 | // Variant 1, Version 3 (MD5 name based) UUID |
30 | daniel-mar | 881 | # -------------------------------------- |
882 | |||
883 | function gen_uuid_v3($namespace_uuid, $name) { |
||
884 | return gen_uuid_md5_namebased($namespace_uuid, $name); |
||
885 | } |
||
2 | daniel-mar | 886 | function gen_uuid_md5_namebased($namespace_uuid, $name) { |
31 | daniel-mar | 887 | if (($namespace_uuid ?? '') === '') throw new Exception("Namespace UUID missing"); |
888 | if (!uuid_valid($namespace_uuid)) throw new Exception("Invalid namespace UUID '$namespace_uuid'"); |
||
889 | |||
2 | daniel-mar | 890 | $namespace_uuid = uuid_canonize($namespace_uuid); |
891 | $namespace_uuid = str_replace('-', '', $namespace_uuid); |
||
892 | $namespace_uuid = hex2bin($namespace_uuid); |
||
893 | |||
894 | $hash = md5($namespace_uuid.$name); |
||
895 | $hash[12] = '3'; // Set version: 3 = MD5 |
||
38 | daniel-mar | 896 | $hash[16] = dechex(hexdec($hash[16]) & 0b0011 | 0b1000); // Set variant to "10xx" (RFC4122) |
2 | daniel-mar | 897 | |
898 | return substr($hash, 0, 8).'-'. |
||
899 | substr($hash, 8, 4).'-'. |
||
900 | substr($hash, 12, 4).'-'. |
||
901 | substr($hash, 16, 4).'-'. |
||
902 | substr($hash, 20, 12); |
||
903 | } |
||
904 | |||
30 | daniel-mar | 905 | # -------------------------------------- |
28 | daniel-mar | 906 | // Variant 1, Version 4 (Random) UUID |
30 | daniel-mar | 907 | # -------------------------------------- |
908 | |||
909 | function gen_uuid_v4() { |
||
910 | return gen_uuid_random(); |
||
911 | } |
||
2 | daniel-mar | 912 | function gen_uuid_random() { |
913 | # On Windows: Requires |
||
914 | # extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext" |
||
915 | # extension=com_dotnet |
||
31 | daniel-mar | 916 | // TODO: can we trust that com_create_guid() always outputs UUIDv4? |
30 | daniel-mar | 917 | /* |
2 | daniel-mar | 918 | if (function_exists('com_create_guid')) { |
919 | return strtolower(trim(com_create_guid(), '{}')); |
||
920 | } |
||
30 | daniel-mar | 921 | */ |
2 | daniel-mar | 922 | |
923 | # On Debian: apt-get install php-uuid |
||
924 | # extension_loaded('uuid') |
||
925 | if (function_exists('uuid_create')) { |
||
926 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
||
927 | /* |
||
928 | $x = uuid_create($context); |
||
929 | uuid_make($context, UUID_MAKE_V4); |
||
930 | uuid_export($context, UUID_FMT_STR, $uuid); |
||
931 | return trim($uuid); |
||
932 | */ |
||
933 | |||
934 | # PECL uuid extension like seen in php-uuid at Debian 9 |
||
935 | return trim(uuid_create(UUID_TYPE_RANDOM)); |
||
936 | } |
||
937 | |||
938 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
||
939 | # On Debian: apt-get install uuid-runtime |
||
940 | $out = array(); |
||
941 | $ec = -1; |
||
942 | exec('uuidgen -r 2>/dev/null', $out, $ec); |
||
943 | if ($ec == 0) return trim($out[0]); |
||
944 | |||
945 | # On Debian Jessie: UUID V4 (Random) |
||
946 | if (file_exists('/proc/sys/kernel/random/uuid')) { |
||
947 | return trim(file_get_contents('/proc/sys/kernel/random/uuid')); |
||
948 | } |
||
949 | } |
||
950 | |||
951 | # Make the UUID by ourselves |
||
952 | # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php |
||
953 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
||
24 | daniel-mar | 954 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), |
955 | _random_int( 0, 0xffff ), |
||
956 | _random_int( 0, 0x0fff ) | 0x4000, |
||
957 | _random_int( 0, 0x3fff ) | 0x8000, |
||
958 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), _random_int( 0, 0xffff ) |
||
2 | daniel-mar | 959 | ); |
960 | } |
||
961 | |||
30 | daniel-mar | 962 | # -------------------------------------- |
28 | daniel-mar | 963 | // Variant 1, Version 5 (SHA1 name based) UUID |
30 | daniel-mar | 964 | # -------------------------------------- |
965 | |||
966 | function gen_uuid_v5($namespace_uuid, $name) { |
||
967 | return gen_uuid_sha1_namebased($namespace_uuid, $name); |
||
968 | } |
||
2 | daniel-mar | 969 | function gen_uuid_sha1_namebased($namespace_uuid, $name) { |
31 | daniel-mar | 970 | if (($namespace_uuid ?? '') === '') throw new Exception("Namespace UUID missing"); |
971 | if (!uuid_valid($namespace_uuid)) throw new Exception("Invalid namespace UUID '$namespace_uuid'"); |
||
972 | |||
2 | daniel-mar | 973 | $namespace_uuid = str_replace('-', '', $namespace_uuid); |
974 | $namespace_uuid = hex2bin($namespace_uuid); |
||
975 | |||
976 | $hash = sha1($namespace_uuid.$name); |
||
977 | $hash[12] = '5'; // Set version: 5 = SHA1 |
||
38 | daniel-mar | 978 | $hash[16] = dechex(hexdec($hash[16]) & 0b0011 | 0b1000); // Set variant to "0b10__" (RFC4122/DCE1.1) |
2 | daniel-mar | 979 | |
980 | return substr($hash, 0, 8).'-'. |
||
981 | substr($hash, 8, 4).'-'. |
||
982 | substr($hash, 12, 4).'-'. |
||
983 | substr($hash, 16, 4).'-'. |
||
984 | substr($hash, 20, 12); |
||
985 | } |
||
986 | |||
30 | daniel-mar | 987 | # -------------------------------------- |
988 | // Variant 1, Version 6 (Reordered) UUID |
||
989 | # -------------------------------------- |
||
990 | |||
991 | function gen_uuid_v6() { |
||
992 | return gen_uuid_reordered(); |
||
993 | } |
||
994 | function gen_uuid_reordered() { |
||
995 | // Start with a UUIDv1 |
||
996 | $uuid = gen_uuid_timebased(); |
||
997 | |||
998 | // Convert to UUIDv6 |
||
999 | return uuid1_to_uuid6($uuid); |
||
1000 | } |
||
1001 | function uuid6_to_uuid1($hex) { |
||
1002 | $hex = uuid_canonize($hex); |
||
1003 | if ($hex === false) return false; |
||
1004 | $hex = preg_replace('@[^0-9A-F]@i', '', $hex); |
||
1005 | $hex = substr($hex, 7, 5). |
||
1006 | substr($hex, 13, 3). |
||
1007 | substr($hex, 3, 4). |
||
1008 | '1' . substr($hex, 0, 3). |
||
1009 | substr($hex, 16); |
||
1010 | return substr($hex, 0, 8).'-'. |
||
1011 | substr($hex, 8, 4).'-'. |
||
1012 | substr($hex, 12, 4).'-'. |
||
1013 | substr($hex, 16, 4).'-'. |
||
1014 | substr($hex, 20, 12); |
||
1015 | } |
||
1016 | function uuid1_to_uuid6($hex) { |
||
1017 | $hex = uuid_canonize($hex); |
||
1018 | if ($hex === false) return false; |
||
1019 | $hex = preg_replace('@[^0-9A-F]@i', '', $hex); |
||
1020 | $hex = substr($hex, 13, 3). |
||
1021 | substr($hex, 8, 4). |
||
1022 | substr($hex, 0, 5). |
||
1023 | '6' . substr($hex, 5, 3). |
||
1024 | substr($hex, 16); |
||
1025 | return substr($hex, 0, 8).'-'. |
||
1026 | substr($hex, 8, 4).'-'. |
||
1027 | substr($hex, 12, 4).'-'. |
||
1028 | substr($hex, 16, 4).'-'. |
||
1029 | substr($hex, 20, 12); |
||
1030 | } |
||
1031 | |||
1032 | # -------------------------------------- |
||
1033 | // Variant 1, Version 7 (Unix Epoch) UUID |
||
1034 | # -------------------------------------- |
||
1035 | |||
1036 | function gen_uuid_v7() { |
||
1037 | return gen_uuid_unix_epoch(); |
||
1038 | } |
||
1039 | function gen_uuid_unix_epoch() { |
||
1040 | // Start with an UUIDv4 |
||
1041 | $uuid = gen_uuid_random(); |
||
1042 | |||
1043 | // Add the timestamp |
||
37 | daniel-mar | 1044 | usleep(1000); // Wait 1ms, to make sure that the time part changes if multiple UUIDs are generated |
30 | daniel-mar | 1045 | if (function_exists('gmp_init')) { |
1046 | list($ms,$sec) = explode(' ', microtime(false)); |
||
1047 | $sec = gmp_init($sec, 10); |
||
1048 | $ms = gmp_init(substr($ms,2,3), 10); |
||
1049 | $unix_ts = gmp_strval(gmp_add(gmp_mul($sec, '1000'), $ms),16); |
||
1050 | } else { |
||
1051 | $unix_ts = dechex((int)round(microtime(true)*1000)); |
||
1052 | } |
||
1053 | $unix_ts = str_pad($unix_ts, 12, '0', STR_PAD_LEFT); |
||
1054 | for ($i=0;$i<8;$i++) $uuid[$i] = substr($unix_ts, $i, 1); |
||
1055 | for ($i=0;$i<4;$i++) $uuid[9+$i] = substr($unix_ts, 8+$i, 1); |
||
1056 | |||
1057 | // set version |
||
1058 | $uuid[14] = '7'; |
||
1059 | |||
1060 | return $uuid; |
||
1061 | } |
||
1062 | |||
1063 | # -------------------------------------- |
||
34 | daniel-mar | 1064 | // Variant 1, Version 8 (Custom) UUID |
1065 | # -------------------------------------- |
||
30 | daniel-mar | 1066 | |
34 | daniel-mar | 1067 | function gen_uuid_v8($block1_32bit, $block2_16bit, $block3_12bit, $block4_14bit, $block5_48bit) { |
1068 | return gen_uuid_custom($block1_32bit, $block2_16bit, $block3_12bit, $block4_14bit, $block5_48bit); |
||
1069 | } |
||
1070 | function gen_uuid_custom($block1_32bit, $block2_16bit, $block3_12bit, $block4_14bit, $block5_48bit) { |
||
1071 | if (preg_replace('@[0-9A-F]@i', '', $block1_32bit) != '') throw new Exception("Invalid data for block 1. Must be hex input"); |
||
1072 | if (preg_replace('@[0-9A-F]@i', '', $block2_16bit) != '') throw new Exception("Invalid data for block 2. Must be hex input"); |
||
1073 | if (preg_replace('@[0-9A-F]@i', '', $block3_12bit) != '') throw new Exception("Invalid data for block 3. Must be hex input"); |
||
1074 | if (preg_replace('@[0-9A-F]@i', '', $block4_14bit) != '') throw new Exception("Invalid data for block 4. Must be hex input"); |
||
1075 | if (preg_replace('@[0-9A-F]@i', '', $block5_48bit) != '') throw new Exception("Invalid data for block 5. Must be hex input"); |
||
1076 | |||
1077 | $block1 = str_pad(substr($block1_32bit, -8), 8, '0', STR_PAD_LEFT); |
||
1078 | $block2 = str_pad(substr($block2_16bit, -4), 4, '0', STR_PAD_LEFT); |
||
1079 | $block3 = str_pad(substr($block3_12bit, -4), 4, '0', STR_PAD_LEFT); |
||
1080 | $block4 = str_pad(substr($block4_14bit, -4), 4, '0', STR_PAD_LEFT); |
||
1081 | $block5 = str_pad(substr($block5_48bit,-12), 12, '0', STR_PAD_LEFT); |
||
1082 | |||
1083 | $block3[0] = '8'; // Version 8 = Custom |
||
38 | daniel-mar | 1084 | $block4[0] = dechex(hexdec($block4[0]) & 0b0011 | 0b1000); // Variant 0b10__ = RFC4122 |
34 | daniel-mar | 1085 | |
1086 | return strtolower($block1.'-'.$block2.'-'.$block3.'-'.$block4.'-'.$block5); |
||
1087 | } |
||
1088 | |||
1089 | # -------------------------------------- |
||
1090 | |||
2 | daniel-mar | 1091 | // http://php.net/manual/de/function.hex2bin.php#113057 |
38 | daniel-mar | 1092 | if (!function_exists('hex2bin')) { |
1093 | function hex2bin($str) { |
||
2 | daniel-mar | 1094 | $sbin = ""; |
38 | daniel-mar | 1095 | $len = strlen($str); |
2 | daniel-mar | 1096 | for ( $i = 0; $i < $len; $i += 2 ) { |
38 | daniel-mar | 1097 | $sbin .= pack("H*", substr($str, $i, 2)); |
2 | daniel-mar | 1098 | } |
1099 | return $sbin; |
||
1100 | } |
||
1101 | } |
||
38 | daniel-mar | 1102 | |
1103 | // https://stackoverflow.com/questions/72127764/shift-right-left-bitwise-operators-in-php7-gmp-extension |
||
1104 | if (!function_exists('gmp_shiftl')) { |
||
1105 | function gmp_shiftl($x,$n) { // shift left |
||
1106 | return(gmp_mul($x,gmp_pow(2,$n))); |
||
1107 | } |
||
1108 | } |
||
1109 | |||
1110 | if (!function_exists('gmp_shiftr')) { |
||
1111 | function gmp_shiftr($x,$n) { // shift right |
||
1112 | return(gmp_div_q($x,gmp_pow(2,$n))); |
||
1113 | } |
||
1114 | } |