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