Subversion Repositories uuid_mac_utils

Rev

Rev 73 | Rev 77 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 73 Rev 75
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-09-24
6
 * Version 2023-10-06
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 1321... Line 1321...
1321
        if (($hash_algo ?? '') === '') throw new Exception("Hash algorithm argument missing");
1321
        if (($hash_algo ?? '') === '') throw new Exception("Hash algorithm argument missing");
1322
 
1322
 
1323
        if (($namespace_uuid ?? '') === '') throw new Exception("Namespace UUID missing");
1323
        if (($namespace_uuid ?? '') === '') throw new Exception("Namespace UUID missing");
1324
        if (!uuid_valid($namespace_uuid)) throw new Exception("Invalid namespace UUID '$namespace_uuid'");
1324
        if (!uuid_valid($namespace_uuid)) throw new Exception("Invalid namespace UUID '$namespace_uuid'");
1325
 
1325
 
1326
        $uuid1 = uuid_valid($hash_algo) ? hex2bin(str_replace('-','',uuid_canonize($hash_algo))) : '';
1326
        $uuid1 = uuid_valid($hash_algo) ? hex2bin(str_replace('-','',uuid_canonize($hash_algo))) : ''; // old "hash space" concept (dropped in Internet Draft 12)
1327
        $uuid2 = hex2bin(str_replace('-','',uuid_canonize($namespace_uuid)));
1327
        $uuid2 = hex2bin(str_replace('-','',uuid_canonize($namespace_uuid)));
1328
        $payload = $uuid1 . $uuid2 . $name;
1328
        $payload = $uuid1 . $uuid2 . $name;
1329
 
1329
 
1330
        if (uuid_valid($hash_algo)) {
1330
        if (uuid_valid($hash_algo)) {
-
 
1331
                if (uuid_equal($hash_algo, '59031ca3-fbdb-47fb-9f6c-0f30e2e83145')) $hash_algo = 'sha224';
1331
                foreach (get_uuidv8_hash_space_ids() as list($algo,$space,$friendlyName,$author,$available)) {
1332
                if (uuid_equal($hash_algo, '3fb32780-953c-4464-9cfd-e85dbbe9843d')) $hash_algo = 'sha256';
-
 
1333
                if (uuid_equal($hash_algo, 'e6800581-f333-484b-8778-601ff2b58da8')) $hash_algo = 'sha384';
1332
                        if (uuid_equal($hash_algo,$space)) {
1334
                if (uuid_equal($hash_algo, '0fde22f2-e7ba-4fd1-9753-9c2ea88fa3f9')) $hash_algo = 'sha512';
-
 
1335
                if (uuid_equal($hash_algo, '003c2038-c4fe-4b95-a672-0c26c1b79542')) $hash_algo = 'sha512/224';
-
 
1336
                if (uuid_equal($hash_algo, '9475ad00-3769-4c07-9642-5e7383732306')) $hash_algo = 'sha512/256';
-
 
1337
                if (uuid_equal($hash_algo, '9768761f-ac5a-419e-a180-7ca239e8025a')) $hash_algo = 'sha3-224';
1333
                                if (!$available) {
1338
                if (uuid_equal($hash_algo, '2034d66b-4047-4553-8f80-70e593176877')) $hash_algo = 'sha3-256';
1334
                                        throw new Exception("Algorithm $algo is not available on this system (PHP version too old)");
1339
                if (uuid_equal($hash_algo, '872fb339-2636-4bdd-bda6-b6dc2a82b1b3')) $hash_algo = 'sha3-384';
1335
                                }
-
 
1336
                                $hash_algo = $algo;
1340
                if (uuid_equal($hash_algo, 'a4920a5d-a8a6-426c-8d14-a6cafbe64c7b')) $hash_algo = 'sha3-512';
1337
                                break;
-
 
1338
                        }
-
 
1339
                }
-
 
-
 
1341
                if (uuid_equal($hash_algo, '7ea218f6-629a-425f-9f88-7439d63296bb')) $hash_algo = 'shake128';
-
 
1342
                if (uuid_equal($hash_algo, '2e7fc6a4-2919-4edc-b0ba-7d7062ce4f0a')) $hash_algo = 'shake256';
1340
        }
1343
        }
1341
 
1344
 
1342
        if ($hash_algo == 'shake128') $hash = shake128($payload, 16/*min. required bytes*/, false);
1345
        if ($hash_algo == 'shake128') $hash = shake128($payload, 16/*min. required bytes*/, false);
1343
        else if ($hash_algo == 'shake256') $hash = shake256($payload, 16/*min. required bytes*/, false);
1346
        else if ($hash_algo == 'shake256') $hash = shake256($payload, 16/*min. required bytes*/, false);
1344
        else $hash = hash($hash_algo, $payload, false);
1347
        else $hash = hash($hash_algo, $payload, false);
Line 1357... Line 1360...
1357
               substr($hash, 12, 4).'-'.
1360
               substr($hash, 12, 4).'-'.
1358
               substr($hash, 16, 4).'-'.
1361
               substr($hash, 16, 4).'-'.
1359
               substr($hash, 20, 12);
1362
               substr($hash, 20, 12);
1360
}
1363
}
1361
 
1364
 
1362
/**
-
 
1363
 * Collection of Namebased UUIDv8 Hash Space IDs
-
 
1364
 * @return array An array containing tuples of [PHP Algo Name, Hash Space UUID, Human friendly name, Hash Space Author, Available]
-
 
1365
 */
-
 
1366
function get_uuidv8_hash_space_ids(): array {
-
 
1367
        $out = array();
-
 
1368
 
-
 
1369
        // The following Hash Space UUIDs are defined in draft-ietf-uuidrev-rfc4122bis-11 as Example for Namebased UUIDv8
-
 
1370
        $category = 'Internet Draft 11, Appendix B';
-
 
1371
        $out[] = ['sha224', '59031ca3-fbdb-47fb-9f6c-0f30e2e83145', 'SHA-224', $category, PHP_VERSION_ID >= 70100];
-
 
1372
        $out[] = ['sha256', '3fb32780-953c-4464-9cfd-e85dbbe9843d', 'SHA-256', $category, PHP_VERSION_ID >= 70100];
-
 
1373
        $out[] = ['sha384', 'e6800581-f333-484b-8778-601ff2b58da8', 'SHA-384', $category, PHP_VERSION_ID >= 70100];
-
 
1374
        $out[] = ['sha512', '0fde22f2-e7ba-4fd1-9753-9c2ea88fa3f9', 'SHA-512', $category, PHP_VERSION_ID >= 70100];
-
 
1375
        $out[] = ['sha512/224', '003c2038-c4fe-4b95-a672-0c26c1b79542', 'SHA-512/224', $category, PHP_VERSION_ID >= 70100];
-
 
1376
        $out[] = ['sha512/256', '9475ad00-3769-4c07-9642-5e7383732306', 'SHA-512/256', $category, PHP_VERSION_ID >= 70100];
-
 
1377
        $out[] = ['sha3-224', '9768761f-ac5a-419e-a180-7ca239e8025a', 'SHA3-224', $category, PHP_VERSION_ID >= 70100];
-
 
1378
        $out[] = ['sha3-256', '2034d66b-4047-4553-8f80-70e593176877', 'SHA3-256', $category, PHP_VERSION_ID >= 70100];
-
 
1379
        $out[] = ['sha3-384', '872fb339-2636-4bdd-bda6-b6dc2a82b1b3', 'SHA3-384', $category, PHP_VERSION_ID >= 70100];
-
 
1380
        $out[] = ['sha3-512', 'a4920a5d-a8a6-426c-8d14-a6cafbe64c7b', 'SHA3-512', $category, PHP_VERSION_ID >= 70100];
-
 
1381
        $out[] = ['shake128'/*Currently no PHP core algorithm!*/, '7ea218f6-629a-425f-9f88-7439d63296bb', 'SHAKE128', $category, file_exists(__DIR__.'/SHA3.php')];
-
 
1382
        $out[] = ['shake256'/*Currently no PHP core algorithm!*/, '2e7fc6a4-2919-4edc-b0ba-7d7062ce4f0a', 'SHAKE256', $category, file_exists(__DIR__.'/SHA3.php')];
-
 
1383
 
-
 
1384
        return $out;
-
 
1385
}
-
 
1386
 
-
 
1387
# --------------------------------------
1365
# --------------------------------------
1388
 
1366
 
1389
// http://php.net/manual/de/function.hex2bin.php#113057
1367
// http://php.net/manual/de/function.hex2bin.php#113057
1390
if (!function_exists('hex2bin')) {
1368
if (!function_exists('hex2bin')) {
1391
    function hex2bin($str) {
1369
    function hex2bin($str) {