Rev 27 | Rev 29 | 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 |
26 | daniel-mar | 6 | * Version 2023-07-11 |
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 | |||
28 | daniel-mar | 26 | define('UUID_NAMEBASED_NS_DNS', '6ba7b810-9dad-11d1-80b4-00c04fd430c8'); // FQDN |
2 | daniel-mar | 27 | define('UUID_NAMEBASED_NS_URL', '6ba7b811-9dad-11d1-80b4-00c04fd430c8'); |
28 | define('UUID_NAMEBASED_NS_OID', '6ba7b812-9dad-11d1-80b4-00c04fd430c8'); |
||
28 | daniel-mar | 29 | define('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 | |||
47 | $uuid = preg_replace('@[0-9A-F]@', '', $uuid); |
||
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); |
||
59 | $uuid = strtoupper($uuid); |
||
60 | $uuid = preg_replace('@[^0-9A-F]@', '', $uuid); |
||
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: |
||
27 | daniel-mar | 71 | echo sprintf("%-32s %s\n", "Variant:", "[0b0__] NCS (reserved for backward compatibility)"); |
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 |
||
92 | - 1 bit Variant (fix 0b0___) |
||
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 | |
28 | daniel-mar | 101 | // TODO: Make a generator for variant 0 UUIDs! |
27 | daniel-mar | 102 | |
2 | daniel-mar | 103 | # Timestamp: Count of 4us intervals since 01 Jan 1980 00:00:00 GMT |
104 | # 1/0,000004 = 250000 |
||
105 | # Seconds between 1970 and 1980 : 315532800 |
||
106 | # 250000*315532800=78883200000000 |
||
107 | $timestamp = substr($uuid, 0, 12); |
||
108 | $ts = gmp_init($timestamp, 16); |
||
109 | $ts = gmp_add($ts, gmp_init("78883200000000")); |
||
110 | $ms = gmp_mod($ts, gmp_init("250000")); |
||
111 | $ts = gmp_div($ts, gmp_init("250000")); |
||
112 | $ts = gmp_strval($ts); |
||
113 | $ms = gmp_strval($ms); |
||
114 | $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT'; |
||
25 | daniel-mar | 115 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts"); |
2 | daniel-mar | 116 | |
117 | $reserved = substr($uuid, 12, 4); |
||
27 | daniel-mar | 118 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$reserved]"); |
2 | daniel-mar | 119 | |
120 | $family_hex = substr($uuid, 16, 2); |
||
121 | $family_dec = hexdec($family_hex); |
||
28 | daniel-mar | 122 | $nodeid_hex = substr($uuid, 18, 14); |
123 | $nodeid_dec = hexdec($nodeid_hex); |
||
2 | daniel-mar | 124 | if ($family_dec == 2) { |
125 | $family_ = 'IP'; |
||
28 | daniel-mar | 126 | // 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 |
127 | // := [ |
||
128 | // time_high := 16#458487df, |
||
129 | // time_low := 16#9fb2, |
||
130 | // reserved := 16#000, |
||
131 | // family := chr(16#02), |
||
132 | // host := [chr(16#c0), chr(16#64), chr(16#02), chr(16#03), |
||
133 | // chr(16#00), chr(16#00), chr(16#00)] |
||
134 | // ] |
||
135 | // This means that the IP address is 32 bits hex, and 32 bits are unused |
||
136 | $nodeid_desc = hexdec(substr($nodeid_hex,0,2)).'.'. |
||
137 | hexdec(substr($nodeid_hex,2,2)).'.'. |
||
138 | hexdec(substr($nodeid_hex,4,2)).'.'. |
||
139 | hexdec(substr($nodeid_hex,6,2)); |
||
140 | $rest = substr($nodeid_hex,8,6); |
||
141 | if ($rest != '000000') $nodeid_desc .= " + unexpected rest 0x$rest"; |
||
2 | daniel-mar | 142 | } else if ($family_dec == 13) { |
143 | $family_ = 'DDS (Data Link)'; |
||
28 | daniel-mar | 144 | // 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 |
145 | // = { 0x34dc23af, |
||
146 | // 0xf000, |
||
147 | // 0x0000, |
||
148 | // 0x0d, |
||
149 | // {0x00, 0x00, 0x7c, 0x5f, 0x00, 0x00, 0x00} }; |
||
150 | // https://github.com/cjsv/uuid/blob/master/Doc writes: |
||
151 | // "Family 13 (dds) looks like node is 00 | nnnnnn 000000." |
||
152 | |||
153 | $nodeid_desc = ''; |
||
154 | |||
155 | $start = substr($nodeid_hex,0,2); |
||
156 | if ($start != '00') $nodeid_desc .= "unexpected start 0x$start + "; |
||
157 | |||
158 | $nodeid_desc .= ($nodeid_dec >> 24) & 0xFFFFFF; |
||
159 | |||
160 | $rest = substr($nodeid_hex,8,6); |
||
161 | if ($rest != '000000') $nodeid_desc .= " + unexpected rest 0x$rest"; |
||
2 | daniel-mar | 162 | } else { |
28 | daniel-mar | 163 | $family_ = "Unknown (Family $family_dec)"; # There are probably no more families |
164 | $nodeid_desc = 'Unknown'; |
||
2 | daniel-mar | 165 | } |
25 | daniel-mar | 166 | echo sprintf("%-32s %s\n", "Family:", "[0x$family_hex = $family_dec] $family_"); |
2 | daniel-mar | 167 | |
28 | daniel-mar | 168 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$nodeid_hex] $nodeid_desc"); |
2 | daniel-mar | 169 | |
170 | break; |
||
171 | case 1: |
||
27 | daniel-mar | 172 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 (Leach-Mealling-Salz) / DCE 1.1"); |
2 | daniel-mar | 173 | |
174 | $version = hexdec(substr($uuid, 12, 1)); |
||
175 | switch ($version) { |
||
176 | case 1: |
||
27 | daniel-mar | 177 | /* |
178 | Variant 1, Version 1 UUID |
||
179 | - 32 bit Low Time |
||
180 | - 16 bit Mid Time |
||
181 | - 4 bit Version (fix 0x1) |
||
182 | - 12 bit High Time |
||
28 | daniel-mar | 183 | - 2 bit Variant (fix 0b10) |
184 | - 6 bit Clock Seq High |
||
185 | - 8 bit Clock Seq Low |
||
27 | daniel-mar | 186 | - 48 bit MAC Address |
187 | */ |
||
188 | |||
25 | daniel-mar | 189 | echo sprintf("%-32s %s\n", "Version:", "[1] Time-based with unique random host identifier"); |
2 | daniel-mar | 190 | |
191 | # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00 |
||
192 | # 1/0,0000001 = 10000000 |
||
193 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).substr($uuid, 0, 8); |
||
194 | $ts = gmp_init($timestamp, 16); |
||
195 | $ts = gmp_sub($ts, gmp_init("122192928000000000")); |
||
196 | $ms = gmp_mod($ts, gmp_init("10000000")); |
||
197 | $ts = gmp_div($ts, gmp_init("10000000")); |
||
198 | $ts = gmp_strval($ts); |
||
199 | $ms = gmp_strval($ms); |
||
200 | $ts = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT'; |
||
25 | daniel-mar | 201 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts"); |
2 | daniel-mar | 202 | |
203 | $x = hexdec(substr($uuid, 16, 4)); |
||
204 | $dec = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x) |
||
205 | $hex = substr($uuid, 16, 4); |
||
25 | daniel-mar | 206 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
2 | daniel-mar | 207 | |
208 | $x = substr($uuid, 20, 12); |
||
209 | $nodeid = ''; |
||
210 | for ($i=0; $i<6; $i++) { |
||
211 | $nodeid .= substr($x, $i*2, 2); |
||
25 | daniel-mar | 212 | if ($i != 5) $nodeid .= '-'; |
2 | daniel-mar | 213 | } |
27 | daniel-mar | 214 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
2 | daniel-mar | 215 | |
216 | if (function_exists('decode_mac')) { |
||
25 | daniel-mar | 217 | echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n\n"; |
15 | daniel-mar | 218 | decode_mac($nodeid); |
2 | daniel-mar | 219 | } |
220 | |||
221 | break; |
||
222 | case 2: |
||
27 | daniel-mar | 223 | /* |
224 | Variant 1, Version 2 UUID |
||
225 | - 32 bit Local Domain Number |
||
226 | - 16 bit Mid Time |
||
227 | - 4 bit Version (fix 0x2) |
||
228 | - 12 bit High Time |
||
28 | daniel-mar | 229 | - 2 bit Variant (fix 0b10) |
230 | - 6 bit Clock Seq |
||
231 | - 8 bit Local Domain |
||
27 | daniel-mar | 232 | - 48 bit MAC Address |
233 | */ |
||
234 | |||
28 | daniel-mar | 235 | // see also https://unicorn-utterances.com/posts/what-happened-to-uuid-v2 |
236 | |||
25 | daniel-mar | 237 | echo sprintf("%-32s %s\n", "Version:", "[2] DCE Security version"); |
2 | daniel-mar | 238 | |
27 | daniel-mar | 239 | # 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". |
240 | $x = substr($uuid, 18, 2); |
||
241 | if ($x == '00') $domain_info = 'Person (POSIX: User-ID)'; |
||
242 | else if ($x == '01') $domain_info = 'Group (POSIX: Group-ID)'; |
||
243 | else if ($x == '02') $domain_info = 'Organization'; |
||
244 | else $domain_info = 'site-defined (Domain '.hexdec($x).')'; |
||
245 | echo sprintf("%-32s %s\n", "Local Domain:", "[0x$x] $domain_info"); |
||
246 | |||
2 | daniel-mar | 247 | # 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. |
248 | $x = substr($uuid, 0, 8); |
||
27 | daniel-mar | 249 | echo sprintf("%-32s %s\n", "Local Domain Number:", "[0x$x]"); |
2 | daniel-mar | 250 | |
251 | # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00 |
||
252 | # 1/0,0000001 = 10000000 |
||
253 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'00000000'; |
||
254 | $ts = gmp_init($timestamp, 16); |
||
255 | $ts = gmp_sub($ts, gmp_init("122192928000000000")); |
||
256 | $ms = gmp_mod($ts, gmp_init("10000000")); |
||
257 | $ts = gmp_div($ts, gmp_init("10000000")); |
||
258 | $ts = gmp_strval($ts); |
||
259 | $ms = gmp_strval($ms); |
||
260 | $ts_min = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT'; |
||
261 | |||
262 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'FFFFFFFF'; |
||
263 | $ts = gmp_init($timestamp, 16); |
||
264 | $ts = gmp_sub($ts, gmp_init("122192928000000000")); |
||
265 | $ms = gmp_mod($ts, gmp_init("10000000")); |
||
266 | $ts = gmp_div($ts, gmp_init("10000000")); |
||
267 | $ts = gmp_strval($ts); |
||
268 | $ms = gmp_strval($ms); |
||
269 | $ts_max = gmdate('Y-m-d H:i:s', intval($ts))."'".str_pad($ms, 7, '0', STR_PAD_LEFT).' GMT'; |
||
270 | |||
271 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).'xxxxxxxx'; |
||
25 | daniel-mar | 272 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts_min - $ts_max"); |
2 | daniel-mar | 273 | |
28 | daniel-mar | 274 | $x = hexdec(substr($uuid, 16, 2)); |
275 | $dec = $x & 0x3F; // The highest 2 bits are used by "variant" (10xx) |
||
276 | $hex = substr($uuid, 16, 2); |
||
27 | daniel-mar | 277 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
2 | daniel-mar | 278 | |
279 | $x = substr($uuid, 20, 12); |
||
280 | $nodeid = ''; |
||
281 | for ($i=0; $i<6; $i++) { |
||
282 | $nodeid .= substr($x, $i*2, 2); |
||
25 | daniel-mar | 283 | if ($i != 5) $nodeid .= '-'; |
2 | daniel-mar | 284 | } |
27 | daniel-mar | 285 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
2 | daniel-mar | 286 | |
287 | if (function_exists('decode_mac')) { |
||
25 | daniel-mar | 288 | echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n\n"; |
15 | daniel-mar | 289 | decode_mac($nodeid); |
2 | daniel-mar | 290 | } |
291 | |||
292 | break; |
||
293 | case 3: |
||
28 | daniel-mar | 294 | /* |
295 | Variant 1, Version 3 UUID |
||
296 | - 48 bit Hash High |
||
297 | - 4 bit Version (fix 0x2) |
||
298 | - 12 bit Hash Mid |
||
299 | - 2 bit Variant (fix 0b10) |
||
300 | - 62 bit Hash Low |
||
301 | */ |
||
302 | |||
25 | daniel-mar | 303 | echo sprintf("%-32s %s\n", "Version:", "[3] Name-based (MD5 hash)"); |
2 | daniel-mar | 304 | |
305 | $hash = str_replace('-', '', strtolower($uuid)); |
||
27 | daniel-mar | 306 | |
2 | daniel-mar | 307 | $hash[12] = '?'; // was overwritten by version |
27 | daniel-mar | 308 | |
309 | $var16a = dechex((hexdec($hash[16])&3) + 0x0/*00__*/); |
||
310 | $var16b = dechex((hexdec($hash[16])&3) + 0x4/*01__*/); |
||
311 | $var16c = dechex((hexdec($hash[16])&3) + 0x8/*10__*/); |
||
312 | $var16d = dechex((hexdec($hash[16])&3) + 0xC/*11__*/); |
||
2 | daniel-mar | 313 | $hash[16] = '?'; // was partially overwritten by variant |
314 | |||
27 | daniel-mar | 315 | echo sprintf("%-32s %s\n", "MD5(Namespace+Subject):", "[0x$hash]"); |
316 | echo sprintf("%-32s %s\n", "", " ^"); |
||
317 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
||
2 | daniel-mar | 318 | |
319 | break; |
||
320 | case 4: |
||
28 | daniel-mar | 321 | /* |
322 | Variant 1, Version 4 UUID |
||
323 | - 48 bit Random High |
||
324 | - 4 bit Version (fix 0x2) |
||
325 | - 12 bit Random Mid |
||
326 | - 2 bit Variant (fix 0b10) |
||
327 | - 62 bit Random Low |
||
328 | */ |
||
329 | |||
25 | daniel-mar | 330 | echo sprintf("%-32s %s\n", "Version:", "[4] Random"); |
2 | daniel-mar | 331 | |
25 | daniel-mar | 332 | $rand_line1 = ''; |
333 | $rand_line2 = ''; |
||
2 | daniel-mar | 334 | for ($i=0; $i<16; $i++) { |
335 | $bin = base_convert(substr($uuid, $i*2, 2), 16, 2); |
||
336 | $bin = str_pad($bin, 8, "0", STR_PAD_LEFT); |
||
337 | |||
338 | if ($i == 6) { |
||
25 | daniel-mar | 339 | // was overwritten by version |
340 | $bin[0] = '?'; |
||
341 | $bin[1] = '?'; |
||
342 | $bin[2] = '?'; |
||
343 | $bin[3] = '?'; |
||
2 | daniel-mar | 344 | } else if ($i == 8) { |
25 | daniel-mar | 345 | // was partially overwritten by variant |
346 | $bin[0] = '?'; |
||
347 | $bin[1] = '?'; |
||
2 | daniel-mar | 348 | } |
349 | |||
25 | daniel-mar | 350 | if ($i<8) $rand_line1 .= "$bin "; |
351 | if ($i>=8) $rand_line2 .= "$bin "; |
||
2 | daniel-mar | 352 | } |
25 | daniel-mar | 353 | echo sprintf("%-32s %s\n", "Random bits:", trim($rand_line1)); |
354 | echo sprintf("%-32s %s\n", "", trim($rand_line2)); |
||
2 | daniel-mar | 355 | |
27 | daniel-mar | 356 | $rand_bytes = str_replace('-', '', strtolower($uuid)); |
357 | $rand_bytes[12] = '?'; // was overwritten by version |
||
358 | $var16a = dechex((hexdec($rand_bytes[16])&3) + 0x0/*00__*/); |
||
359 | $var16b = dechex((hexdec($rand_bytes[16])&3) + 0x4/*01__*/); |
||
360 | $var16c = dechex((hexdec($rand_bytes[16])&3) + 0x8/*10__*/); |
||
361 | $var16d = dechex((hexdec($rand_bytes[16])&3) + 0xC/*11__*/); |
||
362 | $rand_bytes[16] = '?'; // was partially overwritten by variant |
||
363 | echo sprintf("%-32s %s\n", "Random bytes:", "[0x$rand_bytes]"); |
||
364 | echo sprintf("%-32s %s\n", "", " ^"); |
||
365 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
||
366 | |||
2 | daniel-mar | 367 | break; |
368 | case 5: |
||
28 | daniel-mar | 369 | /* |
370 | Variant 1, Version 5 UUID |
||
371 | - 48 bit Hash High |
||
372 | - 4 bit Version (fix 0x2) |
||
373 | - 12 bit Hash Mid |
||
374 | - 2 bit Variant (fix 0b10) |
||
375 | - 62 bit Hash Low |
||
376 | */ |
||
377 | |||
25 | daniel-mar | 378 | echo sprintf("%-32s %s\n", "Version:", "[5] Name-based (SHA-1 hash)"); |
2 | daniel-mar | 379 | |
380 | $hash = str_replace('-', '', strtolower($uuid)); |
||
27 | daniel-mar | 381 | |
2 | daniel-mar | 382 | $hash[12] = '?'; // was overwritten by version |
27 | daniel-mar | 383 | |
384 | $var16a = dechex((hexdec($hash[16])&3) + 0x0/*00__*/); |
||
385 | $var16b = dechex((hexdec($hash[16])&3) + 0x4/*01__*/); |
||
386 | $var16c = dechex((hexdec($hash[16])&3) + 0x8/*10__*/); |
||
387 | $var16d = dechex((hexdec($hash[16])&3) + 0xC/*11__*/); |
||
2 | daniel-mar | 388 | $hash[16] = '?'; // was partially overwritten by variant |
27 | daniel-mar | 389 | |
2 | daniel-mar | 390 | $hash .= '????????'; // was cut off |
391 | |||
27 | daniel-mar | 392 | echo sprintf("%-32s %s\n", "SHA1(Namespace+Subject):", "[0x$hash]"); |
393 | echo sprintf("%-32s %s\n", "", " ^"); |
||
394 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
||
2 | daniel-mar | 395 | |
396 | break; |
||
27 | daniel-mar | 397 | case 6: |
28 | daniel-mar | 398 | echo sprintf("%-32s %s\n", "Version:", "[$version] Reordered Time (NOT IMPLEMENTED YET)"); // TODO: implement |
27 | daniel-mar | 399 | break; |
400 | case 7: |
||
28 | daniel-mar | 401 | echo sprintf("%-32s %s\n", "Version:", "[$version] Unix Epoch Time (NOT IMPLEMENTED YET)"); // TODO: implement |
27 | daniel-mar | 402 | break; |
403 | case 8: |
||
28 | daniel-mar | 404 | echo sprintf("%-32s %s\n", "Version:", "[$version] Custom (NOT IMPLEMENTED YET)"); // TODO: implement |
27 | daniel-mar | 405 | break; |
2 | daniel-mar | 406 | default: |
25 | daniel-mar | 407 | echo sprintf("%-32s %s\n", "Version:", "[$version] Unknown"); |
2 | daniel-mar | 408 | break; |
409 | } |
||
410 | |||
411 | break; |
||
412 | case 2: |
||
27 | daniel-mar | 413 | echo sprintf("%-32s %s\n", "Variant:", "[0b110] Reserved for Microsoft Corporation"); |
2 | daniel-mar | 414 | break; |
415 | case 3: |
||
27 | daniel-mar | 416 | echo sprintf("%-32s %s\n", "Variant:", "[0b111] Reserved for future use"); |
2 | daniel-mar | 417 | break; |
418 | } |
||
28 | daniel-mar | 419 | |
420 | if (!$echo) { |
||
421 | $out = ob_get_contents(); |
||
422 | ob_end_clean(); |
||
423 | return $out; |
||
424 | } |
||
2 | daniel-mar | 425 | } |
426 | |||
427 | function uuid_canonize($uuid) { |
||
428 | if (!uuid_valid($uuid)) return false; |
||
429 | return oid_to_uuid(uuid_to_oid($uuid)); |
||
430 | } |
||
431 | |||
432 | function oid_to_uuid($oid) { |
||
433 | if (!is_uuid_oid($oid)) return false; |
||
434 | |||
8 | daniel-mar | 435 | if (substr($oid,0,1) == '.') { |
2 | daniel-mar | 436 | $oid = substr($oid, 1); |
437 | } |
||
438 | $ary = explode('.', $oid); |
||
439 | |||
440 | if (!isset($ary[2])) return false; |
||
441 | |||
442 | $val = $ary[2]; |
||
443 | |||
444 | $x = gmp_init($val, 10); |
||
445 | $y = gmp_strval($x, 16); |
||
446 | $y = str_pad($y, 32, "0", STR_PAD_LEFT); |
||
447 | return substr($y, 0, 8).'-'. |
||
448 | substr($y, 8, 4).'-'. |
||
449 | substr($y, 12, 4).'-'. |
||
450 | substr($y, 16, 4).'-'. |
||
451 | substr($y, 20, 12); |
||
452 | } |
||
453 | |||
454 | function is_uuid_oid($oid, $only_allow_root=false) { |
||
9 | daniel-mar | 455 | if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot |
2 | daniel-mar | 456 | |
457 | $ary = explode('.', $oid); |
||
458 | |||
459 | if ($only_allow_root) { |
||
460 | if (count($ary) != 3) return false; |
||
461 | } else { |
||
462 | if (count($ary) < 3) return false; |
||
463 | } |
||
464 | |||
465 | if ($ary[0] != '2') return false; |
||
466 | if ($ary[1] != '25') return false; |
||
467 | for ($i=2; $i<count($ary); $i++) { |
||
468 | $v = $ary[$i]; |
||
469 | if (!is_numeric($v)) return false; |
||
470 | if ($i == 2) { |
||
471 | // Must be in the range of 128 bit UUID |
||
472 | $test = gmp_init($v, 10); |
||
473 | if (strlen(gmp_strval($test, 16)) > 32) return false; |
||
474 | } |
||
475 | if ($v < 0) return false; |
||
476 | } |
||
477 | |||
478 | return true; |
||
479 | } |
||
480 | |||
481 | function uuid_to_oid($uuid) { |
||
482 | if (!uuid_valid($uuid)) return false; |
||
483 | |||
484 | $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
||
485 | $x = gmp_init($uuid, 16); |
||
486 | return '2.25.'.gmp_strval($x, 10); # TODO: parameter with or without leading dot |
||
487 | } |
||
488 | |||
489 | function gen_uuid($prefer_timebased = true) { |
||
490 | $uuid = $prefer_timebased ? gen_uuid_timebased() : false; |
||
491 | if ($uuid === false) $uuid = gen_uuid_random(); |
||
492 | return $uuid; |
||
493 | } |
||
494 | |||
28 | daniel-mar | 495 | // Variant 0 (NCS) UUID |
496 | |||
497 | // Variant 1, Version 1 (Time based) UUID |
||
2 | daniel-mar | 498 | function gen_uuid_timebased() { |
499 | # On Debian: apt-get install php-uuid |
||
500 | # extension_loaded('uuid') |
||
501 | if (function_exists('uuid_create')) { |
||
502 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
||
503 | /* |
||
504 | $x = uuid_create($context); |
||
505 | uuid_make($context, UUID_MAKE_V1); |
||
506 | uuid_export($context, UUID_FMT_STR, $uuid); |
||
507 | return trim($uuid); |
||
508 | */ |
||
509 | |||
510 | # PECL uuid extension like seen in php-uuid at Debian 9 |
||
511 | return trim(uuid_create(UUID_TYPE_TIME)); |
||
512 | } |
||
513 | |||
514 | # On Debian: apt-get install uuid-runtime |
||
515 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
||
516 | $out = array(); |
||
517 | $ec = -1; |
||
518 | exec('uuidgen -t 2>/dev/null', $out, $ec); |
||
519 | if ($ec == 0) return trim($out[0]); |
||
520 | } |
||
521 | |||
522 | # If we hadn't any success yet, then implement the time based generation routine ourselves! |
||
523 | # Based on https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php |
||
524 | |||
525 | $uuid = array( |
||
526 | 'time_low' => 0, /* 32-bit */ |
||
527 | 'time_mid' => 0, /* 16-bit */ |
||
528 | 'time_hi' => 0, /* 16-bit */ |
||
529 | 'clock_seq_hi' => 0, /* 8-bit */ |
||
530 | 'clock_seq_low' => 0, /* 8-bit */ |
||
531 | 'node' => array() /* 48-bit */ |
||
532 | ); |
||
533 | |||
534 | /* |
||
535 | * Get current time in 100 ns intervals. The magic value |
||
536 | * is the offset between UNIX epoch and the UUID UTC |
||
537 | * time base October 15, 1582. |
||
538 | */ |
||
539 | $tp = gettimeofday(); |
||
540 | $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) + 0x01B21DD213814000; |
||
541 | |||
542 | $uuid['time_low'] = $time & 0xffffffff; |
||
543 | /* Work around PHP 32-bit bit-operation limits */ |
||
544 | $high = intval($time / 0xffffffff); |
||
545 | $uuid['time_mid'] = $high & 0xffff; |
||
546 | $uuid['time_hi'] = (($high >> 16) & 0xfff) | (1/*TimeBased*/ << 12); |
||
547 | |||
548 | /* |
||
549 | * We don't support saved state information and generate |
||
550 | * a random clock sequence each time. |
||
551 | */ |
||
24 | daniel-mar | 552 | $uuid['clock_seq_hi'] = 0x80 | _random_int(0, 64); |
553 | $uuid['clock_seq_low'] = _random_int(0, 255); |
||
2 | daniel-mar | 554 | |
555 | /* |
||
556 | * Node should be set to the 48-bit IEEE node identifier |
||
557 | */ |
||
558 | $mac = get_mac_address(); |
||
559 | if ($mac) { |
||
25 | daniel-mar | 560 | $node = str_replace('-','',str_replace(':','',$mac)); |
2 | daniel-mar | 561 | for ($i = 0; $i < 6; $i++) { |
562 | $uuid['node'][$i] = hexdec(substr($node, $i*2, 2)); |
||
563 | } |
||
564 | |||
565 | /* |
||
566 | * Now output the UUID |
||
567 | */ |
||
568 | return sprintf( |
||
569 | '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x', |
||
570 | ($uuid['time_low']), ($uuid['time_mid']), ($uuid['time_hi']), |
||
571 | $uuid['clock_seq_hi'], $uuid['clock_seq_low'], |
||
572 | $uuid['node'][0], $uuid['node'][1], $uuid['node'][2], |
||
573 | $uuid['node'][3], $uuid['node'][4], $uuid['node'][5]); |
||
574 | } |
||
575 | |||
576 | # We cannot generate the timebased UUID! |
||
577 | return false; |
||
578 | } |
||
579 | |||
580 | function get_mac_address() { |
||
581 | static $detected_mac = false; |
||
582 | |||
583 | if ($detected_mac !== false) { // false NOT null! |
||
584 | return $detected_mac; |
||
585 | } |
||
586 | |||
587 | // TODO: This should actually be part of mac_utils.inc.php, but we need it |
||
588 | // here, and mac_utils.inc.php shall only be optional. What to do? |
||
589 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
590 | // Windows |
||
591 | $cmds = array( |
||
592 | "ipconfig /all", // faster |
||
593 | "getmac" |
||
594 | ); |
||
595 | foreach ($cmds as $cmd) { |
||
596 | $out = array(); |
||
597 | $ec = -1; |
||
598 | exec($cmd, $out, $ec); |
||
599 | if ($ec == 0) { |
||
600 | $out = implode("\n",$out); |
||
601 | $m = array(); |
||
602 | 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 | 603 | $detected_mac = strtolower($m[1]); |
2 | daniel-mar | 604 | return $detected_mac; |
605 | } |
||
606 | } |
||
607 | } |
||
608 | } else if (strtoupper(PHP_OS) == 'DARWIN') { |
||
609 | // Mac OS X |
||
610 | $cmds = array( |
||
611 | "networksetup -listallhardwareports 2>/dev/null", |
||
612 | "netstat -i 2>/dev/null" |
||
613 | ); |
||
614 | foreach ($cmds as $cmd) { |
||
615 | $out = array(); |
||
616 | $ec = -1; |
||
617 | exec($cmd, $out, $ec); |
||
618 | if ($ec == 0) { |
||
619 | $out = implode("\n",$out); |
||
620 | $m = array(); |
||
621 | 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)) { |
||
622 | $detected_mac = $m[1]; |
||
623 | return $detected_mac; |
||
624 | } |
||
625 | } |
||
626 | } |
||
627 | } else { |
||
628 | // Linux |
||
8 | daniel-mar | 629 | $addresses = @glob('/sys/class/net/'.'*'.'/address'); |
630 | foreach ($addresses as $x) { |
||
2 | daniel-mar | 631 | if (!strstr($x,'/lo/')) { |
632 | $detected_mac = trim(file_get_contents($x)); |
||
633 | return $detected_mac; |
||
634 | } |
||
635 | } |
||
636 | $cmds = array( |
||
637 | "netstat -ie 2>/dev/null", |
||
638 | "ifconfig 2>/dev/null" // only available for root (because it is in sbin) |
||
639 | ); |
||
640 | foreach ($cmds as $cmd) { |
||
641 | $out = array(); |
||
642 | $ec = -1; |
||
643 | exec($cmd, $out, $ec); |
||
644 | if ($ec == 0) { |
||
645 | $out = implode("\n",$out); |
||
646 | $m = array(); |
||
647 | 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)) { |
||
648 | $detected_mac = $m[1]; |
||
649 | return $detected_mac; |
||
650 | } |
||
651 | } |
||
652 | } |
||
653 | } |
||
654 | |||
655 | $detected_mac = null; |
||
656 | return $detected_mac; |
||
657 | } |
||
658 | |||
28 | daniel-mar | 659 | // Variant 1, Version 2 (DCE Security) UUID |
27 | daniel-mar | 660 | define('DCE_DOMAIN_PERSON', 0); |
661 | define('DCE_DOMAIN_GROUP', 1); |
||
662 | define('DCE_DOMAIN_ORG', 2); |
||
2 | daniel-mar | 663 | function gen_uuid_dce($domain, $id) { |
664 | # Start with a version 1 UUID |
||
665 | $uuid = gen_uuid_timebased(); |
||
666 | |||
27 | daniel-mar | 667 | # Add Domain Number |
2 | daniel-mar | 668 | $uuid = str_pad(dechex($id), 8, '0', STR_PAD_LEFT) . substr($uuid, 8); |
669 | |||
27 | daniel-mar | 670 | # Add Domain (this overwrites part of the clock sequence) |
2 | daniel-mar | 671 | $uuid = substr($uuid,0,21) . str_pad(dechex($domain), 2, '0', STR_PAD_LEFT) . substr($uuid, 23); |
672 | |||
673 | # Change version to 2 |
||
674 | $uuid[14] = '2'; |
||
675 | |||
676 | return $uuid; |
||
677 | } |
||
678 | |||
28 | daniel-mar | 679 | // Variant 1, Version 3 (MD5 name based) UUID |
2 | daniel-mar | 680 | function gen_uuid_md5_namebased($namespace_uuid, $name) { |
681 | if (!uuid_valid($namespace_uuid)) return false; |
||
682 | $namespace_uuid = uuid_canonize($namespace_uuid); |
||
683 | $namespace_uuid = str_replace('-', '', $namespace_uuid); |
||
684 | $namespace_uuid = hex2bin($namespace_uuid); |
||
685 | |||
686 | $hash = md5($namespace_uuid.$name); |
||
687 | $hash[12] = '3'; // Set version: 3 = MD5 |
||
688 | $hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "10xx" (RFC4122) |
||
689 | |||
690 | return substr($hash, 0, 8).'-'. |
||
691 | substr($hash, 8, 4).'-'. |
||
692 | substr($hash, 12, 4).'-'. |
||
693 | substr($hash, 16, 4).'-'. |
||
694 | substr($hash, 20, 12); |
||
695 | } |
||
696 | |||
28 | daniel-mar | 697 | // Variant 1, Version 4 (Random) UUID |
2 | daniel-mar | 698 | function gen_uuid_random() { |
699 | # On Windows: Requires |
||
700 | # extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext" |
||
701 | # extension=com_dotnet |
||
702 | if (function_exists('com_create_guid')) { |
||
703 | return strtolower(trim(com_create_guid(), '{}')); |
||
704 | } |
||
705 | |||
706 | # On Debian: apt-get install php-uuid |
||
707 | # extension_loaded('uuid') |
||
708 | if (function_exists('uuid_create')) { |
||
709 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
||
710 | /* |
||
711 | $x = uuid_create($context); |
||
712 | uuid_make($context, UUID_MAKE_V4); |
||
713 | uuid_export($context, UUID_FMT_STR, $uuid); |
||
714 | return trim($uuid); |
||
715 | */ |
||
716 | |||
717 | # PECL uuid extension like seen in php-uuid at Debian 9 |
||
718 | return trim(uuid_create(UUID_TYPE_RANDOM)); |
||
719 | } |
||
720 | |||
721 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
||
722 | # On Debian: apt-get install uuid-runtime |
||
723 | $out = array(); |
||
724 | $ec = -1; |
||
725 | exec('uuidgen -r 2>/dev/null', $out, $ec); |
||
726 | if ($ec == 0) return trim($out[0]); |
||
727 | |||
728 | # On Debian Jessie: UUID V4 (Random) |
||
729 | if (file_exists('/proc/sys/kernel/random/uuid')) { |
||
730 | return trim(file_get_contents('/proc/sys/kernel/random/uuid')); |
||
731 | } |
||
732 | } |
||
733 | |||
734 | # Make the UUID by ourselves |
||
735 | # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php |
||
736 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
||
24 | daniel-mar | 737 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), |
738 | _random_int( 0, 0xffff ), |
||
739 | _random_int( 0, 0x0fff ) | 0x4000, |
||
740 | _random_int( 0, 0x3fff ) | 0x8000, |
||
741 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), _random_int( 0, 0xffff ) |
||
2 | daniel-mar | 742 | ); |
743 | } |
||
744 | |||
28 | daniel-mar | 745 | // Variant 1, Version 5 (SHA1 name based) UUID |
2 | daniel-mar | 746 | function gen_uuid_sha1_namebased($namespace_uuid, $name) { |
747 | $namespace_uuid = str_replace('-', '', $namespace_uuid); |
||
748 | $namespace_uuid = hex2bin($namespace_uuid); |
||
749 | |||
750 | $hash = sha1($namespace_uuid.$name); |
||
751 | $hash[12] = '5'; // Set version: 5 = SHA1 |
||
27 | daniel-mar | 752 | $hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "0b10__" (RFC4122/DCE1.1) |
2 | daniel-mar | 753 | |
754 | return substr($hash, 0, 8).'-'. |
||
755 | substr($hash, 8, 4).'-'. |
||
756 | substr($hash, 12, 4).'-'. |
||
757 | substr($hash, 16, 4).'-'. |
||
758 | substr($hash, 20, 12); |
||
759 | } |
||
760 | |||
761 | function uuid_numeric_value($uuid) { |
||
762 | $oid = uuid_to_oid($uuid); |
||
763 | if (!$oid) return false; |
||
764 | return substr($oid, strlen('2.25.')); |
||
765 | } |
||
766 | |||
767 | function uuid_c_syntax($uuid) { |
||
768 | $uuid = str_replace('{', '', $uuid); |
||
769 | return '{ 0x' . substr($uuid, 0, 8) . |
||
770 | ', 0x' . substr($uuid, 9, 4) . |
||
771 | ', 0x' . substr($uuid, 14, 4) . |
||
772 | ', { 0x' . substr($uuid, 19, 2). |
||
773 | ', 0x' . substr($uuid, 21, 2) . |
||
774 | ', 0x' . substr($uuid, 24, 2) . |
||
775 | ', 0x' . substr($uuid, 26, 2) . |
||
776 | ', 0x' . substr($uuid, 28, 2) . |
||
777 | ', 0x' . substr($uuid, 30, 2) . |
||
778 | ', 0x' . substr($uuid, 32, 2) . |
||
779 | ', 0x' . substr($uuid, 34, 2) . ' } }'; |
||
780 | } |
||
781 | |||
782 | # --- |
||
783 | |||
784 | // http://php.net/manual/de/function.hex2bin.php#113057 |
||
785 | if ( !function_exists( 'hex2bin' ) ) { |
||
786 | function hex2bin( $str ) { |
||
787 | $sbin = ""; |
||
788 | $len = strlen( $str ); |
||
789 | for ( $i = 0; $i < $len; $i += 2 ) { |
||
790 | $sbin .= pack( "H*", substr( $str, $i, 2 ) ); |
||
791 | } |
||
792 | |||
793 | return $sbin; |
||
794 | } |
||
795 | } |