Rev 29 | Rev 31 | 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 | |||
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: |
||
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 |
||
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 | */ |
||
204 | echo sprintf("%-32s %s\n", "Version:", "[$version] Reordered Time"); |
||
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 | |||
29 | daniel-mar | 227 | if ($version == 1) echo sprintf("%-32s %s\n", "Version:", "[1] Time-based with unique random 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 | |
350 | $var16a = dechex((hexdec($hash[16])&3) + 0x0/*00__*/); |
||
351 | $var16b = dechex((hexdec($hash[16])&3) + 0x4/*01__*/); |
||
352 | $var16c = dechex((hexdec($hash[16])&3) + 0x8/*10__*/); |
||
353 | $var16d = 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 |
||
399 | $var16a = dechex((hexdec($rand_bytes[16])&3) + 0x0/*00__*/); |
||
400 | $var16b = dechex((hexdec($rand_bytes[16])&3) + 0x4/*01__*/); |
||
401 | $var16c = dechex((hexdec($rand_bytes[16])&3) + 0x8/*10__*/); |
||
402 | $var16d = dechex((hexdec($rand_bytes[16])&3) + 0xC/*11__*/); |
||
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 | |
425 | $var16a = dechex((hexdec($hash[16])&3) + 0x0/*00__*/); |
||
426 | $var16b = dechex((hexdec($hash[16])&3) + 0x4/*01__*/); |
||
427 | $var16c = dechex((hexdec($hash[16])&3) + 0x8/*10__*/); |
||
428 | $var16d = 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 | |||
448 | echo sprintf("%-32s %s\n", "Version:", "[$version] Unix Epoch Time"); |
||
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); |
||
483 | $var16a = dechex((hexdec($rand_bytes[3])&3) + 0x0/*00__*/); |
||
484 | $var16b = dechex((hexdec($rand_bytes[3])&3) + 0x4/*01__*/); |
||
485 | $var16c = dechex((hexdec($rand_bytes[3])&3) + 0x8/*10__*/); |
||
486 | $var16d = dechex((hexdec($rand_bytes[3])&3) + 0xC/*11__*/); |
||
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 | |||
507 | echo sprintf("%-32s %s\n", "Version:", "[$version] Custom implementation"); |
||
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 | |||
513 | echo sprintf("%-32s %s\n", "Custom data:", "[0x$custom_data]"); |
||
514 | |||
515 | |||
27 | daniel-mar | 516 | break; |
2 | daniel-mar | 517 | default: |
25 | daniel-mar | 518 | echo sprintf("%-32s %s\n", "Version:", "[$version] Unknown"); |
2 | daniel-mar | 519 | break; |
520 | } |
||
521 | |||
522 | break; |
||
523 | case 2: |
||
27 | daniel-mar | 524 | echo sprintf("%-32s %s\n", "Variant:", "[0b110] Reserved for Microsoft Corporation"); |
2 | daniel-mar | 525 | break; |
526 | case 3: |
||
27 | daniel-mar | 527 | echo sprintf("%-32s %s\n", "Variant:", "[0b111] Reserved for future use"); |
2 | daniel-mar | 528 | break; |
529 | } |
||
28 | daniel-mar | 530 | |
531 | if (!$echo) { |
||
532 | $out = ob_get_contents(); |
||
533 | ob_end_clean(); |
||
534 | return $out; |
||
535 | } |
||
2 | daniel-mar | 536 | } |
537 | |||
538 | function uuid_canonize($uuid) { |
||
539 | if (!uuid_valid($uuid)) return false; |
||
540 | return oid_to_uuid(uuid_to_oid($uuid)); |
||
541 | } |
||
542 | |||
543 | function oid_to_uuid($oid) { |
||
544 | if (!is_uuid_oid($oid)) return false; |
||
545 | |||
8 | daniel-mar | 546 | if (substr($oid,0,1) == '.') { |
2 | daniel-mar | 547 | $oid = substr($oid, 1); |
548 | } |
||
549 | $ary = explode('.', $oid); |
||
550 | |||
551 | if (!isset($ary[2])) return false; |
||
552 | |||
553 | $val = $ary[2]; |
||
554 | |||
555 | $x = gmp_init($val, 10); |
||
556 | $y = gmp_strval($x, 16); |
||
557 | $y = str_pad($y, 32, "0", STR_PAD_LEFT); |
||
558 | return substr($y, 0, 8).'-'. |
||
559 | substr($y, 8, 4).'-'. |
||
560 | substr($y, 12, 4).'-'. |
||
561 | substr($y, 16, 4).'-'. |
||
562 | substr($y, 20, 12); |
||
563 | } |
||
564 | |||
565 | function is_uuid_oid($oid, $only_allow_root=false) { |
||
9 | daniel-mar | 566 | if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot |
2 | daniel-mar | 567 | |
568 | $ary = explode('.', $oid); |
||
569 | |||
570 | if ($only_allow_root) { |
||
571 | if (count($ary) != 3) return false; |
||
572 | } else { |
||
573 | if (count($ary) < 3) return false; |
||
574 | } |
||
575 | |||
576 | if ($ary[0] != '2') return false; |
||
577 | if ($ary[1] != '25') return false; |
||
578 | for ($i=2; $i<count($ary); $i++) { |
||
579 | $v = $ary[$i]; |
||
580 | if (!is_numeric($v)) return false; |
||
581 | if ($i == 2) { |
||
582 | // Must be in the range of 128 bit UUID |
||
583 | $test = gmp_init($v, 10); |
||
584 | if (strlen(gmp_strval($test, 16)) > 32) return false; |
||
585 | } |
||
586 | if ($v < 0) return false; |
||
587 | } |
||
588 | |||
589 | return true; |
||
590 | } |
||
591 | |||
592 | function uuid_to_oid($uuid) { |
||
593 | if (!uuid_valid($uuid)) return false; |
||
594 | |||
595 | $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
||
596 | $x = gmp_init($uuid, 16); |
||
29 | daniel-mar | 597 | return '2.25.'.gmp_strval($x, 10); |
2 | daniel-mar | 598 | } |
599 | |||
30 | daniel-mar | 600 | function gen_uuid($prefer_mac_address_based = true) { |
601 | $uuid = $prefer_mac_address_based ? gen_uuid_reordered()/*UUIDv6*/ : false; |
||
602 | if ($uuid === false) $uuid = gen_uuid_unix_epoch()/*UUIDv7*/; |
||
2 | daniel-mar | 603 | return $uuid; |
604 | } |
||
605 | |||
30 | daniel-mar | 606 | # -------------------------------------- |
607 | // Variant 1, Version 1 (Time based) UUID |
||
608 | # -------------------------------------- |
||
28 | daniel-mar | 609 | |
30 | daniel-mar | 610 | function gen_uuid_v1() { |
611 | return gen_uuid_timebased(); |
||
612 | } |
||
2 | daniel-mar | 613 | function gen_uuid_timebased() { |
614 | # On Debian: apt-get install php-uuid |
||
615 | # extension_loaded('uuid') |
||
616 | if (function_exists('uuid_create')) { |
||
617 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
||
618 | /* |
||
619 | $x = uuid_create($context); |
||
620 | uuid_make($context, UUID_MAKE_V1); |
||
621 | uuid_export($context, UUID_FMT_STR, $uuid); |
||
622 | return trim($uuid); |
||
623 | */ |
||
624 | |||
625 | # PECL uuid extension like seen in php-uuid at Debian 9 |
||
626 | return trim(uuid_create(UUID_TYPE_TIME)); |
||
627 | } |
||
628 | |||
629 | # On Debian: apt-get install uuid-runtime |
||
630 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
||
631 | $out = array(); |
||
632 | $ec = -1; |
||
633 | exec('uuidgen -t 2>/dev/null', $out, $ec); |
||
634 | if ($ec == 0) return trim($out[0]); |
||
635 | } |
||
636 | |||
637 | # If we hadn't any success yet, then implement the time based generation routine ourselves! |
||
638 | # Based on https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php |
||
639 | |||
640 | $uuid = array( |
||
641 | 'time_low' => 0, /* 32-bit */ |
||
642 | 'time_mid' => 0, /* 16-bit */ |
||
643 | 'time_hi' => 0, /* 16-bit */ |
||
644 | 'clock_seq_hi' => 0, /* 8-bit */ |
||
645 | 'clock_seq_low' => 0, /* 8-bit */ |
||
646 | 'node' => array() /* 48-bit */ |
||
647 | ); |
||
648 | |||
649 | /* |
||
650 | * Get current time in 100 ns intervals. The magic value |
||
651 | * is the offset between UNIX epoch and the UUID UTC |
||
652 | * time base October 15, 1582. |
||
653 | */ |
||
29 | daniel-mar | 654 | usleep(1); // make sure the timestamp is not used before |
2 | daniel-mar | 655 | $tp = gettimeofday(); |
656 | $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) + 0x01B21DD213814000; |
||
657 | |||
658 | $uuid['time_low'] = $time & 0xffffffff; |
||
659 | /* Work around PHP 32-bit bit-operation limits */ |
||
660 | $high = intval($time / 0xffffffff); |
||
661 | $uuid['time_mid'] = $high & 0xffff; |
||
662 | $uuid['time_hi'] = (($high >> 16) & 0xfff) | (1/*TimeBased*/ << 12); |
||
663 | |||
664 | /* |
||
665 | * We don't support saved state information and generate |
||
666 | * a random clock sequence each time. |
||
667 | */ |
||
24 | daniel-mar | 668 | $uuid['clock_seq_hi'] = 0x80 | _random_int(0, 64); |
669 | $uuid['clock_seq_low'] = _random_int(0, 255); |
||
2 | daniel-mar | 670 | |
671 | /* |
||
672 | * Node should be set to the 48-bit IEEE node identifier |
||
673 | */ |
||
674 | $mac = get_mac_address(); |
||
675 | if ($mac) { |
||
25 | daniel-mar | 676 | $node = str_replace('-','',str_replace(':','',$mac)); |
2 | daniel-mar | 677 | for ($i = 0; $i < 6; $i++) { |
678 | $uuid['node'][$i] = hexdec(substr($node, $i*2, 2)); |
||
679 | } |
||
680 | |||
681 | /* |
||
682 | * Now output the UUID |
||
683 | */ |
||
684 | return sprintf( |
||
685 | '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x', |
||
686 | ($uuid['time_low']), ($uuid['time_mid']), ($uuid['time_hi']), |
||
687 | $uuid['clock_seq_hi'], $uuid['clock_seq_low'], |
||
688 | $uuid['node'][0], $uuid['node'][1], $uuid['node'][2], |
||
689 | $uuid['node'][3], $uuid['node'][4], $uuid['node'][5]); |
||
690 | } |
||
691 | |||
692 | # We cannot generate the timebased UUID! |
||
693 | return false; |
||
694 | } |
||
695 | function get_mac_address() { |
||
696 | static $detected_mac = false; |
||
697 | |||
698 | if ($detected_mac !== false) { // false NOT null! |
||
699 | return $detected_mac; |
||
700 | } |
||
701 | |||
702 | // TODO: This should actually be part of mac_utils.inc.php, but we need it |
||
703 | // here, and mac_utils.inc.php shall only be optional. What to do? |
||
704 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
705 | // Windows |
||
706 | $cmds = array( |
||
707 | "ipconfig /all", // faster |
||
708 | "getmac" |
||
709 | ); |
||
710 | foreach ($cmds as $cmd) { |
||
711 | $out = array(); |
||
712 | $ec = -1; |
||
713 | exec($cmd, $out, $ec); |
||
714 | if ($ec == 0) { |
||
715 | $out = implode("\n",$out); |
||
716 | $m = array(); |
||
717 | 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 | 718 | $detected_mac = strtolower($m[1]); |
2 | daniel-mar | 719 | return $detected_mac; |
720 | } |
||
721 | } |
||
722 | } |
||
723 | } else if (strtoupper(PHP_OS) == 'DARWIN') { |
||
724 | // Mac OS X |
||
725 | $cmds = array( |
||
726 | "networksetup -listallhardwareports 2>/dev/null", |
||
727 | "netstat -i 2>/dev/null" |
||
728 | ); |
||
729 | foreach ($cmds as $cmd) { |
||
730 | $out = array(); |
||
731 | $ec = -1; |
||
732 | exec($cmd, $out, $ec); |
||
733 | if ($ec == 0) { |
||
734 | $out = implode("\n",$out); |
||
735 | $m = array(); |
||
736 | 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)) { |
||
737 | $detected_mac = $m[1]; |
||
738 | return $detected_mac; |
||
739 | } |
||
740 | } |
||
741 | } |
||
742 | } else { |
||
743 | // Linux |
||
8 | daniel-mar | 744 | $addresses = @glob('/sys/class/net/'.'*'.'/address'); |
745 | foreach ($addresses as $x) { |
||
2 | daniel-mar | 746 | if (!strstr($x,'/lo/')) { |
747 | $detected_mac = trim(file_get_contents($x)); |
||
748 | return $detected_mac; |
||
749 | } |
||
750 | } |
||
751 | $cmds = array( |
||
752 | "netstat -ie 2>/dev/null", |
||
753 | "ifconfig 2>/dev/null" // only available for root (because it is in sbin) |
||
754 | ); |
||
755 | foreach ($cmds as $cmd) { |
||
756 | $out = array(); |
||
757 | $ec = -1; |
||
758 | exec($cmd, $out, $ec); |
||
759 | if ($ec == 0) { |
||
760 | $out = implode("\n",$out); |
||
761 | $m = array(); |
||
762 | 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)) { |
||
763 | $detected_mac = $m[1]; |
||
764 | return $detected_mac; |
||
765 | } |
||
766 | } |
||
767 | } |
||
768 | } |
||
769 | |||
770 | $detected_mac = null; |
||
771 | return $detected_mac; |
||
772 | } |
||
773 | |||
30 | daniel-mar | 774 | # -------------------------------------- |
28 | daniel-mar | 775 | // Variant 1, Version 2 (DCE Security) UUID |
30 | daniel-mar | 776 | # -------------------------------------- |
777 | |||
27 | daniel-mar | 778 | define('DCE_DOMAIN_PERSON', 0); |
779 | define('DCE_DOMAIN_GROUP', 1); |
||
780 | define('DCE_DOMAIN_ORG', 2); |
||
30 | daniel-mar | 781 | function gen_uuid_v2($domain, $id) { |
782 | return gen_uuid_dce($domain, $id); |
||
783 | } |
||
2 | daniel-mar | 784 | function gen_uuid_dce($domain, $id) { |
785 | # Start with a version 1 UUID |
||
786 | $uuid = gen_uuid_timebased(); |
||
787 | |||
27 | daniel-mar | 788 | # Add Domain Number |
2 | daniel-mar | 789 | $uuid = str_pad(dechex($id), 8, '0', STR_PAD_LEFT) . substr($uuid, 8); |
790 | |||
27 | daniel-mar | 791 | # Add Domain (this overwrites part of the clock sequence) |
2 | daniel-mar | 792 | $uuid = substr($uuid,0,21) . str_pad(dechex($domain), 2, '0', STR_PAD_LEFT) . substr($uuid, 23); |
793 | |||
794 | # Change version to 2 |
||
795 | $uuid[14] = '2'; |
||
796 | |||
797 | return $uuid; |
||
798 | } |
||
799 | |||
30 | daniel-mar | 800 | # -------------------------------------- |
28 | daniel-mar | 801 | // Variant 1, Version 3 (MD5 name based) UUID |
30 | daniel-mar | 802 | # -------------------------------------- |
803 | |||
804 | function gen_uuid_v3($namespace_uuid, $name) { |
||
805 | return gen_uuid_md5_namebased($namespace_uuid, $name); |
||
806 | } |
||
2 | daniel-mar | 807 | function gen_uuid_md5_namebased($namespace_uuid, $name) { |
808 | if (!uuid_valid($namespace_uuid)) return false; |
||
809 | $namespace_uuid = uuid_canonize($namespace_uuid); |
||
810 | $namespace_uuid = str_replace('-', '', $namespace_uuid); |
||
811 | $namespace_uuid = hex2bin($namespace_uuid); |
||
812 | |||
813 | $hash = md5($namespace_uuid.$name); |
||
814 | $hash[12] = '3'; // Set version: 3 = MD5 |
||
815 | $hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "10xx" (RFC4122) |
||
816 | |||
817 | return substr($hash, 0, 8).'-'. |
||
818 | substr($hash, 8, 4).'-'. |
||
819 | substr($hash, 12, 4).'-'. |
||
820 | substr($hash, 16, 4).'-'. |
||
821 | substr($hash, 20, 12); |
||
822 | } |
||
823 | |||
30 | daniel-mar | 824 | # -------------------------------------- |
28 | daniel-mar | 825 | // Variant 1, Version 4 (Random) UUID |
30 | daniel-mar | 826 | # -------------------------------------- |
827 | |||
828 | function gen_uuid_v4() { |
||
829 | return gen_uuid_random(); |
||
830 | } |
||
2 | daniel-mar | 831 | function gen_uuid_random() { |
832 | # On Windows: Requires |
||
833 | # extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext" |
||
834 | # extension=com_dotnet |
||
30 | daniel-mar | 835 | // TODO: can we trust that it always outputs UUIDv4? |
836 | /* |
||
2 | daniel-mar | 837 | if (function_exists('com_create_guid')) { |
838 | return strtolower(trim(com_create_guid(), '{}')); |
||
839 | } |
||
30 | daniel-mar | 840 | */ |
2 | daniel-mar | 841 | |
842 | # On Debian: apt-get install php-uuid |
||
843 | # extension_loaded('uuid') |
||
844 | if (function_exists('uuid_create')) { |
||
845 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
||
846 | /* |
||
847 | $x = uuid_create($context); |
||
848 | uuid_make($context, UUID_MAKE_V4); |
||
849 | uuid_export($context, UUID_FMT_STR, $uuid); |
||
850 | return trim($uuid); |
||
851 | */ |
||
852 | |||
853 | # PECL uuid extension like seen in php-uuid at Debian 9 |
||
854 | return trim(uuid_create(UUID_TYPE_RANDOM)); |
||
855 | } |
||
856 | |||
857 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
||
858 | # On Debian: apt-get install uuid-runtime |
||
859 | $out = array(); |
||
860 | $ec = -1; |
||
861 | exec('uuidgen -r 2>/dev/null', $out, $ec); |
||
862 | if ($ec == 0) return trim($out[0]); |
||
863 | |||
864 | # On Debian Jessie: UUID V4 (Random) |
||
865 | if (file_exists('/proc/sys/kernel/random/uuid')) { |
||
866 | return trim(file_get_contents('/proc/sys/kernel/random/uuid')); |
||
867 | } |
||
868 | } |
||
869 | |||
870 | # Make the UUID by ourselves |
||
871 | # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php |
||
872 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
||
24 | daniel-mar | 873 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), |
874 | _random_int( 0, 0xffff ), |
||
875 | _random_int( 0, 0x0fff ) | 0x4000, |
||
876 | _random_int( 0, 0x3fff ) | 0x8000, |
||
877 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), _random_int( 0, 0xffff ) |
||
2 | daniel-mar | 878 | ); |
879 | } |
||
880 | |||
30 | daniel-mar | 881 | # -------------------------------------- |
28 | daniel-mar | 882 | // Variant 1, Version 5 (SHA1 name based) UUID |
30 | daniel-mar | 883 | # -------------------------------------- |
884 | |||
885 | function gen_uuid_v5($namespace_uuid, $name) { |
||
886 | return gen_uuid_sha1_namebased($namespace_uuid, $name); |
||
887 | } |
||
2 | daniel-mar | 888 | function gen_uuid_sha1_namebased($namespace_uuid, $name) { |
889 | $namespace_uuid = str_replace('-', '', $namespace_uuid); |
||
890 | $namespace_uuid = hex2bin($namespace_uuid); |
||
891 | |||
892 | $hash = sha1($namespace_uuid.$name); |
||
893 | $hash[12] = '5'; // Set version: 5 = SHA1 |
||
27 | daniel-mar | 894 | $hash[16] = dechex(hexdec($hash[16]) & 0x3 | 0x8); // Set variant to "0b10__" (RFC4122/DCE1.1) |
2 | daniel-mar | 895 | |
896 | return substr($hash, 0, 8).'-'. |
||
897 | substr($hash, 8, 4).'-'. |
||
898 | substr($hash, 12, 4).'-'. |
||
899 | substr($hash, 16, 4).'-'. |
||
900 | substr($hash, 20, 12); |
||
901 | } |
||
902 | |||
30 | daniel-mar | 903 | # -------------------------------------- |
904 | // Variant 1, Version 6 (Reordered) UUID |
||
905 | # -------------------------------------- |
||
906 | |||
907 | function gen_uuid_v6() { |
||
908 | return gen_uuid_reordered(); |
||
909 | } |
||
910 | function gen_uuid_reordered() { |
||
911 | // Start with a UUIDv1 |
||
912 | $uuid = gen_uuid_timebased(); |
||
913 | |||
914 | // Convert to UUIDv6 |
||
915 | return uuid1_to_uuid6($uuid); |
||
916 | } |
||
917 | function uuid6_to_uuid1($hex) { |
||
918 | $hex = uuid_canonize($hex); |
||
919 | if ($hex === false) return false; |
||
920 | $hex = preg_replace('@[^0-9A-F]@i', '', $hex); |
||
921 | $hex = substr($hex, 7, 5). |
||
922 | substr($hex, 13, 3). |
||
923 | substr($hex, 3, 4). |
||
924 | '1' . substr($hex, 0, 3). |
||
925 | substr($hex, 16); |
||
926 | return substr($hex, 0, 8).'-'. |
||
927 | substr($hex, 8, 4).'-'. |
||
928 | substr($hex, 12, 4).'-'. |
||
929 | substr($hex, 16, 4).'-'. |
||
930 | substr($hex, 20, 12); |
||
931 | } |
||
932 | function uuid1_to_uuid6($hex) { |
||
933 | $hex = uuid_canonize($hex); |
||
934 | if ($hex === false) return false; |
||
935 | $hex = preg_replace('@[^0-9A-F]@i', '', $hex); |
||
936 | $hex = substr($hex, 13, 3). |
||
937 | substr($hex, 8, 4). |
||
938 | substr($hex, 0, 5). |
||
939 | '6' . substr($hex, 5, 3). |
||
940 | substr($hex, 16); |
||
941 | return substr($hex, 0, 8).'-'. |
||
942 | substr($hex, 8, 4).'-'. |
||
943 | substr($hex, 12, 4).'-'. |
||
944 | substr($hex, 16, 4).'-'. |
||
945 | substr($hex, 20, 12); |
||
946 | } |
||
947 | |||
948 | # -------------------------------------- |
||
949 | // Variant 1, Version 7 (Unix Epoch) UUID |
||
950 | # -------------------------------------- |
||
951 | |||
952 | function gen_uuid_v7() { |
||
953 | return gen_uuid_unix_epoch(); |
||
954 | } |
||
955 | function gen_uuid_unix_epoch() { |
||
956 | // Start with an UUIDv4 |
||
957 | $uuid = gen_uuid_random(); |
||
958 | |||
959 | // Add the timestamp |
||
960 | usleep(1); // make sure the timestamp is not repeated |
||
961 | if (function_exists('gmp_init')) { |
||
962 | list($ms,$sec) = explode(' ', microtime(false)); |
||
963 | $sec = gmp_init($sec, 10); |
||
964 | $ms = gmp_init(substr($ms,2,3), 10); |
||
965 | $unix_ts = gmp_strval(gmp_add(gmp_mul($sec, '1000'), $ms),16); |
||
966 | } else { |
||
967 | $unix_ts = dechex((int)round(microtime(true)*1000)); |
||
968 | } |
||
969 | $unix_ts = str_pad($unix_ts, 12, '0', STR_PAD_LEFT); |
||
970 | for ($i=0;$i<8;$i++) $uuid[$i] = substr($unix_ts, $i, 1); |
||
971 | for ($i=0;$i<4;$i++) $uuid[9+$i] = substr($unix_ts, 8+$i, 1); |
||
972 | |||
973 | // set version |
||
974 | $uuid[14] = '7'; |
||
975 | |||
976 | return $uuid; |
||
977 | } |
||
978 | |||
979 | # -------------------------------------- |
||
980 | |||
2 | daniel-mar | 981 | function uuid_numeric_value($uuid) { |
982 | $oid = uuid_to_oid($uuid); |
||
983 | if (!$oid) return false; |
||
984 | return substr($oid, strlen('2.25.')); |
||
985 | } |
||
986 | |||
987 | function uuid_c_syntax($uuid) { |
||
988 | $uuid = str_replace('{', '', $uuid); |
||
989 | return '{ 0x' . substr($uuid, 0, 8) . |
||
990 | ', 0x' . substr($uuid, 9, 4) . |
||
991 | ', 0x' . substr($uuid, 14, 4) . |
||
992 | ', { 0x' . substr($uuid, 19, 2). |
||
993 | ', 0x' . substr($uuid, 21, 2) . |
||
994 | ', 0x' . substr($uuid, 24, 2) . |
||
995 | ', 0x' . substr($uuid, 26, 2) . |
||
996 | ', 0x' . substr($uuid, 28, 2) . |
||
997 | ', 0x' . substr($uuid, 30, 2) . |
||
998 | ', 0x' . substr($uuid, 32, 2) . |
||
999 | ', 0x' . substr($uuid, 34, 2) . ' } }'; |
||
1000 | } |
||
1001 | |||
30 | daniel-mar | 1002 | # -------------------------------------- |
2 | daniel-mar | 1003 | |
1004 | // http://php.net/manual/de/function.hex2bin.php#113057 |
||
1005 | if ( !function_exists( 'hex2bin' ) ) { |
||
1006 | function hex2bin( $str ) { |
||
1007 | $sbin = ""; |
||
1008 | $len = strlen( $str ); |
||
1009 | for ( $i = 0; $i < $len; $i += 2 ) { |
||
1010 | $sbin .= pack( "H*", substr( $str, $i, 2 ) ); |
||
1011 | } |
||
1012 | |||
1013 | return $sbin; |
||
1014 | } |
||
1015 | } |