Rev 61 | Rev 63 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 61 | Rev 62 | ||
---|---|---|---|
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-08-02 |
6 | * Version 2023-08-03 |
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 1283... | Line 1283... | ||
1283 | $uuid1 = hex2bin(str_replace('-','',uuid_canonize($hash_uuid))); |
1283 | $uuid1 = hex2bin(str_replace('-','',uuid_canonize($hash_uuid))); |
1284 | $uuid2 = hex2bin(str_replace('-','',uuid_canonize($namespace_uuid))); |
1284 | $uuid2 = hex2bin(str_replace('-','',uuid_canonize($namespace_uuid))); |
1285 | $payload = $uuid1 . $uuid2 . $name; |
1285 | $payload = $uuid1 . $uuid2 . $name; |
1286 | 1286 | ||
1287 | $hash = null; |
1287 | $hash = null; |
1288 | foreach (get_uuidv8_hash_space_ids() as list($algo,$space,$friendlyname,$author)) { |
1288 | foreach (get_uuidv8_hash_space_ids() as list($algo,$space,$friendlyName,$author,$available)) { |
1289 | if (uuid_equal($hash_uuid,$space)) { |
1289 | if (uuid_equal($hash_uuid,$space)) { |
- | 1290 | if (!$available) { |
|
- | 1291 | throw new Exception("Algorithm $algo is not available on this system (PHP version too old)"); |
|
- | 1292 | } |
|
- | 1293 | ||
1290 | if ($algo == 'shake128') $hash = shake128($payload, 16/*min. required bytes*/, false); |
1294 | if ($algo == 'shake128') $hash = shake128($payload, 16/*min. required bytes*/, false); |
1291 | else if ($algo == 'shake256') $hash = shake256($payload, 16/*min. required bytes*/, false); |
1295 | else if ($algo == 'shake256') $hash = shake256($payload, 16/*min. required bytes*/, false); |
1292 | else $hash = hash($algo, $payload, false); |
1296 | else $hash = hash($algo, $payload, false); |
1293 | break; |
1297 | break; |
1294 | } |
1298 | } |
Line 1308... | Line 1312... | ||
1308 | substr($hash, 20, 12); |
1312 | substr($hash, 20, 12); |
1309 | } |
1313 | } |
1310 | 1314 | ||
1311 | /** |
1315 | /** |
1312 | * Collection of Namebased UUIDv8 Hash Space IDs |
1316 | * Collection of Namebased UUIDv8 Hash Space IDs |
1313 | * @return array An array containing tuples of [PHP Algo Name, Hash Space UUID, Human friendly name, Hash Space Author] |
1317 | * @return array An array containing tuples of [PHP Algo Name, Hash Space UUID, Human friendly name, Hash Space Author, Available] |
1314 | */ |
1318 | */ |
1315 | function get_uuidv8_hash_space_ids(): array { |
1319 | function get_uuidv8_hash_space_ids(): array { |
1316 | $out = array(); |
1320 | $out = array(); |
1317 | 1321 | ||
1318 | // The following Hash Space UUIDs are defined in the RFC4122bis as Example for Namebased UUIDv8 |
1322 | // The following Hash Space UUIDs are defined in the RFC4122bis as Example for Namebased UUIDv8 |
1319 | $out[] = ['sha224', '59031ca3-fbdb-47fb-9f6c-0f30e2e83145', 'SHA2-224', 'RFC Example']; |
1323 | $out[] = ['sha224', '59031ca3-fbdb-47fb-9f6c-0f30e2e83145', 'SHA2-224', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1320 | $out[] = ['sha256', '3fb32780-953c-4464-9cfd-e85dbbe9843d', 'SHA2-256', 'RFC Example']; |
1324 | $out[] = ['sha256', '3fb32780-953c-4464-9cfd-e85dbbe9843d', 'SHA2-256', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1321 | $out[] = ['sha384', 'e6800581-f333-484b-8778-601ff2b58da8', 'SHA2-384', 'RFC Example']; |
1325 | $out[] = ['sha384', 'e6800581-f333-484b-8778-601ff2b58da8', 'SHA2-384', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1322 | $out[] = ['sha512', '0fde22f2-e7ba-4fd1-9753-9c2ea88fa3f9', 'SHA2-512', 'RFC Example']; |
1326 | $out[] = ['sha512', '0fde22f2-e7ba-4fd1-9753-9c2ea88fa3f9', 'SHA2-512', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1323 | $out[] = ['sha512/224', '003c2038-c4fe-4b95-a672-0c26c1b79542', 'SHA2-512/224', 'RFC Example']; |
1327 | $out[] = ['sha512/224', '003c2038-c4fe-4b95-a672-0c26c1b79542', 'SHA2-512/224', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1324 | $out[] = ['sha512/256', '9475ad00-3769-4c07-9642-5e7383732306', 'SHA2-512/256', 'RFC Example']; |
1328 | $out[] = ['sha512/256', '9475ad00-3769-4c07-9642-5e7383732306', 'SHA2-512/256', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1325 | $out[] = ['sha3-224', '9768761f-ac5a-419e-a180-7ca239e8025a', 'SHA3-224', 'RFC Example']; |
1329 | $out[] = ['sha3-224', '9768761f-ac5a-419e-a180-7ca239e8025a', 'SHA3-224', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1326 | $out[] = ['sha3-256', '2034d66b-4047-4553-8f80-70e593176877', 'SHA3-256', 'RFC Example']; |
1330 | $out[] = ['sha3-256', '2034d66b-4047-4553-8f80-70e593176877', 'SHA3-256', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1327 | $out[] = ['sha3-384', '872fb339-2636-4bdd-bda6-b6dc2a82b1b3', 'SHA3-384', 'RFC Example']; |
1331 | $out[] = ['sha3-384', '872fb339-2636-4bdd-bda6-b6dc2a82b1b3', 'SHA3-384', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1328 | $out[] = ['sha3-512', 'a4920a5d-a8a6-426c-8d14-a6cafbe64c7b', 'SHA3-512', 'RFC Example']; |
1332 | $out[] = ['sha3-512', 'a4920a5d-a8a6-426c-8d14-a6cafbe64c7b', 'SHA3-512', 'RFC Example', PHP_VERSION_ID >= 70100]; |
1329 | $out[] = ['shake128'/*Currently no PHP core algorithm!*/, '7ea218f6-629a-425f-9f88-7439d63296bb', 'SHAKE-128', 'RFC Example']; |
1333 | $out[] = ['shake128'/*Currently no PHP core algorithm!*/, '7ea218f6-629a-425f-9f88-7439d63296bb', 'SHAKE-128', 'RFC Example', file_exists(__DIR__.'/SHA3.php')]; |
1330 | $out[] = ['shake256'/*Currently no PHP core algorithm!*/, '2e7fc6a4-2919-4edc-b0ba-7d7062ce4f0a', 'SHAKE-256', 'RFC Example']; |
1334 | $out[] = ['shake256'/*Currently no PHP core algorithm!*/, '2e7fc6a4-2919-4edc-b0ba-7d7062ce4f0a', 'SHAKE-256', 'RFC Example', file_exists(__DIR__.'/SHA3.php')]; |
1331 | 1335 | ||
1332 | // The following Hash Space UUIDs are defined by ViaThinkSoft |
1336 | // The following Hash Space UUIDs are defined by ViaThinkSoft |
1333 | // These Hash Spaces can be calculated as follows: |
1337 | // These Hash Spaces can be calculated as follows: |
1334 | // UUIDv8_NamebasedViaThinkSoft := <HashAlgo>_AsUUIDv8( BinaryHashSpaceUUID + BinaryNamespaceUUID + Data ) |
1338 | // UUIDv8_NamebasedViaThinkSoft := <HashAlgo>_AsUUIDv8( BinaryHashSpaceUUID + BinaryNamespaceUUID + Data ) |
1335 | // BinaryHashSpaceUUID := SHA1_AsUUIDv5( hex2bin('1ee317e2-1853-64b2-8fe9-3c4a92df8582') + PhpHashAlgoName ) |
1339 | // BinaryHashSpaceUUID := SHA1_AsUUIDv5( hex2bin('1ee317e2-1853-64b2-8fe9-3c4a92df8582') + PhpHashAlgoName ) |
1336 | foreach (hash_algos() as $algo) { |
1340 | foreach (hash_algos() as $algo) { |
1337 | if ($algo == 'md5') continue; // MD5 is already used in UUIDv3 |
1341 | if ($algo == 'md5') continue; // MD5 is already used in UUIDv3 |
1338 | if ($algo == 'sha1') continue; // MD5 is already used in UUIDv5 |
1342 | if ($algo == 'sha1') continue; // MD5 is already used in UUIDv5 |
1339 | foreach ($out as list($algo2,$space,$friendlyname,$author)) { |
1343 | foreach ($out as list($algo2,$space,$friendlyName,$author)) { |
1340 | if ($algo == $algo2) continue 2; // UUID is already defined by RFC, don't need a VTS Hash Space UUID |
1344 | if ($algo == $algo2) continue 2; // UUID is already defined by RFC, don't need a VTS Hash Space UUID |
1341 | } |
1345 | } |
1342 | if (strlen(hash($algo,'',false)) < 32) continue; // Hash too short (needs at least 16 bytes) |
1346 | if (strlen(hash($algo,'',false)) < 32) continue; // Hash too short (needs at least 16 bytes) |
1343 | $out[] = [$algo, gen_uuid_v5('1ee317e2-1853-64b2-8fe9-3c4a92df8582', $algo), strtoupper($algo), 'ViaThinkSoft']; |
1347 | $out[] = [$algo, gen_uuid_v5('1ee317e2-1853-64b2-8fe9-3c4a92df8582', $algo), strtoupper($algo), 'ViaThinkSoft', true]; |
- | 1348 | } |
|
- | 1349 | ||
- | 1350 | // How to update this list $unavailable_algos: |
|
- | 1351 | // 1. Look if new hashes are listed in the PHP documentation: https://www.php.net/manual/de/function.hash-algos.php |
|
- | 1352 | // 2. If the required version is lower than our server version, then you don't need to do anything |
|
- | 1353 | // 3. Otherwise, run this command on a different machine (where the algorithms are implemented) to check if the hashes have the correct length |
|
- | 1354 | // foreach (hash_algos() as $algo) { |
|
- | 1355 | // $len = strlen(hash($algo, '', false)); |
|
- | 1356 | // if ($len >= 32) echo "$algo, length $len\n"; |
|
- | 1357 | // } |
|
- | 1358 | // 4. Then, include all fitting hashes here |
|
- | 1359 | // 5. Please publish the new hash space IDs here: https://oidplus.viathinksoft.com/oidplus/?goto=guid%3Auuid_mac_utils%2Fuuidv8_hash_space_vts |
|
- | 1360 | $unavailable_algos = []; |
|
- | 1361 | if (PHP_VERSION_ID < 80100/*8.1.0*/) { |
|
- | 1362 | $unavailable_algos[] = 'murmur3c'; // length: 16 bytes |
|
- | 1363 | $unavailable_algos[] = 'murmur3f'; // length: 16 bytes |
|
- | 1364 | $unavailable_algos[] = 'xxh128'; // length: 16 bytes |
|
- | 1365 | } |
|
- | 1366 | foreach ($unavailable_algos as $algo) { |
|
- | 1367 | $out[] = [$algo, gen_uuid_v5('1ee317e2-1853-64b2-8fe9-3c4a92df8582', $algo), strtoupper($algo), 'ViaThinkSoft', false]; |
|
1344 | } |
1368 | } |
- | 1369 | ||
1345 | return $out; |
1370 | return $out; |
1346 | } |
1371 | } |
1347 | 1372 | ||
1348 | # -------------------------------------- |
1373 | # -------------------------------------- |
1349 | 1374 |