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