Rev 34 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 34 | Rev 36 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * UUID utils for PHP |
4 | * UUID utils for PHP |
5 | * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft |
6 | * Version 2023-07-13 |
6 | * Version 2023-11-11 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with 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 |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
Line 19... | Line 19... | ||
19 | */ |
19 | */ |
20 | 20 | ||
21 | # This library requires either the GMP extension (or BCMath if gmp_supplement.inc.php is present) |
21 | # This library requires either the GMP extension (or BCMath if gmp_supplement.inc.php is present) |
22 | // TODO: If we are on 64 bit PHP (PHP_INT_SIZE > 4), then replace GMP with normal PHP operations |
22 | // TODO: If we are on 64 bit PHP (PHP_INT_SIZE > 4), then replace GMP with normal PHP operations |
23 | 23 | ||
24 | if (file_exists(__DIR__ . '/mac_utils.inc.phps')) include_once __DIR__ . '/mac_utils.inc.phps'; // optionally used for uuid_info() |
- | |
25 | if (file_exists(__DIR__ . '/mac_utils.inc.php')) include_once __DIR__ . '/mac_utils.inc.php'; // optionally used for uuid_info() |
24 | if (file_exists($f = __DIR__ . '/mac_utils.inc.php')) include_once $f; |
26 | if (file_exists(__DIR__ . '/gmp_supplement.inc.php')) include_once __DIR__ . '/gmp_supplement.inc.php'; |
25 | else if (file_exists($f = __DIR__ . '/mac_utils.inc.phps')) include_once $f; |
27 | 26 | ||
- | 27 | if (file_exists($f = __DIR__ . '/gmp_supplement.inc.php')) include_once $f; |
|
- | 28 | else if (file_exists($f = __DIR__ . '/gmp_supplement.inc.phps')) include_once $f; |
|
- | 29 | ||
- | 30 | if (file_exists($f = __DIR__ . '/OidDerConverter.class.php')) include_once $f; |
|
- | 31 | else if (file_exists($f = __DIR__ . '/OidDerConverter.class.phps')) include_once $f; |
|
- | 32 | ||
- | 33 | // Note: The RFC allows various notations as payload, not a strict notation constraint |
|
28 | const UUID_NAMEBASED_NS_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; // FQDN |
34 | const UUID_NAMEBASED_NS_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
29 | const UUID_NAMEBASED_NS_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
35 | const UUID_NAMEBASED_NS_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
30 | const UUID_NAMEBASED_NS_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
36 | const UUID_NAMEBASED_NS_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
31 | const UUID_NAMEBASED_NS_X500_DN = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; // "DER or text encoding" according to RFC4122bis |
37 | const UUID_NAMEBASED_NS_X500_DN = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; |
32 | 38 | ||
33 | if (!function_exists('_random_int')) { |
39 | if (!function_exists('_random_int')) { |
34 | function _random_int($min, $max) { |
40 | function _random_int($min, $max) { |
35 | // This function tries a CSRNG and falls back to a RNG if no CSRNG is available |
41 | // This function tries a CSRNG and falls back to a RNG if no CSRNG is available |
36 | try { |
42 | try { |
Line 51... | Line 57... | ||
51 | $uuid = preg_replace('@[0-9A-F]@i', '', $uuid); |
57 | $uuid = preg_replace('@[0-9A-F]@i', '', $uuid); |
52 | 58 | ||
53 | return ($uuid == ''); |
59 | return ($uuid == ''); |
54 | } |
60 | } |
55 | 61 | ||
- | 62 | function uuid_equal($uuid1, $uuid2) { |
|
- | 63 | $uuid1 = uuid_canonize($uuid1); |
|
- | 64 | if (!$uuid1) return false; |
|
- | 65 | $uuid2 = uuid_canonize($uuid2); |
|
- | 66 | if (!$uuid2) return false; |
|
- | 67 | return $uuid1 === $uuid2; |
|
- | 68 | } |
|
- | 69 | ||
- | 70 | function uuid_version($uuid) { |
|
- | 71 | $uuid = uuid_canonize($uuid); |
|
- | 72 | if (!$uuid) return false; |
|
- | 73 | return substr($uuid, 19, 1); |
|
- | 74 | } |
|
- | 75 | ||
56 | function uuid_info($uuid, $echo=true) { |
76 | function uuid_info($uuid, $echo=true) { |
57 | if (!uuid_valid($uuid)) return false; |
77 | if (!uuid_valid($uuid)) return false; |
58 | 78 | ||
- | 79 | $oid = uuid_to_oid($uuid); |
|
- | 80 | ||
- | 81 | echo sprintf("%-32s %s\n", "Your input:", $uuid); |
|
- | 82 | echo "\n"; |
|
- | 83 | echo "<u>Various notations:</u>\n"; |
|
- | 84 | echo "\n"; |
|
- | 85 | echo sprintf("%-32s %s\n", "URN:", 'urn:uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid)))); |
|
- | 86 | echo sprintf("%-32s %s\n", "URI:", 'uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid)))); |
|
- | 87 | echo sprintf("%-32s %s\n", "Microsoft GUID syntax:", '{' . strtoupper(oid_to_uuid(uuid_to_oid($uuid))) . '}'); |
|
- | 88 | echo sprintf("%-32s %s\n", "C++ struct syntax:", uuid_c_syntax($uuid)); |
|
- | 89 | echo "\n"; |
|
- | 90 | ||
- | 91 | echo sprintf("%-32s %s\n", "As OID (ISO/ITU-T 128 bits):", $oid=uuid_to_oid($uuid, '2.25')); |
|
- | 92 | # Removed because it is too much information (we would also need to add this to the other OIDs too) |
|
- | 93 | #if (class_exists('OidDerConverter')) { |
|
- | 94 | # echo sprintf("%-32s %s\n", "DER encoding of OID:", OidDerConverter::hexarrayToStr(OidDerConverter::oidToDER($oid))); |
|
- | 95 | #} |
|
- | 96 | echo sprintf("%-32s %s\n", "As OID (Microsoft):", $oid=uuid_to_oid($uuid, '1.2.840.113556.1.8000.2554')); |
|
- | 97 | echo sprintf("%-32s %s\n", "As OID (Waterjuice 2x64 bits):", $oid=uuid_to_oid($uuid, '1.3.6.1.4.1.54392.1')); |
|
- | 98 | echo sprintf("%-32s %s\n", "As OID (Waterjuice 4x32 bits):", $oid=uuid_to_oid($uuid, '1.3.6.1.4.1.54392.2')); |
|
- | 99 | echo sprintf("%-32s %s\n", "As OID (Waterjuice 8x16 bits):", $oid=uuid_to_oid($uuid, '1.3.6.1.4.1.54392.3')); |
|
- | 100 | ||
- | 101 | echo "\n"; |
|
- | 102 | ||
- | 103 | echo "<u>Interpretation of the UUID:</u>\n\n"; |
|
- | 104 | ||
59 | if (!$echo) ob_start(); |
105 | if (!$echo) ob_start(); |
60 | 106 | ||
61 | #$uuid = trim($uuid); |
107 | #$uuid = trim($uuid); |
62 | # $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
108 | # $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
63 | $uuid = strtolower($uuid); |
109 | $uuid = strtolower($uuid); |
Line 285... | Line 331... | ||
285 | if ($version <= 2) { |
331 | if ($version <= 2) { |
286 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 (Leach-Mealling-Salz) / DCE 1.1"); |
332 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 (Leach-Mealling-Salz) / DCE 1.1"); |
287 | } else if (($version >= 3) && ($version <= 5)) { |
333 | } else if (($version >= 3) && ($version <= 5)) { |
288 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 (Leach-Mealling-Salz)"); |
334 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 (Leach-Mealling-Salz)"); |
289 | } else if (($version >= 6) && ($version <= 8)) { |
335 | } else if (($version >= 6) && ($version <= 8)) { |
290 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122bis (Leach-Mealling-Peabody-Davis)"); |
336 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC draft-ietf-uuidrev-rfc4122bis (Davis-Peabody-Leach)"); // TODO: When new RFC is published, replace the RFC number |
291 | } else { |
337 | } else { |
292 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] RFC 4122 ?"); |
338 | echo sprintf("%-32s %s\n", "Variant:", "[0b10_] Unknown RFC"); |
293 | } |
339 | } |
294 | 340 | ||
295 | switch ($version) { |
341 | switch ($version) { |
296 | case 6: |
342 | case 6: |
297 | /* |
343 | /* |
Line 302... | Line 348... | ||
302 | - 2 bit Variant (fix 0b10) |
348 | - 2 bit Variant (fix 0b10) |
303 | - 6 bit Clock Sequence High |
349 | - 6 bit Clock Sequence High |
304 | - 8 bit Clock Sequence Low |
350 | - 8 bit Clock Sequence Low |
305 | - 48 bit MAC Address |
351 | - 48 bit MAC Address |
306 | */ |
352 | */ |
307 | echo sprintf("%-32s %s\n", "Version:", "[6] Reordered Time"); |
353 | echo sprintf("%-32s %s\n", "Version:", "[0x6] Reordered Time-Based"); |
308 | $uuid = substr($uuid, 0, 8).'-'. |
354 | $uuid = substr($uuid, 0, 8).'-'. |
309 | substr($uuid, 8, 4).'-'. |
355 | substr($uuid, 8, 4).'-'. |
310 | substr($uuid, 12, 4).'-'. |
356 | substr($uuid, 12, 4).'-'. |
311 | substr($uuid, 16, 4).'-'. |
357 | substr($uuid, 16, 4).'-'. |
312 | substr($uuid, 20, 12); |
358 | substr($uuid, 20, 12); |
Line 325... | Line 371... | ||
325 | - 6 bit Clock Sequence High |
371 | - 6 bit Clock Sequence High |
326 | - 8 bit Clock Sequence Low |
372 | - 8 bit Clock Sequence Low |
327 | - 48 bit MAC Address |
373 | - 48 bit MAC Address |
328 | */ |
374 | */ |
329 | 375 | ||
330 | if ($version == 1) echo sprintf("%-32s %s\n", "Version:", "[1] Time-based with unique host identifier"); |
376 | if ($version == 1) echo sprintf("%-32s %s\n", "Version:", "[0x1] Time-based with unique host identifier"); |
331 | 377 | ||
332 | # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00 |
378 | # Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00 |
333 | # 1/0,0000001 = 10000000 |
379 | # 1/0,0000001 = 10000000 |
334 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).substr($uuid, 0, 8); |
380 | $timestamp = substr($uuid, 13, 3).substr($uuid, 8, 4).substr($uuid, 0, 8); |
335 | $ts = gmp_init($timestamp, 16); |
381 | $ts = gmp_init($timestamp, 16); |
Line 342... | Line 388... | ||
342 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts"); |
388 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts"); |
343 | 389 | ||
344 | $x = hexdec(substr($uuid, 16, 4)); |
390 | $x = hexdec(substr($uuid, 16, 4)); |
345 | $dec = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x) |
391 | $dec = $x & 0x3FFF; // The highest 2 bits are used by "variant" (10x) |
346 | $hex = substr($uuid, 16, 4); |
392 | $hex = substr($uuid, 16, 4); |
- | 393 | $hex = '<abbr title="The highest 2 bits are used by the UUID variant (10xx)">'.$hex[0].'</abbr>'.substr($hex,1); |
|
347 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
394 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
348 | 395 | ||
349 | $x = substr($uuid, 20, 12); |
396 | $x = substr($uuid, 20, 12); |
350 | $nodeid = ''; |
397 | $nodeid = ''; |
351 | for ($i=0; $i<6; $i++) { |
398 | for ($i=0; $i<6; $i++) { |
Line 353... | Line 400... | ||
353 | if ($i != 5) $nodeid .= '-'; |
400 | if ($i != 5) $nodeid .= '-'; |
354 | } |
401 | } |
355 | $nodeid = strtoupper($nodeid); |
402 | $nodeid = strtoupper($nodeid); |
356 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
403 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
357 | 404 | ||
358 | if (function_exists('decode_mac')) { |
- | |
359 | echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n\n"; |
405 | echo "\n<u>In case that this Node ID is a MAC address, here is the interpretation of that MAC address:</u>\n\n"; |
360 | decode_mac(strtoupper($nodeid)); |
406 | decode_mac(strtoupper($nodeid)); |
361 | } |
- | |
362 | 407 | ||
363 | break; |
408 | break; |
364 | case 2: |
409 | case 2: |
365 | /* |
410 | /* |
366 | Variant 1, Version 2 UUID |
411 | Variant 1, Version 2 UUID |
Line 374... | Line 419... | ||
374 | - 48 bit MAC Address |
419 | - 48 bit MAC Address |
375 | */ |
420 | */ |
376 | 421 | ||
377 | // see also https://unicorn-utterances.com/posts/what-happened-to-uuid-v2 |
422 | // see also https://unicorn-utterances.com/posts/what-happened-to-uuid-v2 |
378 | 423 | ||
379 | echo sprintf("%-32s %s\n", "Version:", "[2] DCE Security version"); |
424 | echo sprintf("%-32s %s\n", "Version:", "[0x2] DCE Security version"); |
380 | 425 | ||
381 | # 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". |
426 | # 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". |
382 | $x = substr($uuid, 18, 2); |
427 | $x = substr($uuid, 18, 2); |
383 | if ($x == '00') $domain_info = 'Person (POSIX: User-ID)'; |
428 | if ($x == '00') $domain_info = 'Person (e.g. POSIX UID)'; |
384 | else if ($x == '01') $domain_info = 'Group (POSIX: Group-ID)'; |
429 | else if ($x == '01') $domain_info = 'Group (e.g. POSIX GID)'; |
385 | else if ($x == '02') $domain_info = 'Organization'; |
430 | else if ($x == '02') $domain_info = 'Organization'; |
386 | else $domain_info = 'site-defined (Domain '.hexdec($x).')'; |
431 | else $domain_info = 'site-defined (Domain '.hexdec($x).')'; |
387 | echo sprintf("%-32s %s\n", "Local Domain:", "[0x$x] $domain_info"); |
432 | echo sprintf("%-32s %s\n", "Local Domain:", "[0x$x] $domain_info"); |
388 | 433 | ||
389 | # 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. |
434 | # 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. |
Line 415... | Line 460... | ||
415 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts_min - $ts_max"); |
460 | echo sprintf("%-32s %s\n", "Timestamp:", "[0x$timestamp] $ts_min - $ts_max"); |
416 | 461 | ||
417 | $x = hexdec(substr($uuid, 16, 2)); |
462 | $x = hexdec(substr($uuid, 16, 2)); |
418 | $dec = $x & 0x3F; // The highest 2 bits are used by "variant" (10xx) |
463 | $dec = $x & 0x3F; // The highest 2 bits are used by "variant" (10xx) |
419 | $hex = substr($uuid, 16, 2); |
464 | $hex = substr($uuid, 16, 2); |
- | 465 | $hex = '<abbr title="The highest 2 bits are used by the UUID variant (10xx)">'.$hex[0].'</abbr>'.substr($hex,1); |
|
420 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
466 | echo sprintf("%-32s %s\n", "Clock ID:", "[0x$hex] $dec"); |
421 | 467 | ||
422 | $x = substr($uuid, 20, 12); |
468 | $x = substr($uuid, 20, 12); |
423 | $nodeid = ''; |
469 | $nodeid = ''; |
424 | for ($i=0; $i<6; $i++) { |
470 | for ($i=0; $i<6; $i++) { |
Line 426... | Line 472... | ||
426 | if ($i != 5) $nodeid .= '-'; |
472 | if ($i != 5) $nodeid .= '-'; |
427 | } |
473 | } |
428 | $nodeid = strtoupper($nodeid); |
474 | $nodeid = strtoupper($nodeid); |
429 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
475 | echo sprintf("%-32s %s\n", "Node ID:", "[0x$x] $nodeid"); |
430 | 476 | ||
431 | if (function_exists('decode_mac')) { |
- | |
432 | echo "\nIn case that this Node ID is a MAC address, here is the interpretation of that MAC address:\n\n"; |
477 | echo "\n<u>In case that this Node ID is a MAC address, here is the interpretation of that MAC address:</u>\n\n"; |
433 | decode_mac(strtoupper($nodeid)); |
478 | decode_mac(strtoupper($nodeid)); |
434 | } |
- | |
435 | 479 | ||
436 | break; |
480 | break; |
437 | case 3: |
481 | case 3: |
438 | /* |
482 | /* |
439 | Variant 1, Version 3 UUID |
483 | Variant 1, Version 3 UUID |
Line 442... | Line 486... | ||
442 | - 12 bit Hash Mid |
486 | - 12 bit Hash Mid |
443 | - 2 bit Variant (fix 0b10) |
487 | - 2 bit Variant (fix 0b10) |
444 | - 62 bit Hash Low |
488 | - 62 bit Hash Low |
445 | */ |
489 | */ |
446 | 490 | ||
447 | echo sprintf("%-32s %s\n", "Version:", "[3] Name-based (MD5 hash)"); |
491 | echo sprintf("%-32s %s\n", "Version:", "[0x3] Name-based (MD5 hash)"); |
448 | 492 | ||
449 | $hash = str_replace('-', '', strtolower($uuid)); |
493 | $hash = str_replace('-', '', strtolower($uuid)); |
450 | 494 | ||
451 | $hash[12] = '?'; // was overwritten by version |
495 | $hash[12] = '?'; // was overwritten by version |
452 | 496 | ||
Line 454... | Line 498... | ||
454 | $var16b = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b0100)); |
498 | $var16b = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b0100)); |
455 | $var16c = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1000)); |
499 | $var16c = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1000)); |
456 | $var16d = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1100)); |
500 | $var16d = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1100)); |
457 | $hash[16] = '?'; // was partially overwritten by variant |
501 | $hash[16] = '?'; // was partially overwritten by variant |
458 | 502 | ||
- | 503 | $p = 16; |
|
- | 504 | $hash = substr($hash,0,$p)."<abbr title=\"$var16a, $var16b, $var16c, or $var16d\">".substr($hash,$p,1).'</abbr>'.substr($hash,$p+1); |
|
459 | echo sprintf("%-32s %s\n", "MD5(Namespace+Subject):", "[0x$hash]"); |
505 | echo sprintf("%-32s %s\n", "MD5(Namespace+Subject):", "[0x$hash]"); |
460 | echo sprintf("%-32s %s\n", "", " ^"); |
- | |
461 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
- | |
462 | 506 | ||
463 | break; |
507 | break; |
464 | case 4: |
508 | case 4: |
465 | /* |
509 | /* |
466 | Variant 1, Version 4 UUID |
510 | Variant 1, Version 4 UUID |
Line 469... | Line 513... | ||
469 | - 12 bit Random Mid |
513 | - 12 bit Random Mid |
470 | - 2 bit Variant (fix 0b10) |
514 | - 2 bit Variant (fix 0b10) |
471 | - 62 bit Random Low |
515 | - 62 bit Random Low |
472 | */ |
516 | */ |
473 | 517 | ||
474 | echo sprintf("%-32s %s\n", "Version:", "[4] Random"); |
518 | echo sprintf("%-32s %s\n", "Version:", "[0x4] Random"); |
475 | 519 | ||
476 | $rand_line1 = ''; |
520 | $rand_line1 = ''; |
477 | $rand_line2 = ''; |
521 | $rand_line2 = ''; |
478 | for ($i=0; $i<16; $i++) { |
522 | for ($i=0; $i<16; $i++) { |
479 | $bin = base_convert(substr($uuid, $i*2, 2), 16, 2); |
523 | $bin = base_convert(substr($uuid, $i*2, 2), 16, 2); |
Line 502... | Line 546... | ||
502 | $var16a = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b0000)); |
546 | $var16a = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b0000)); |
503 | $var16b = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b0100)); |
547 | $var16b = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b0100)); |
504 | $var16c = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b1000)); |
548 | $var16c = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b1000)); |
505 | $var16d = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b1100)); |
549 | $var16d = strtoupper(dechex(hexdec($rand_bytes[16]) & 0b0011 | 0b1100)); |
506 | $rand_bytes[16] = '?'; // was partially overwritten by variant |
550 | $rand_bytes[16] = '?'; // was partially overwritten by variant |
- | 551 | ||
- | 552 | $p = 16; |
|
- | 553 | $rand_bytes = substr($rand_bytes,0,$p)."<abbr title=\"$var16a, $var16b, $var16c, or $var16d\">".substr($rand_bytes,$p,1).'</abbr>'.substr($rand_bytes,$p+1); |
|
507 | echo sprintf("%-32s %s\n", "Random bytes:", "[0x$rand_bytes]"); |
554 | echo sprintf("%-32s %s\n", "Random bytes:", "[0x$rand_bytes]"); |
508 | echo sprintf("%-32s %s\n", "", " ^"); |
- | |
509 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
- | |
510 | 555 | ||
511 | break; |
556 | break; |
512 | case 5: |
557 | case 5: |
513 | /* |
558 | /* |
514 | Variant 1, Version 5 UUID |
559 | Variant 1, Version 5 UUID |
Line 517... | Line 562... | ||
517 | - 12 bit Hash Mid |
562 | - 12 bit Hash Mid |
518 | - 2 bit Variant (fix 0b10) |
563 | - 2 bit Variant (fix 0b10) |
519 | - 62 bit Hash Low |
564 | - 62 bit Hash Low |
520 | */ |
565 | */ |
521 | 566 | ||
522 | echo sprintf("%-32s %s\n", "Version:", "[5] Name-based (SHA-1 hash)"); |
567 | echo sprintf("%-32s %s\n", "Version:", "[0x5] Name-based (SHA-1 hash)"); |
523 | 568 | ||
524 | $hash = str_replace('-', '', strtolower($uuid)); |
569 | $hash = str_replace('-', '', strtolower($uuid)); |
525 | 570 | ||
526 | $hash[12] = '?'; // was overwritten by version |
571 | $hash[12] = '?'; // was overwritten by version |
527 | 572 | ||
Line 531... | Line 576... | ||
531 | $var16d = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1100)); |
576 | $var16d = strtoupper(dechex(hexdec($hash[16]) & 0b0011 | 0b1100)); |
532 | $hash[16] = '?'; // was partially overwritten by variant |
577 | $hash[16] = '?'; // was partially overwritten by variant |
533 | 578 | ||
534 | $hash .= '????????'; // was cut off |
579 | $hash .= '????????'; // was cut off |
535 | 580 | ||
- | 581 | $p = 16; |
|
- | 582 | $hash = substr($hash,0,$p)."<abbr title=\"$var16a, $var16b, $var16c, or $var16d\">".substr($hash,$p,1).'</abbr>'.substr($hash,$p+1); |
|
536 | echo sprintf("%-32s %s\n", "SHA1(Namespace+Subject):", "[0x$hash]"); |
583 | echo sprintf("%-32s %s\n", "SHA1(Namespace+Subject):", "[0x$hash]"); |
537 | echo sprintf("%-32s %s\n", "", " ^"); |
- | |
538 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
- | |
539 | 584 | ||
540 | break; |
585 | break; |
541 | case 7: |
586 | case 7: |
542 | /* |
587 | /* |
543 | Variant 1, Version 7 UUID |
588 | Variant 1, Version 7 UUID |
544 | - 48 bit Unix Time in milliseconds |
589 | - 48 bit Unix Time in milliseconds |
545 | - 4 bit Version (fix 0x7) |
590 | - 4 bit Version (fix 0x7) |
546 | - 12 bit Random |
591 | - 12 bit Data |
547 | - 2 bit Variant (fix 0b10) |
592 | - 2 bit Variant (fix 0b10) |
548 | - 62 bit Random |
593 | - 62 bit Data |
- | 594 | ||
- | 595 | Structure of data (74 bits): |
|
- | 596 | - OPTIONAL : Sub-millisecond timestamp fraction (0-12 bits) |
|
- | 597 | - OPTIONAL : Carefully seeded counter |
|
- | 598 | - Random generated bits for any remaining space |
|
- | 599 | ||
- | 600 | Since we don't know if timestamp fraction or counters are implemented |
|
- | 601 | (and if so, how many bits), we don't decode this information |
|
549 | */ |
602 | */ |
550 | 603 | ||
551 | echo sprintf("%-32s %s\n", "Version:", "[7] Unix Epoch Time"); |
604 | echo sprintf("%-32s %s\n", "Version:", "[0x7] Unix Epoch Time"); |
552 | 605 | ||
553 | $timestamp = substr($uuid, 0, 12); |
606 | $timestamp = substr($uuid, 0, 12); |
554 | 607 | ||
555 | // Timestamp: Split into seconds and milliseconds |
608 | // Timestamp: Split into seconds and milliseconds |
556 | $ts = gmp_init($timestamp, 16); |
609 | $ts = gmp_init($timestamp, 16); |
Line 586... | Line 639... | ||
586 | $var16a = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b0000)); |
639 | $var16a = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b0000)); |
587 | $var16b = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b0100)); |
640 | $var16b = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b0100)); |
588 | $var16c = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b1000)); |
641 | $var16c = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b1000)); |
589 | $var16d = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b1100)); |
642 | $var16d = strtoupper(dechex(hexdec($rand_bytes[3]) & 0b0011 | 0b1100)); |
590 | $rand_bytes[3] = '?'; // was partially overwritten by variant |
643 | $rand_bytes[3] = '?'; // was partially overwritten by variant |
- | 644 | ||
- | 645 | $p = 3; |
|
- | 646 | $rand_bytes = substr($rand_bytes,0,$p)."<abbr title=\"$var16a, $var16b, $var16c, or $var16d\">".substr($rand_bytes,$p,1).'</abbr>'.substr($rand_bytes,$p+1); |
|
591 | echo sprintf("%-32s %s\n", "Random bytes:", "[0x$rand_bytes]"); |
647 | echo sprintf("%-32s %s\n", "Random bytes:", "[0x$rand_bytes]"); |
592 | echo sprintf("%-32s %s\n", "", " ^"); |
- | |
593 | echo sprintf("%-32s %s\n", "", " $var16a, $var16b, $var16c, or $var16d"); |
- | |
594 | 648 | ||
595 | // TODO: convert to and from Base32 CROCKFORD ULID (make 2 methods in uuid_utils.inc.php) |
649 | // TODO: convert to and from Base32 CROCKFORD ULID (make 2 methods in uuid_utils.inc.php) |
596 | // e.g. ULID: 01GCZ05N3JFRKBRWKNGCQZGP44 |
650 | // e.g. ULID: 01GCZ05N3JFRKBRWKNGCQZGP44 |
597 | // "Be aware that all version 7 UUIDs may be converted to ULIDs but not all ULIDs may be converted to UUIDs." |
651 | // "Be aware that all version 7 UUIDs may be converted to ULIDs but not all ULIDs may be converted to UUIDs." |
598 | 652 | ||
599 | break; |
653 | break; |
600 | case 8: |
654 | case 8: |
601 | /* |
655 | /* |
602 | Variant 1, Version 8 UUID |
656 | Variant 1, Version 8 UUID |
603 | - 48 bit Custom data |
657 | - 48 bit Custom data [Block 1+2] |
604 | - 4 bit Version (fix 0x8) |
658 | - 4 bit Version (fix 0x8) |
605 | - 12 bit Custom data |
659 | - 12 bit Custom data [Block 3] |
606 | - 2 bit Variant (fix 0b10) |
660 | - 2 bit Variant (fix 0b10) |
607 | - 62 bit Custom data |
661 | - 62 bit Custom data [Block 4+5] |
608 | */ |
662 | */ |
609 | 663 | ||
610 | echo sprintf("%-32s %s\n", "Version:", "[8] Custom implementation"); |
664 | echo sprintf("%-32s %s\n", "Version:", "[0x8] Custom implementation"); |
611 | 665 | ||
612 | $custom_data = substr($uuid,0,12).substr($uuid,13); // exclude version nibble |
666 | $custom_data = substr($uuid,0,12).substr($uuid,13); // exclude version nibble |
613 | $custom_data[15] = dechex(hexdec($custom_data[15]) & 0b0011); // nibble was partially overwritten by variant |
667 | $custom_data[15] = dechex(hexdec($custom_data[15]) & 0b0011); // nibble was partially overwritten by variant |
614 | $custom_data = strtolower($custom_data); |
668 | $custom_data = strtolower($custom_data); |
615 | 669 | ||
Line 621... | Line 675... | ||
621 | 675 | ||
622 | $custom_block3 = substr($custom_block3, 1); // remove version |
676 | $custom_block3 = substr($custom_block3, 1); // remove version |
623 | $custom_block4[0] = dechex(hexdec($custom_block4[0]) & 0b0011); // remove variant |
677 | $custom_block4[0] = dechex(hexdec($custom_block4[0]) & 0b0011); // remove variant |
624 | 678 | ||
625 | echo sprintf("%-32s %s\n", "Custom data:", "[0x$custom_data]"); |
679 | echo sprintf("%-32s %s\n", "Custom data:", "[0x$custom_data]"); |
626 | echo sprintf("%-32s %s\n", "Custom block1 (32 bit):", "[0x$custom_block1]"); |
680 | echo sprintf("%-32s %s\n", "Custom data block1 (32 bit):", "[0x$custom_block1]"); |
627 | echo sprintf("%-32s %s\n", "Custom block2 (16 bit):", "[0x$custom_block2]"); |
681 | echo sprintf("%-32s %s\n", "Custom data block2 (16 bit):", "[0x$custom_block2]"); |
628 | echo sprintf("%-32s %s\n", "Custom block3 (12 bit):", "[0x$custom_block3]"); |
682 | echo sprintf("%-32s %s\n", "Custom data block3 (12 bit):", "[0x$custom_block3]"); |
629 | echo sprintf("%-32s %s\n", "Custom block4 (14 bit):", "[0x$custom_block4]"); |
683 | echo sprintf("%-32s %s\n", "Custom data block4 (14 bit):", "[0x$custom_block4]"); |
630 | echo sprintf("%-32s %s\n", "Custom block5 (48 bit):", "[0x$custom_block5]"); |
684 | echo sprintf("%-32s %s\n", "Custom data block5 (48 bit):", "[0x$custom_block5]"); |
- | 685 | ||
- | 686 | // START: Check if Custom UUIDv8 is likely an OIDplus 2.0 Custom UUID |
|
- | 687 | ||
- | 688 | $oidplus_systemid_hex = $custom_block1; |
|
- | 689 | $oidplus_systemid_int = hexdec($oidplus_systemid_hex); // 31 bit hash of public key |
|
- | 690 | $oidplus_systemid_valid = hexdec($custom_block1) < 0x80000000; |
|
- | 691 | ||
- | 692 | $oidplus_creation_hex = $custom_block2; |
|
- | 693 | $oidplus_creation_int = hexdec($oidplus_creation_hex); // days since 1 January 1970, or 0 if unknown |
|
- | 694 | //$oidplus_creation_valid = ($oidplus_creation_int >= 14610/*1 Jan 2010*/) && ($oidplus_creation_int <= floor(time()/24/60/60)/*Today*/); |
|
- | 695 | $oidplus_creation_unknown = $oidplus_creation_int == 0; |
|
- | 696 | ||
- | 697 | $oidplus_reserved_hex = $custom_block3; |
|
- | 698 | $oidplus_reserved_int = hexdec($oidplus_reserved_hex); |
|
- | 699 | ||
- | 700 | $oidplus_namespace_hex = $custom_block4; |
|
- | 701 | $oidplus_namespace_int = hexdec($oidplus_namespace_hex); |
|
- | 702 | ||
- | 703 | $oidplus_data_hex = $custom_block5; |
|
- | 704 | $oidplus_data_int = (PHP_INT_SIZE == 4) ? gmp_strval(gmp_init($oidplus_data_hex,16),10) : hexdec($custom_block5); |
|
- | 705 | ||
- | 706 | if ($oidplus_systemid_valid && ($oidplus_reserved_int == 0)) { |
|
- | 707 | if (($oidplus_namespace_int == 0) && $oidplus_creation_unknown && (strtolower($oidplus_data_hex) == '1890afd80709')) { |
|
- | 708 | // System GUID, e.g. 6e932dd7-0000-8000-8000-1890afd80709 |
|
- | 709 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 710 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 711 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); /**@phpstan-ignore-line*/ |
|
- | 712 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 713 | echo sprintf("%-32s %s\n", "Namespace:", "[0x$oidplus_namespace_hex] $oidplus_namespace_int=System"); |
|
- | 714 | echo sprintf("%-32s %s\n", "Data (empty string hash):", "[0x$oidplus_data_hex] SHA1('') = ????????????????????????????$oidplus_data_hex"); |
|
- | 715 | } |
|
- | 716 | else if (($oidplus_namespace_int == 1) && $oidplus_creation_unknown) { |
|
- | 717 | // User GUID, e.g. 6e932dd7-0000-8000-8001-2938f50e857e (User), 6e932dd7-0000-8000-8001-000000000000 (Admin) |
|
- | 718 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 719 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 720 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); /**@phpstan-ignore-line*/ |
|
- | 721 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 722 | echo sprintf("%-32s %s\n", "Namespace:", "[0x$oidplus_namespace_hex] $oidplus_namespace_int=User"); |
|
- | 723 | if ($oidplus_data_int == 0) { |
|
- | 724 | echo sprintf("%-32s %s\n", "Data (Username):", "[0x$oidplus_data_hex] 0=Admin"); |
|
- | 725 | } else { |
|
- | 726 | echo sprintf("%-32s %s\n", "Data (Username):", "[0x$oidplus_data_hex] SHA1(UserName) = ????????????????????????????$oidplus_data_hex"); |
|
- | 727 | } |
|
- | 728 | } |
|
- | 729 | else if (($oidplus_namespace_int == 2)/* && $oidplus_creation_valid*/) { |
|
- | 730 | // Log entry GUID, e.g. 6e932dd7-458c-8000-8002-0000000004d2 |
|
- | 731 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 732 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 733 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); |
|
- | 734 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 735 | echo sprintf("%-32s %s\n", "Namespace:", "[0x$oidplus_namespace_hex] $oidplus_namespace_int=Log Entry"); |
|
- | 736 | echo sprintf("%-32s %s\n", "Data (Sequence number):", "[0x$oidplus_data_hex] $oidplus_data_int"); |
|
- | 737 | } |
|
- | 738 | else if (($oidplus_namespace_int == 3) && $oidplus_creation_unknown) { |
|
- | 739 | // Configuration entry GUID, e.g. 6e932dd7-0000-8000-8003-f14dda42862a |
|
- | 740 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 741 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 742 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); /**@phpstan-ignore-line*/ |
|
- | 743 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 744 | echo sprintf("%-32s %s\n", "Namespace:", "[0x$oidplus_namespace_hex] $oidplus_namespace_int=Configuration Entry"); |
|
- | 745 | echo sprintf("%-32s %s\n", "Data (Setting name hash):", "[0x$oidplus_data_hex] SHA1(SettingName) = ????????????????????????????$oidplus_data_hex"); |
|
- | 746 | } |
|
- | 747 | else if ($oidplus_namespace_int == 4) { |
|
- | 748 | // ASN.1 Alpahnumeric identifier GUID, e.g. 6e932dd7-0000-8000-8004-208ded8a3f8f |
|
- | 749 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 750 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 751 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); |
|
- | 752 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 753 | echo sprintf("%-32s %s\n", "Namespace:", "[0x$oidplus_namespace_hex] $oidplus_namespace_int=ASN.1 Alphanumeric ID"); |
|
- | 754 | $oidplus_data_24hi_hex = substr($oidplus_data_hex, 0, 6); |
|
- | 755 | $oidplus_data_24lo_hex = substr($oidplus_data_hex, 6, 6); |
|
- | 756 | echo sprintf("%-32s %s\n", "Data (OID hash):", "[0x$oidplus_data_24hi_hex] SHA1(OID) = ????????????????????????????$oidplus_data_24hi_hex"); |
|
- | 757 | echo sprintf("%-32s %s\n", "Data (Name hash):", "[0x$oidplus_data_24lo_hex] SHA1(AlphaNumId) = ????????????????????????????$oidplus_data_24lo_hex"); |
|
- | 758 | } |
|
- | 759 | else if ($oidplus_namespace_int == 5) { |
|
- | 760 | // Unicode Label entry GUID, e.g. 6e932dd7-0000-8000-8005-208dedaf9a96 |
|
- | 761 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 762 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 763 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); |
|
- | 764 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 765 | echo sprintf("%-32s %s\n", "Namespace:", "[0x$oidplus_namespace_hex] $oidplus_namespace_int=Unicode Label"); |
|
- | 766 | $oidplus_data_24hi_hex = substr($oidplus_data_hex, 0, 6); |
|
- | 767 | $oidplus_data_24lo_hex = substr($oidplus_data_hex, 6, 6); |
|
- | 768 | echo sprintf("%-32s %s\n", "Data (OID hash):", "[0x$oidplus_data_24hi_hex] SHA1(OID) = ????????????????????????????$oidplus_data_24hi_hex"); |
|
- | 769 | echo sprintf("%-32s %s\n", "Data (Name hash):", "[0x$oidplus_data_24lo_hex] SHA1(UnicodeLabel) = ????????????????????????????$oidplus_data_24lo_hex"); |
|
- | 770 | } |
|
- | 771 | else if (($oidplus_namespace_int >= 6) && ($oidplus_namespace_int <= 0xF)) { |
|
- | 772 | // System reserved |
|
- | 773 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 774 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 775 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); |
|
- | 776 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 777 | echo sprintf("%-32s %s\n", "Namespace:", "[0x$oidplus_namespace_hex] $oidplus_namespace_int=Unknown (System Reserved)"); |
|
- | 778 | echo sprintf("%-32s %s\n", "Data (Setting name hash):", "[0x$oidplus_data_hex] Unknown"); |
|
- | 779 | } |
|
- | 780 | else if ($oidplus_namespace_int > 0xF) { |
|
- | 781 | // Information Object GUID, e.g. 6e932dd7-458c-8000-b9e9-c1e3894d1105 |
|
- | 782 | $known_objecttype_plugins = array( |
|
- | 783 | // Latest list here: https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md |
|
- | 784 | '1.3.6.1.4.1.37476.2.5.2.4.8.1' => 'doi (ViaThinkSoft plugin)', |
|
- | 785 | '1.3.6.1.4.1.37476.2.5.2.4.8.2' => 'gs1 (ViaThinkSoft plugin)', |
|
- | 786 | '1.3.6.1.4.1.37476.2.5.2.4.8.3' => 'guid (ViaThinkSoft plugin)', |
|
- | 787 | '1.3.6.1.4.1.37476.2.5.2.4.8.4' => 'ipv4 (ViaThinkSoft plugin)', |
|
- | 788 | '1.3.6.1.4.1.37476.2.5.2.4.8.5' => 'ipv6 (ViaThinkSoft plugin)', |
|
- | 789 | '1.3.6.1.4.1.37476.2.5.2.4.8.6' => 'java (ViaThinkSoft plugin)', |
|
- | 790 | '1.3.6.1.4.1.37476.2.5.2.4.8.7' => 'oid (ViaThinkSoft plugin)', |
|
- | 791 | '1.3.6.1.4.1.37476.2.5.2.4.8.8' => 'other (ViaThinkSoft plugin)', |
|
- | 792 | '1.3.6.1.4.1.37476.2.5.2.4.8.9' => 'domain (ViaThinkSoft plugin)', |
|
- | 793 | '1.3.6.1.4.1.37476.2.5.2.4.8.10' => 'fourcc (ViaThinkSoft plugin)', |
|
- | 794 | '1.3.6.1.4.1.37476.2.5.2.4.8.11' => 'aid (ViaThinkSoft plugin)', |
|
- | 795 | '1.3.6.1.4.1.37476.2.5.2.4.8.12' => 'php (ViaThinkSoft plugin)', |
|
- | 796 | '1.3.6.1.4.1.37476.2.5.2.4.8.13' => 'mac (ViaThinkSoft plugin)', |
|
- | 797 | '1.3.6.1.4.1.37553.8.1.8.8.53354196964.27255728261' => 'circuit (Frdlweb plugin)', |
|
- | 798 | '1.3.6.1.4.1.37476.9000.108.19361.856' => 'ns (Frdlweb plugin)', |
|
- | 799 | '1.3.6.1.4.1.37553.8.1.8.8.53354196964.32927' => 'pen (Frdlweb plugin)', |
|
- | 800 | '1.3.6.1.4.1.37553.8.1.8.8.53354196964.39870' => 'uri (Frdlweb plugin)', |
|
- | 801 | '1.3.6.1.4.1.37553.8.1.8.8.53354196964.1958965295' => 'web+fan (Frdlweb plugin)' |
|
- | 802 | ); |
|
- | 803 | $namespace_desc = 'Unknown object type'; |
|
- | 804 | foreach ($known_objecttype_plugins as $oid => $name) { |
|
- | 805 | if ((hexdec(substr(sha1($oid), -4)) & 0x3fff) == $oidplus_namespace_int) $namespace_desc = "$oid = $name"; |
|
- | 806 | } |
|
- | 807 | echo "\n<u>Interpretation of <a href=\"https://github.com/danielmarschall/oidplus/blob/master/doc/oidplus_custom_guid.md\">OIDplus 2.0 Custom UUID</a></u>\n\n"; |
|
- | 808 | echo sprintf("%-32s %s\n", "System ID:", "[0x$oidplus_systemid_hex] ".$oidplus_systemid_int); |
|
- | 809 | echo sprintf("%-32s %s\n", "Creation time:", "[0x$oidplus_creation_hex] ".($oidplus_creation_unknown ? 'Unknown' : date('Y-m-d', $oidplus_creation_int*24*60*60))); |
|
- | 810 | echo sprintf("%-32s %s\n", "Reserved:", "[0x$oidplus_reserved_hex]"); |
|
- | 811 | echo sprintf("%-32s %s\n", "Namespace (Obj.type OID hash):", "[0x$oidplus_namespace_hex] $namespace_desc"); |
|
- | 812 | echo sprintf("%-32s %s\n", "Data (Object name hash):", "[0x$oidplus_data_hex] SHA1(ObjectName) = ????????????????????????????$oidplus_data_hex"); |
|
- | 813 | } |
|
- | 814 | } |
|
- | 815 | ||
- | 816 | // END: OIDplus 2.0 Custom UUID Interpretation |
|
631 | 817 | ||
632 | break; |
818 | break; |
633 | default: |
819 | default: |
634 | echo sprintf("%-32s %s\n", "Version:", "[$version] Unknown"); |
820 | echo sprintf("%-32s %s\n", "Version:", "[0x".dechex($version)."] Unknown"); |
635 | break; |
821 | break; |
636 | } |
822 | } |
637 | 823 | ||
638 | break; |
824 | break; |
639 | case 2: |
825 | case 2: |
Line 660... | Line 846... | ||
660 | if (!uuid_valid($uuid)) return false; |
846 | if (!uuid_valid($uuid)) return false; |
661 | return oid_to_uuid(uuid_to_oid($uuid)); |
847 | return oid_to_uuid(uuid_to_oid($uuid)); |
662 | } |
848 | } |
663 | 849 | ||
664 | function oid_to_uuid($oid) { |
850 | function oid_to_uuid($oid) { |
- | 851 | // TODO: Also support Non-2.25 base UUID-to-OID |
|
665 | if (!is_uuid_oid($oid)) return false; |
852 | if (!is_uuid_oid($oid,true)) return false; |
666 | 853 | ||
667 | if (substr($oid,0,1) == '.') { |
854 | if (substr($oid,0,1) == '.') { |
668 | $oid = substr($oid, 1); |
855 | $oid = substr($oid, 1); |
669 | } |
856 | } |
670 | $ary = explode('.', $oid); |
857 | $ary = explode('.', $oid); |
Line 682... | Line 869... | ||
682 | substr($y, 16, 4).'-'. |
869 | substr($y, 16, 4).'-'. |
683 | substr($y, 20, 12); |
870 | substr($y, 20, 12); |
684 | } |
871 | } |
685 | 872 | ||
686 | function is_uuid_oid($oid, $only_allow_root=false) { |
873 | function is_uuid_oid($oid, $only_allow_root=false) { |
- | 874 | // TODO: Also support Non-2.25 base UUID-to-OID |
|
687 | if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot |
875 | if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot |
688 | 876 | ||
689 | $ary = explode('.', $oid); |
877 | $ary = explode('.', $oid); |
690 | 878 | ||
691 | if ($only_allow_root) { |
879 | if ($only_allow_root) { |
Line 708... | Line 896... | ||
708 | } |
896 | } |
709 | 897 | ||
710 | return true; |
898 | return true; |
711 | } |
899 | } |
712 | 900 | ||
713 | function uuid_to_oid($uuid) { |
901 | function uuid_to_oid($uuid, $base='2.25') { |
714 | if (!uuid_valid($uuid)) return false; |
902 | if (!uuid_valid($uuid)) return false; |
- | 903 | #$base = oid_sanitize($base); |
|
715 | 904 | ||
716 | $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
905 | $uuid = str_replace(array('-', '{', '}'), '', $uuid); |
- | 906 | ||
- | 907 | // Information about Microsoft and Waterjuice UUID-OID: https://waterjuiceweb.wordpress.com/2019/09/24/guids-to-oids/ |
|
- | 908 | ||
- | 909 | if ($base == '2.25') { |
|
717 | $x = gmp_init($uuid, 16); |
910 | $x = gmp_init($uuid, 16); |
718 | return '2.25.'.gmp_strval($x, 10); |
911 | return $base.'.'.gmp_strval($x, 10); |
- | 912 | } else if ($base == '1.2.840.113556.1.8000.2554') { |
|
- | 913 | return $base.'.'. |
|
- | 914 | gmp_strval(gmp_init(substr($uuid,0,4),16),10).'.'. |
|
- | 915 | gmp_strval(gmp_init(substr($uuid,4,4),16),10).'.'. |
|
- | 916 | gmp_strval(gmp_init(substr($uuid,8,4),16),10).'.'. |
|
- | 917 | gmp_strval(gmp_init(substr($uuid,12,4),16),10).'.'. |
|
- | 918 | gmp_strval(gmp_init(substr($uuid,16,4),16),10).'.'. |
|
- | 919 | gmp_strval(gmp_init(substr($uuid,20,6),16),10).'.'. |
|
- | 920 | gmp_strval(gmp_init(substr($uuid,26,6),16),10); |
|
- | 921 | } else if ($base == '1.3.6.1.4.1.54392.1') { |
|
- | 922 | return $base.'.'. |
|
- | 923 | gmp_strval(gmp_init(substr($uuid,0,16),16),10).'.'. |
|
- | 924 | gmp_strval(gmp_init(substr($uuid,16,16),16),10); |
|
- | 925 | } else if ($base == '1.3.6.1.4.1.54392.2') { |
|
- | 926 | return $base.'.'. |
|
- | 927 | gmp_strval(gmp_init(substr($uuid,0,8),16),10).'.'. |
|
- | 928 | gmp_strval(gmp_init(substr($uuid,8,8),16),10).'.'. |
|
- | 929 | gmp_strval(gmp_init(substr($uuid,16,8),16),10).'.'. |
|
- | 930 | gmp_strval(gmp_init(substr($uuid,24,8),16),10); |
|
- | 931 | } else if ($base == '1.3.6.1.4.1.54392.3') { |
|
- | 932 | return $base.'.'. |
|
- | 933 | gmp_strval(gmp_init(substr($uuid,0,4),16),10).'.'. |
|
- | 934 | gmp_strval(gmp_init(substr($uuid,4,4),16),10).'.'. |
|
- | 935 | gmp_strval(gmp_init(substr($uuid,8,4),16),10).'.'. |
|
- | 936 | gmp_strval(gmp_init(substr($uuid,12,4),16),10).'.'. |
|
- | 937 | gmp_strval(gmp_init(substr($uuid,16,4),16),10).'.'. |
|
- | 938 | gmp_strval(gmp_init(substr($uuid,20,4),16),10).'.'. |
|
- | 939 | gmp_strval(gmp_init(substr($uuid,24,4),16),10).'.'. |
|
- | 940 | gmp_strval(gmp_init(substr($uuid,28,4),16),10); |
|
- | 941 | } else { |
|
- | 942 | throw new Exception("Unsupported UUID-to-OID base"); |
|
- | 943 | } |
|
719 | } |
944 | } |
720 | 945 | ||
721 | function uuid_numeric_value($uuid) { |
946 | function uuid_numeric_value($uuid) { |
722 | $oid = uuid_to_oid($uuid); |
947 | $oid = uuid_to_oid($uuid); |
723 | if (!$oid) return false; |
948 | if (!$oid) return false; |
Line 800... | Line 1025... | ||
800 | $tp['usec'] = gmp_init($tp['usec'],10); |
1025 | $tp['usec'] = gmp_init($tp['usec'],10); |
801 | $time = gmp_add(gmp_add(gmp_mul($tp['sec'], gmp_init('10000000',10)),gmp_mul($tp['usec'], gmp_init('10',10))),gmp_init('01B21DD213814000',16)); |
1026 | $time = gmp_add(gmp_add(gmp_mul($tp['sec'], gmp_init('10000000',10)),gmp_mul($tp['usec'], gmp_init('10',10))),gmp_init('01B21DD213814000',16)); |
802 | $uuid['time_low'] = gmp_and($time, gmp_init('ffffffff',16)); |
1027 | $uuid['time_low'] = gmp_and($time, gmp_init('ffffffff',16)); |
803 | $high = gmp_shiftr($time,32); |
1028 | $high = gmp_shiftr($time,32); |
804 | $uuid['time_mid'] = gmp_and($high, gmp_init('ffff',16)); |
1029 | $uuid['time_mid'] = gmp_and($high, gmp_init('ffff',16)); |
805 | $uuid['time_hi'] = intval(gmp_and(gmp_shiftr($high,16),gmp_init('fff',16)),10) | (1/*TimeBased*/ << 12); |
1030 | $uuid['time_hi'] = gmp_intval(gmp_and(gmp_shiftr($high,16),gmp_init('fff',16))) | (1/*TimeBased*/ << 12); |
806 | } else { |
1031 | } else { |
807 | $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) + 0x01B21DD213814000; |
1032 | $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) + 0x01B21DD213814000; |
808 | $uuid['time_low'] = $time & 0xffffffff; |
1033 | $uuid['time_low'] = $time & 0xffffffff; |
809 | /* Work around PHP 32-bit bit-operation limits */ |
1034 | /* Work around PHP 32-bit bit-operation limits */ |
810 | $high = intval($time / 0xffffffff); |
1035 | $high = intval($time / 0xffffffff); |
Line 828... | Line 1053... | ||
828 | for ($i = 0; $i < 6; $i++) { |
1053 | for ($i = 0; $i < 6; $i++) { |
829 | $uuid['node'][$i] = hexdec(substr($node, $i*2, 2)); |
1054 | $uuid['node'][$i] = hexdec(substr($node, $i*2, 2)); |
830 | } |
1055 | } |
831 | } else { |
1056 | } else { |
832 | // If we cannot get a MAC address, then generate a random AAI |
1057 | // If we cannot get a MAC address, then generate a random AAI |
- | 1058 | // RFC 4122 requires the multicast bit to be set, to make sure |
|
- | 1059 | // that a UUID from a system with network card never conflicts |
|
- | 1060 | // with a UUID from a system without network ard. |
|
- | 1061 | // We are additionally defining the other 3 bits as AAI, |
|
- | 1062 | // to avoid that we are misusing the CID or OUI from other vendors |
|
- | 1063 | // if we would create multicast ELI (based on CID) or EUI (based on OUI). |
|
833 | $uuid['node'] = explode('-', gen_aai(48, false)); |
1064 | $uuid['node'] = explode('-', gen_aai(48, true/*Multicast*/)); |
834 | $uuid['node'] = array_map('hexdec', $uuid['node']); |
1065 | $uuid['node'] = array_map('hexdec', $uuid['node']); |
835 | } |
1066 | } |
836 | 1067 | ||
837 | /* |
1068 | /* |
838 | * Now output the UUID |
1069 | * Now output the UUID |
Line 842... | Line 1073... | ||
842 | ($uuid['time_low']), ($uuid['time_mid']), ($uuid['time_hi']), |
1073 | ($uuid['time_low']), ($uuid['time_mid']), ($uuid['time_hi']), |
843 | $uuid['clock_seq_hi'], $uuid['clock_seq_low'], |
1074 | $uuid['clock_seq_hi'], $uuid['clock_seq_low'], |
844 | $uuid['node'][0], $uuid['node'][1], $uuid['node'][2], |
1075 | $uuid['node'][0], $uuid['node'][1], $uuid['node'][2], |
845 | $uuid['node'][3], $uuid['node'][4], $uuid['node'][5]); |
1076 | $uuid['node'][3], $uuid['node'][4], $uuid['node'][5]); |
846 | } |
1077 | } |
847 | function get_mac_address() { |
- | |
848 | static $detected_mac = false; |
- | |
849 | - | ||
850 | if ($detected_mac !== false) { // false NOT null! |
- | |
851 | return $detected_mac; |
- | |
852 | } |
- | |
853 | - | ||
854 | // TODO: This method get_mac_address() should actually be part of mac_utils.inc.php, but we need it |
- | |
855 | // here, and mac_utils.inc.php shall only be optional. What to do? |
- | |
856 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
- | |
857 | // Windows |
- | |
858 | $cmds = array( |
- | |
859 | "ipconfig /all", // faster |
- | |
860 | "getmac" |
- | |
861 | ); |
- | |
862 | foreach ($cmds as $cmd) { |
- | |
863 | $out = array(); |
- | |
864 | $ec = -1; |
- | |
865 | exec($cmd, $out, $ec); |
- | |
866 | if ($ec == 0) { |
- | |
867 | $out = implode("\n",$out); |
- | |
868 | $m = array(); |
- | |
869 | 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)) { |
- | |
870 | $detected_mac = strtolower($m[1]); |
- | |
871 | return $detected_mac; |
- | |
872 | } |
- | |
873 | } |
- | |
874 | } |
- | |
875 | } else if (strtoupper(PHP_OS) == 'DARWIN') { |
- | |
876 | // Mac OS X |
- | |
877 | $cmds = array( |
- | |
878 | "networksetup -listallhardwareports 2>/dev/null", |
- | |
879 | "netstat -i 2>/dev/null" |
- | |
880 | ); |
- | |
881 | foreach ($cmds as $cmd) { |
- | |
882 | $out = array(); |
- | |
883 | $ec = -1; |
- | |
884 | exec($cmd, $out, $ec); |
- | |
885 | if ($ec == 0) { |
- | |
886 | $out = implode("\n",$out); |
- | |
887 | $m = array(); |
- | |
888 | 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)) { |
- | |
889 | $detected_mac = $m[1]; |
- | |
890 | return $detected_mac; |
- | |
891 | } |
- | |
892 | } |
- | |
893 | } |
- | |
894 | } else { |
- | |
895 | // Linux |
- | |
896 | $addresses = @glob('/sys/class/net/'.'*'.'/address'); |
- | |
897 | foreach ($addresses as $x) { |
- | |
898 | if (!strstr($x,'/lo/')) { |
- | |
899 | $detected_mac = trim(file_get_contents($x)); |
- | |
900 | if (substr(mac_type($detected_mac),0,6) == 'EUI-48') { |
- | |
901 | return $detected_mac; |
- | |
902 | } |
- | |
903 | } |
- | |
904 | } |
- | |
905 | $cmds = array( |
- | |
906 | "netstat -ie 2>/dev/null", |
- | |
907 | "ifconfig 2>/dev/null" // only available for root (because it is in sbin) |
- | |
908 | ); |
- | |
909 | foreach ($cmds as $cmd) { |
- | |
910 | $out = array(); |
- | |
911 | $ec = -1; |
- | |
912 | exec($cmd, $out, $ec); |
- | |
913 | if ($ec == 0) { |
- | |
914 | $out = implode("\n",$out); |
- | |
915 | $m = array(); |
- | |
916 | 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)) { |
- | |
917 | $detected_mac = $m[1]; |
- | |
918 | return $detected_mac; |
- | |
919 | } |
- | |
920 | } |
- | |
921 | } |
- | |
922 | } |
- | |
923 | - | ||
924 | $detected_mac = null; |
- | |
925 | return $detected_mac; |
- | |
926 | } |
- | |
927 | 1078 | ||
928 | # -------------------------------------- |
1079 | # -------------------------------------- |
929 | // Variant 1, Version 2 (DCE Security) UUID |
1080 | // Variant 1, Version 2 (DCE Security) UUID |
930 | # -------------------------------------- |
1081 | # -------------------------------------- |
931 | 1082 | ||
Line 936... | Line 1087... | ||
936 | return gen_uuid_dce($domain, $id); |
1087 | return gen_uuid_dce($domain, $id); |
937 | } |
1088 | } |
938 | function gen_uuid_dce($domain, $id) { |
1089 | function gen_uuid_dce($domain, $id) { |
939 | if (($domain ?? '') === '') throw new Exception("Domain ID missing"); |
1090 | if (($domain ?? '') === '') throw new Exception("Domain ID missing"); |
940 | if (!is_numeric($domain)) throw new Exception("Invalid Domain ID"); |
1091 | if (!is_numeric($domain)) throw new Exception("Invalid Domain ID"); |
941 | if (($domain < 0) || ($domain > 255)) throw new Exception("Domain ID must be in range 0..255"); |
1092 | if (($domain < 0) || ($domain > 0xFF)) throw new Exception("Domain ID must be in range 0..255"); |
942 | 1093 | ||
943 | if (($id ?? '') === '') throw new Exception("ID value missing"); |
1094 | if (($id ?? '') === '') throw new Exception("ID value missing"); |
944 | if (!is_numeric($id)) throw new Exception("Invalid ID value"); |
1095 | if (!is_numeric($id)) throw new Exception("Invalid ID value"); |
945 | if (($id < 0) || ($id > 4294967295)) throw new Exception("ID value must be in range 0..4294967295"); |
1096 | if (($id < 0) || ($id > 0xFFFFFFFF)) throw new Exception("ID value must be in range 0..4294967295"); |
946 | 1097 | ||
947 | # Start with a version 1 UUID |
1098 | # Start with a version 1 UUID |
948 | $uuid = gen_uuid_timebased(); |
1099 | $uuid = gen_uuid_timebased(); |
949 | 1100 | ||
950 | # Add Domain Number |
1101 | # Add Domain Number |
Line 994... | Line 1145... | ||
994 | } |
1145 | } |
995 | function gen_uuid_random() { |
1146 | function gen_uuid_random() { |
996 | # On Windows: Requires |
1147 | # On Windows: Requires |
997 | # extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext" |
1148 | # extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext" |
998 | # extension=com_dotnet |
1149 | # extension=com_dotnet |
999 | // TODO: can we trust that com_create_guid() always outputs UUIDv4? |
- | |
1000 | /* |
- | |
1001 | if (function_exists('com_create_guid')) { |
1150 | if (function_exists('com_create_guid')) { |
1002 | return strtolower(trim(com_create_guid(), '{}')); |
1151 | $uuid = trim(com_create_guid(), '{}'); |
- | 1152 | if (uuid_version($uuid) === '4') { // <-- just to make 100% sure that Windows's CoCreateGuid() did output UUIDv4 |
|
- | 1153 | return strtolower($uuid); |
|
- | 1154 | } |
|
1003 | } |
1155 | } |
1004 | */ |
- | |
1005 | 1156 | ||
1006 | # On Debian: apt-get install php-uuid |
1157 | # On Debian: apt-get install php-uuid |
1007 | # extension_loaded('uuid') |
1158 | # extension_loaded('uuid') |
1008 | if (function_exists('uuid_create')) { |
1159 | if (function_exists('uuid_create')) { |
1009 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
1160 | # OSSP uuid extension like seen in php5-uuid at Debian 8 |
Line 1017... | Line 1168... | ||
1017 | # PECL uuid extension like seen in php-uuid at Debian 9 |
1168 | # PECL uuid extension like seen in php-uuid at Debian 9 |
1018 | return trim(uuid_create(UUID_TYPE_RANDOM)); |
1169 | return trim(uuid_create(UUID_TYPE_RANDOM)); |
1019 | } |
1170 | } |
1020 | 1171 | ||
1021 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
1172 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
- | 1173 | # On Debian Jessie: UUID V4 (Random) |
|
- | 1174 | if (file_exists($uuidv4_file = '/proc/sys/kernel/random/uuid')) { |
|
- | 1175 | $uuid = file_get_contents($uuidv4_file); |
|
- | 1176 | if (uuid_version($uuid) === '4') { // <-- just to make 100% sure that it did output UUIDv4 |
|
- | 1177 | return $uuid; |
|
- | 1178 | } |
|
- | 1179 | } |
|
- | 1180 | ||
1022 | # On Debian: apt-get install uuid-runtime |
1181 | # On Debian: apt-get install uuid-runtime |
1023 | $out = array(); |
1182 | $out = array(); |
1024 | $ec = -1; |
1183 | $ec = -1; |
1025 | exec('uuidgen -r 2>/dev/null', $out, $ec); |
1184 | exec('uuidgen -r 2>/dev/null', $out, $ec); |
1026 | if ($ec == 0) return trim($out[0]); |
1185 | if ($ec == 0) return trim($out[0]); |
1027 | - | ||
1028 | # On Debian Jessie: UUID V4 (Random) |
- | |
1029 | if (file_exists('/proc/sys/kernel/random/uuid')) { |
- | |
1030 | return trim(file_get_contents('/proc/sys/kernel/random/uuid')); |
- | |
1031 | } |
- | |
1032 | } |
1186 | } |
1033 | 1187 | ||
1034 | # Make the UUID by ourselves |
1188 | # Make the UUID by ourselves |
- | 1189 | ||
- | 1190 | if (function_exists('openssl_random_pseudo_bytes')) { |
|
- | 1191 | // Source: https://www.php.net/manual/en/function.com-create-guid.php#119168 |
|
- | 1192 | $data = openssl_random_pseudo_bytes(16); |
|
- | 1193 | $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 |
|
- | 1194 | $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 |
|
- | 1195 | return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); |
|
- | 1196 | } else { |
|
1035 | # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php |
1197 | // Source: http://rogerstringer.com/2013/11/15/generate-uuids-php |
1036 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
1198 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
1037 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), |
1199 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), |
1038 | _random_int( 0, 0xffff ), |
1200 | _random_int( 0, 0xffff ), |
1039 | _random_int( 0, 0x0fff ) | 0x4000, |
1201 | _random_int( 0, 0x0fff ) | 0x4000, |
1040 | _random_int( 0, 0x3fff ) | 0x8000, |
1202 | _random_int( 0, 0x3fff ) | 0x8000, |
1041 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), _random_int( 0, 0xffff ) |
1203 | _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), _random_int( 0, 0xffff ) |
1042 | ); |
1204 | ); |
1043 | } |
1205 | } |
- | 1206 | } |
|
1044 | 1207 | ||
1045 | # -------------------------------------- |
1208 | # -------------------------------------- |
1046 | // Variant 1, Version 5 (SHA1 name based) UUID |
1209 | // Variant 1, Version 5 (SHA1 name based) UUID |
1047 | # -------------------------------------- |
1210 | # -------------------------------------- |
1048 | 1211 | ||
Line 1114... | Line 1277... | ||
1114 | 1277 | ||
1115 | # -------------------------------------- |
1278 | # -------------------------------------- |
1116 | // Variant 1, Version 7 (Unix Epoch) UUID |
1279 | // Variant 1, Version 7 (Unix Epoch) UUID |
1117 | # -------------------------------------- |
1280 | # -------------------------------------- |
1118 | 1281 | ||
1119 | function gen_uuid_v7() { |
1282 | function gen_uuid_v7(int $num_ms_frac_bits=12) { |
1120 | return gen_uuid_unix_epoch(); |
1283 | return gen_uuid_unix_epoch($num_ms_frac_bits); |
1121 | } |
1284 | } |
1122 | function gen_uuid_unix_epoch() { |
1285 | function gen_uuid_unix_epoch(int $num_ms_frac_bits=12) { |
1123 | // Start with an UUIDv4 |
- | |
1124 | $uuid = gen_uuid_random(); |
1286 | $uuid_nibbles = ''; |
1125 | 1287 | ||
1126 | // Add the timestamp |
1288 | // Add the timestamp (milliseconds Unix) |
1127 | usleep(1000); // Wait 1ms, to make sure that the time part changes if multiple UUIDs are generated |
- | |
1128 | if (function_exists('gmp_init')) { |
1289 | if (function_exists('gmp_init')) { |
1129 | list($ms,$sec) = explode(' ', microtime(false)); |
1290 | list($ms,$sec) = explode(' ', microtime(false)); |
1130 | $sec = gmp_init($sec, 10); |
1291 | $sec = gmp_init($sec, 10); |
1131 | $ms = gmp_init(substr($ms,2,3), 10); |
1292 | $ms = gmp_init(substr($ms,2,3), 10); |
1132 | $unix_ts = gmp_strval(gmp_add(gmp_mul($sec, '1000'), $ms),16); |
1293 | $unix_ts = gmp_strval(gmp_add(gmp_mul($sec, '1000'), $ms),16); |
1133 | } else { |
1294 | } else { |
1134 | $unix_ts = dechex((int)round(microtime(true)*1000)); |
1295 | $unix_ts = dechex((int)ceil(microtime(true)*1000)); |
1135 | } |
1296 | } |
1136 | $unix_ts = str_pad($unix_ts, 12, '0', STR_PAD_LEFT); |
1297 | $unix_ts = str_pad($unix_ts, 12, '0', STR_PAD_LEFT); |
1137 | for ($i=0;$i<8;$i++) $uuid[$i] = substr($unix_ts, $i, 1); |
1298 | $uuid_nibbles = $unix_ts; |
1138 | for ($i=0;$i<4;$i++) $uuid[9+$i] = substr($unix_ts, 8+$i, 1); |
- | |
1139 | 1299 | ||
1140 | // set version |
1300 | // Version = 7 |
1141 | $uuid[14] = '7'; |
1301 | $uuid_nibbles .= '7'; |
1142 | 1302 | ||
- | 1303 | // Optional: millisecond fraction (max 12 bits) |
|
- | 1304 | if (($num_ms_frac_bits < 0) || ($num_ms_frac_bits > 12)) throw new Exception("Invalid msec frac bits (must be 0..12)"); |
|
- | 1305 | $resolution_ns = 1000000 / pow(2,$num_ms_frac_bits); |
|
- | 1306 | if ($num_ms_frac_bits > 0) { |
|
- | 1307 | $seconds_fraction = (float)explode(' ',microtime(false))[0]; // <sec=0>,<msec> |
|
- | 1308 | ||
- | 1309 | $ms_fraction = $seconds_fraction * 1000; // <msec>,<us> |
|
- | 1310 | $ms_fraction -= floor($ms_fraction); // <msec=0>,<us> |
|
- | 1311 | ||
- | 1312 | $ns_fraction = $ms_fraction * 1000000; // <ns> |
|
- | 1313 | $val = (int)ceil($ns_fraction / $resolution_ns); |
|
- | 1314 | ||
- | 1315 | // Currently, for the output we only allow frac bits 0, 4, 8, 12 (0-3 nibbles), |
|
- | 1316 | // since UUIDs are usually sorted in their hex notation, and one of the main |
|
- | 1317 | // reasons for using the sub-millisecond fractions it to increase monotonicity |
|
- | 1318 | $num_nibbles = (int)ceil($num_ms_frac_bits/4); |
|
- | 1319 | $uuid_nibbles .= str_pad(dechex($val), $num_nibbles, '0', STR_PAD_LEFT); |
|
- | 1320 | } |
|
- | 1321 | ||
- | 1322 | // TODO Not implemented: Optional counter (to be defined as parameter to this method) |
|
- | 1323 | // The counter bits need to be spread before and after the variant bits |
|
- | 1324 | ||
- | 1325 | // Fill with random bits (and the variant bits) |
|
- | 1326 | $uuid = gen_uuid_random(); |
|
- | 1327 | $uuid = str_replace('-', '', $uuid); |
|
- | 1328 | for ($i=0; $i<strlen($uuid_nibbles); $i++) $uuid[$i] = $uuid_nibbles[$i]; |
|
- | 1329 | ||
- | 1330 | // Wait to make sure that the time part changes if multiple UUIDs are generated |
|
- | 1331 | if (time_nanosleep(0,(int)ceil($resolution_ns)) !== true) usleep((int)ceil($resolution_ns/1000)); |
|
- | 1332 | ||
- | 1333 | // Output |
|
1143 | return $uuid; |
1334 | return substr($uuid, 0, 8).'-'. |
- | 1335 | substr($uuid, 8, 4).'-'. |
|
- | 1336 | substr($uuid, 12, 4).'-'. |
|
- | 1337 | substr($uuid, 16, 4).'-'. |
|
- | 1338 | substr($uuid, 20, 12); |
|
1144 | } |
1339 | } |
1145 | 1340 | ||
1146 | # -------------------------------------- |
1341 | # -------------------------------------- |
1147 | // Variant 1, Version 8 (Custom) UUID |
1342 | // Variant 1, Version 8 (Custom) UUID |
1148 | # -------------------------------------- |
1343 | # -------------------------------------- |
Line 1167... | Line 1362... | ||
1167 | $block4[0] = dechex(hexdec($block4[0]) & 0b0011 | 0b1000); // Variant 0b10__ = RFC4122 |
1362 | $block4[0] = dechex(hexdec($block4[0]) & 0b0011 | 0b1000); // Variant 0b10__ = RFC4122 |
1168 | 1363 | ||
1169 | return strtolower($block1.'-'.$block2.'-'.$block3.'-'.$block4.'-'.$block5); |
1364 | return strtolower($block1.'-'.$block2.'-'.$block3.'-'.$block4.'-'.$block5); |
1170 | } |
1365 | } |
1171 | 1366 | ||
- | 1367 | function gen_uuid_v8_namebased($hash_algo, $namespace_uuid, $name) { |
|
- | 1368 | if (($hash_algo ?? '') === '') throw new Exception("Hash algorithm argument missing"); |
|
- | 1369 | ||
- | 1370 | if (($namespace_uuid ?? '') === '') throw new Exception("Namespace UUID missing"); |
|
- | 1371 | if (!uuid_valid($namespace_uuid)) throw new Exception("Invalid namespace UUID '$namespace_uuid'"); |
|
- | 1372 | ||
- | 1373 | $uuid1 = uuid_valid($hash_algo) ? hex2bin(str_replace('-','',uuid_canonize($hash_algo))) : ''; // old "hash space" concept (dropped in Internet Draft 12) |
|
- | 1374 | $uuid2 = hex2bin(str_replace('-','',uuid_canonize($namespace_uuid))); |
|
- | 1375 | $payload = $uuid1 . $uuid2 . $name; |
|
- | 1376 | ||
- | 1377 | if (uuid_valid($hash_algo)) { |
|
- | 1378 | if (uuid_equal($hash_algo, '59031ca3-fbdb-47fb-9f6c-0f30e2e83145')) $hash_algo = 'sha224'; |
|
- | 1379 | if (uuid_equal($hash_algo, '3fb32780-953c-4464-9cfd-e85dbbe9843d')) $hash_algo = 'sha256'; |
|
- | 1380 | if (uuid_equal($hash_algo, 'e6800581-f333-484b-8778-601ff2b58da8')) $hash_algo = 'sha384'; |
|
- | 1381 | if (uuid_equal($hash_algo, '0fde22f2-e7ba-4fd1-9753-9c2ea88fa3f9')) $hash_algo = 'sha512'; |
|
- | 1382 | if (uuid_equal($hash_algo, '003c2038-c4fe-4b95-a672-0c26c1b79542')) $hash_algo = 'sha512/224'; |
|
- | 1383 | if (uuid_equal($hash_algo, '9475ad00-3769-4c07-9642-5e7383732306')) $hash_algo = 'sha512/256'; |
|
- | 1384 | if (uuid_equal($hash_algo, '9768761f-ac5a-419e-a180-7ca239e8025a')) $hash_algo = 'sha3-224'; |
|
- | 1385 | if (uuid_equal($hash_algo, '2034d66b-4047-4553-8f80-70e593176877')) $hash_algo = 'sha3-256'; |
|
- | 1386 | if (uuid_equal($hash_algo, '872fb339-2636-4bdd-bda6-b6dc2a82b1b3')) $hash_algo = 'sha3-384'; |
|
- | 1387 | if (uuid_equal($hash_algo, 'a4920a5d-a8a6-426c-8d14-a6cafbe64c7b')) $hash_algo = 'sha3-512'; |
|
- | 1388 | if (uuid_equal($hash_algo, '7ea218f6-629a-425f-9f88-7439d63296bb')) $hash_algo = 'shake128'; |
|
- | 1389 | if (uuid_equal($hash_algo, '2e7fc6a4-2919-4edc-b0ba-7d7062ce4f0a')) $hash_algo = 'shake256'; |
|
- | 1390 | } |
|
- | 1391 | ||
- | 1392 | if ($hash_algo == 'shake128') $hash = shake128($payload, 16/*min. required bytes*/, false); |
|
- | 1393 | else if ($hash_algo == 'shake256') $hash = shake256($payload, 16/*min. required bytes*/, false); |
|
- | 1394 | else $hash = hash($hash_algo, $payload, false); |
|
- | 1395 | ||
- | 1396 | if ($hash == null) { |
|
- | 1397 | throw new Exception("Unknown Hash Algorithm $hash_algo"); |
|
- | 1398 | } |
|
- | 1399 | ||
- | 1400 | $hash = str_pad($hash, 32, '0', STR_PAD_RIGHT); // fill short hashes with zeros to the right |
|
- | 1401 | ||
- | 1402 | $hash[12] = '8'; // Set version: 8 = Custom |
|
- | 1403 | $hash[16] = dechex(hexdec($hash[16]) & 0b0011 | 0b1000); // Set variant to "0b10__" (RFC4122/DCE1.1) |
|
- | 1404 | ||
- | 1405 | return substr($hash, 0, 8).'-'. |
|
- | 1406 | substr($hash, 8, 4).'-'. |
|
- | 1407 | substr($hash, 12, 4).'-'. |
|
- | 1408 | substr($hash, 16, 4).'-'. |
|
- | 1409 | substr($hash, 20, 12); |
|
- | 1410 | } |
|
- | 1411 | ||
1172 | # -------------------------------------- |
1412 | # -------------------------------------- |
1173 | 1413 | ||
1174 | // http://php.net/manual/de/function.hex2bin.php#113057 |
1414 | // http://php.net/manual/de/function.hex2bin.php#113057 |
1175 | if (!function_exists('hex2bin')) { |
1415 | if (!function_exists('hex2bin')) { |
1176 | function hex2bin($str) { |
1416 | function hex2bin($str) { |
Line 1193... | Line 1433... | ||
1193 | if (!function_exists('gmp_shiftr')) { |
1433 | if (!function_exists('gmp_shiftr')) { |
1194 | function gmp_shiftr($x,$n) { // shift right |
1434 | function gmp_shiftr($x,$n) { // shift right |
1195 | return(gmp_div_q($x,gmp_pow(2,$n))); |
1435 | return(gmp_div_q($x,gmp_pow(2,$n))); |
1196 | } |
1436 | } |
1197 | } |
1437 | } |
- | 1438 | ||
- | 1439 | function shake128(string $msg, int $outputLength=512, bool $binary=false): string { |
|
- | 1440 | include_once __DIR__.'/SHA3.php'; |
|
- | 1441 | $sponge = SHA3::init(SHA3::SHAKE128); |
|
- | 1442 | $sponge->absorb($msg); |
|
- | 1443 | $bin = $sponge->squeeze($outputLength); |
|
- | 1444 | return $binary ? $bin : bin2hex($bin); |
|
- | 1445 | } |
|
- | 1446 | ||
- | 1447 | function shake256(string $msg, int $outputLength=512, bool $binary=false): string { |
|
- | 1448 | include_once __DIR__.'/SHA3.php'; |
|
- | 1449 | $sponge = SHA3::init(SHA3::SHAKE256); |
|
- | 1450 | $sponge->absorb($msg); |
|
- | 1451 | $bin = $sponge->squeeze($outputLength); |
|
- | 1452 | return $binary ? $bin : bin2hex($bin); |
|
- | 1453 | } |