21,11 → 21,15 |
# This library requires either the GMP extension (or BCMath if gmp_supplement.inc.php is present) |
// TODO: If we are on 64 bit PHP (PHP_INT_SIZE > 4), then replace GMP with normal PHP operations |
|
if (file_exists(__DIR__ . '/mac_utils.inc.phps')) include_once __DIR__ . '/mac_utils.inc.phps'; |
if (file_exists(__DIR__ . '/mac_utils.inc.php')) include_once __DIR__ . '/mac_utils.inc.php'; |
if (file_exists($f = __DIR__ . '/mac_utils.inc.php')) include_once $f; |
else if (file_exists($f = __DIR__ . '/mac_utils.inc.phps')) include_once $f; |
|
if (file_exists(__DIR__ . '/gmp_supplement.inc.php')) include_once __DIR__ . '/gmp_supplement.inc.php'; |
if (file_exists($f = __DIR__ . '/gmp_supplement.inc.php')) include_once $f; |
else if (file_exists($f = __DIR__ . '/gmp_supplement.inc.phps')) include_once $f; |
|
if (file_exists($f = __DIR__ . '/OidDerConverter.class.php')) include_once $f; |
else if (file_exists($f = __DIR__ . '/OidDerConverter.class.phps')) include_once $f; |
|
const UUID_NAMEBASED_NS_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; // FQDN |
const UUID_NAMEBASED_NS_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
const UUID_NAMEBASED_NS_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
54,9 → 58,33 |
return ($uuid == ''); |
} |
|
function uuid_version($uuid) { |
$uuid = uuid_canonize($uuid); |
if (!$uuid) return false; |
return substr($uuid, 19, 1); |
} |
|
function uuid_info($uuid, $echo=true) { |
if (!uuid_valid($uuid)) return false; |
|
$oid = uuid_to_oid($uuid); |
|
echo sprintf("%-32s %s\n", "Your input:", $uuid); |
echo "\n"; |
echo "<u>Various notations:</u>\n"; |
echo "\n"; |
echo sprintf("%-32s %s\n", "URN:", 'urn:uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid)))); |
echo sprintf("%-32s %s\n", "URI:", 'uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid)))); |
echo sprintf("%-32s %s\n", "Microsoft GUID syntax:", '{' . strtoupper(oid_to_uuid(uuid_to_oid($uuid))) . '}'); |
echo sprintf("%-32s %s\n", "C++ struct syntax:", uuid_c_syntax($uuid)); |
echo "\n"; |
echo sprintf("%-32s %s\n", "As OID:", $oid); |
if (class_exists('OidDerConverter')) { |
echo sprintf("%-32s %s\n", "DER encoding of OID:", OidDerConverter::hexarrayToStr(OidDerConverter::oidToDER($oid))); |
} |
echo "\n"; |
echo "<u>Interpretation of the UUID:</u>\n\n"; |
|
if (!$echo) ob_start(); |
|
#$uuid = trim($uuid); |
305,7 → 333,7 |
- 8 bit Clock Sequence Low |
- 48 bit MAC Address |
*/ |
echo sprintf("%-32s %s\n", "Version:", "[6] Reordered Time"); |
echo sprintf("%-32s %s\n", "Version:", "[0x6] Reordered Time-Based"); |
$uuid = substr($uuid, 0, 8).'-'. |
substr($uuid, 8, 4).'-'. |
substr($uuid, 12, 4).'-'. |
328,7 → 356,7 |
- 48 bit MAC Address |
*/ |
|
if ($version == 1) echo sprintf("%-32s %s\n", "Version:", "[1] Time-based with unique host identifier"); |
if ($version == 1) echo sprintf("%-32s %s\n", "Version:", "[0x1] Time-based with unique host identifier"); |
|
# Timestamp: Count of 100ns intervals since 15 Oct 1582 00:00:00 |
# 1/0,0000001 = 10000000 |
376,7 → 404,7 |
|
// see also https://unicorn-utterances.com/posts/what-happened-to-uuid-v2 |
|
echo sprintf("%-32s %s\n", "Version:", "[2] DCE Security version"); |
echo sprintf("%-32s %s\n", "Version:", "[0x2] DCE Security version"); |
|
# 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". |
$x = substr($uuid, 18, 2); |
443,7 → 471,7 |
- 62 bit Hash Low |
*/ |
|
echo sprintf("%-32s %s\n", "Version:", "[3] Name-based (MD5 hash)"); |
echo sprintf("%-32s %s\n", "Version:", "[0x3] Name-based (MD5 hash)"); |
|
$hash = str_replace('-', '', strtolower($uuid)); |
|
470,7 → 498,7 |
- 62 bit Random Low |
*/ |
|
echo sprintf("%-32s %s\n", "Version:", "[4] Random"); |
echo sprintf("%-32s %s\n", "Version:", "[0x4] Random"); |
|
$rand_line1 = ''; |
$rand_line2 = ''; |
519,7 → 547,7 |
- 62 bit Hash Low |
*/ |
|
echo sprintf("%-32s %s\n", "Version:", "[5] Name-based (SHA-1 hash)"); |
echo sprintf("%-32s %s\n", "Version:", "[0x5] Name-based (SHA-1 hash)"); |
|
$hash = str_replace('-', '', strtolower($uuid)); |
|
548,7 → 576,7 |
- 62 bit Random |
*/ |
|
echo sprintf("%-32s %s\n", "Version:", "[7] Unix Epoch Time"); |
echo sprintf("%-32s %s\n", "Version:", "[0x7] Unix Epoch Time"); |
|
$timestamp = substr($uuid, 0, 12); |
|
608,7 → 636,7 |
- 62 bit Custom data |
*/ |
|
echo sprintf("%-32s %s\n", "Version:", "[8] Custom implementation"); |
echo sprintf("%-32s %s\n", "Version:", "[0x8] Custom implementation"); |
|
$custom_data = substr($uuid,0,12).substr($uuid,13); // exclude version nibble |
$custom_data[15] = dechex(hexdec($custom_data[15]) & 0b0011); // nibble was partially overwritten by variant |
632,7 → 660,7 |
|
break; |
default: |
echo sprintf("%-32s %s\n", "Version:", "[$version] Unknown"); |
echo sprintf("%-32s %s\n", "Version:", "[0x".dechex($version)."] Unknown"); |
break; |
} |
|
917,12 → 945,12 |
# On Windows: Requires |
# extension_dir = "C:\php-8.0.3-nts-Win32-vs16-x64\ext" |
# extension=com_dotnet |
// TODO: can we trust that com_create_guid() always outputs UUIDv4? |
/* |
if (function_exists('com_create_guid')) { |
return strtolower(trim(com_create_guid(), '{}')); |
$uuid = trim(com_create_guid(), '{}'); |
if (uuid_version($uuid) === '4') { // <-- just to make 100% sure that Windows's CoCreateGuid() did output UUIDv4 |
return strtolower($uuid); |
} |
*/ |
} |
|
# On Debian: apt-get install php-uuid |
# extension_loaded('uuid') |
953,7 → 981,15 |
} |
|
# Make the UUID by ourselves |
# Source: http://rogerstringer.com/2013/11/15/generate-uuids-php |
|
if (function_exists('openssl_random_pseudo_bytes')) { |
// Source: https://www.php.net/manual/en/function.com-create-guid.php#119168 |
$data = openssl_random_pseudo_bytes(16); |
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 |
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 |
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); |
} else { |
// Source: http://rogerstringer.com/2013/11/15/generate-uuids-php |
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
_random_int( 0, 0xffff ), _random_int( 0, 0xffff ), |
_random_int( 0, 0xffff ), |
962,6 → 998,7 |
_random_int( 0, 0xffff ), _random_int( 0, 0xffff ), _random_int( 0, 0xffff ) |
); |
} |
} |
|
# -------------------------------------- |
// Variant 1, Version 5 (SHA1 name based) UUID |