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