Subversion Repositories uuid_mac_utils

Rev

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

Rev 68 Rev 69
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
* UUID & MAC Utils
4
* UUID & MAC Utils
5
* Copyright 2017 - 2023 Daniel Marschall, ViaThinkSoft
5
* Copyright 2017 - 2023 Daniel Marschall, ViaThinkSoft
6
* Version 2023-09-07
6
* Version 2023-09-23
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 316... Line 316...
316
 
316
 
317
<h3 id="gen_uuidv35">Generate name-based (version 3 / 5 / <font color="green">New: 8</font>) UUID</h3>
317
<h3 id="gen_uuidv35">Generate name-based (version 3 / 5 / <font color="green">New: 8</font>) UUID</h3>
318
 
318
 
319
<p><i>An UUIDv3 is made out of a MD5 hash and an UUIDv5 is made out of a SHA1 hash.
319
<p><i>An UUIDv3 is made out of a MD5 hash and an UUIDv5 is made out of a SHA1 hash.
320
The revision of RFC4122 also contains an example for a custom UUIDv8 that
320
The revision of RFC4122 also contains an example for a custom UUIDv8 that
321
allows SHA2, SHA3 and SHAKE hash algorithms. ViaThinkSoft added more hash
321
allows SHA2, SHA3 and SHAKE hash algorithms.</i></p>
322
algorithms and assigned Hash Space IDs to them.</i></p>
-
 
323
 
322
 
324
<script>
323
<script>
325
function show_uuidv35_info() {
324
function show_uuidv35_info() {
326
        document.getElementById("uuidv35_info_button").style.display = "none";
325
        document.getElementById("uuidv35_info_button").style.display = "none";
327
        document.getElementById("uuidv35_info").style.display = "block";
326
        document.getElementById("uuidv35_info").style.display = "block";
Line 334... Line 333...
334
- 12 bit Hash Mid
333
- 12 bit Hash Mid
335
-  2 bit Variant (fix 0b10)
334
-  2 bit Variant (fix 0b10)
336
- 62 bit Hash Low
335
- 62 bit Hash Low
337
 
336
 
338
 
337
 
339
 
-
 
340
<u>Overview of namebased UUIDs:</u>
338
<u>As defined by <a href="https://datatracker.ietf.org/doc/rfc4122/">RFC4122</a>:</u>
341
UUIDv3(<i>NamespaceUuid</i>, <i>Data</i>)           := <abbr title="Adds UUID variant 0b10 and version 3">ConvertRawBytesToUuid_v3</abbr>(MD5( Binary[<i>NameSpaceUuid</i>] || <i>Data</i> )).
339
UUIDv3(<i>NamespaceUuid</i>, <i>Data</i>) := <abbr title="Adds UUID variant 0b10 and version 3">ConvertRawBytesToUuid_v3</abbr>(MD5( Binary[<i>NameSpaceUuid</i>] || <i>Data</i> )).
342
UUIDv5(<i>NamespaceUuid</i>, <i>Data</i>)           := <abbr title="Adds UUID variant 0b10 and version 5">ConvertRawBytesToUuid_v5</abbr>(SHA1( Binary[<i>NameSpaceUuid</i>] || <i>Data</i> )).
340
UUIDv5(<i>NamespaceUuid</i>, <i>Data</i>) := <abbr title="Adds UUID variant 0b10 and version 5">ConvertRawBytesToUuid_v5</abbr>(SHA1( Binary[<i>NameSpaceUuid</i>] || <i>Data</i> )).
343
UUIDv8(<i>HashAlgo</i>, <i>NameSpaceUuid</i>, <i>Data</i>) := <abbr title="Adds UUID variant 0b10 and version 8">ConvertRawBytesToUuid_v8</abbr>(<i>HashAlgo</i>( Binary[HashSpaceUuid&lt;<i>HashAlgo</i>&gt;] || Binary[<i>NameSpaceUuid</i>] || <i>Data</i> )).
-
 
344
 
-
 
345
<u>As defined by <a href="https://datatracker.ietf.org/doc/rfc4122/">RFC4122</a> Appendix C / <a href="https://datatracker.ietf.org/doc/draft-ietf-uuidrev-rfc4122bis/">draft-ietf-uuidrev-rfc4122bis-11</a> Appendix A:</u><!-- TODO: When new RFC is published, replace the RFC number -->
-
 
346
NameSpaceUuid&lt;DNS&gt;          := "6ba7b810-9dad-11d1-80b4-00c04fd430c8".
341
NameSpaceUuid&lt;DNS&gt;          := "6ba7b810-9dad-11d1-80b4-00c04fd430c8".
347
NameSpaceUuid&lt;URL&gt;          := "6ba7b811-9dad-11d1-80b4-00c04fd430c8".
342
NameSpaceUuid&lt;URL&gt;          := "6ba7b811-9dad-11d1-80b4-00c04fd430c8".
348
NameSpaceUuid&lt;OID&gt;          := "6ba7b812-9dad-11d1-80b4-00c04fd430c8".
343
NameSpaceUuid&lt;OID&gt;          := "6ba7b812-9dad-11d1-80b4-00c04fd430c8".
349
NameSpaceUuid&lt;X500&gt;         := "6ba7b814-9dad-11d1-80b4-00c04fd430c8".
344
NameSpaceUuid&lt;X500&gt;         := "6ba7b814-9dad-11d1-80b4-00c04fd430c8".
350
 
345
 
351
<u>As defined by <a href="https://datatracker.ietf.org/doc/draft-ietf-uuidrev-rfc4122bis/">draft-ietf-uuidrev-rfc4122bis-11</a> Appendix B:</u>
346
<u>As defined by <a href="https://datatracker.ietf.org/doc/draft-ietf-uuidrev-rfc4122bis/">draft-ietf-uuidrev-rfc4122bis-11</a>:</u>
-
 
347
UUIDv8(<i>HashAlgo</i>, <i>NameSpaceUuid</i>, <i>Data</i>) := <abbr title="Adds UUID variant 0b10 and version 8">ConvertRawBytesToUuid_v8</abbr>(<i>HashAlgo</i>( Binary[HashSpaceUuid&lt;<i>HashAlgo</i>&gt;] || Binary[<i>NameSpaceUuid</i>] || <i>Data</i> )).
352
<?php
348
<?php
353
 
349
 
354
$tmp = [];
350
$tmp = [];
355
foreach (get_uuidv8_hash_space_ids() as list($algo,$space,$friendlyName,$author,$available,$oid)) {
351
foreach (get_uuidv8_hash_space_ids() as list($algo,$space,$friendlyName,$author,$available)) {
356
        if (strpos($author,'ViaThinkSoft') === false) {
-
 
357
                $line = str_pad('HashSpaceUuid&lt;'.htmlentities($friendlyName).'&gt;', 34, ' ', STR_PAD_RIGHT);
352
        $line = str_pad('HashSpaceUuid&lt;'.htmlentities($friendlyName).'&gt;', 34, ' ', STR_PAD_RIGHT);
358
                $line .= ':= "'.$space.'".';
353
        $line .= ':= "'.$space.'".';
359
                if (!$available) $line .= " (Currently not available on this system)";
354
        if (!$available) $line .= " (Currently not available on this system)";
360
                $line .= "\n";
355
        $line .= "\n";
361
                if (isset($tmp[$friendlyName])) continue; // Ignore "Internet Draft 12 Proposal"
-
 
362
                $tmp[$friendlyName] = $line;
-
 
363
        }
-
 
364
}
-
 
365
ksort($tmp);
-
 
366
foreach ($tmp as $line) {
-
 
367
        echo $line;
-
 
368
}
-
 
369
 
-
 
370
?>
-
 
371
 
-
 
372
<u>As defined in <a href="https://github.com/ietf-wg-uuidrev/rfc4122bis/issues/143#issuecomment-1709117798">Internet Draft 12 Proposal</a>:</u>
-
 
373
HashSpaceUuid&lt;<i>HashAlgo</i>&gt;     := UUIDv5(NS_OID, OID[<i>HashAlgo</i>]).
-
 
374
which results in the following UUIDs:
-
 
375
<?php
-
 
376
 
-
 
377
$tmp = [];
-
 
378
foreach (get_uuidv8_hash_space_ids() as list($algo,$space,$friendlyName,$author,$available,$oid)) {
-
 
379
        if (strpos($author,'ViaThinkSoft') === false) {
-
 
380
                $line = str_pad('HashSpaceUuid&lt;'.htmlentities($friendlyName).'&gt;', 34, ' ', STR_PAD_RIGHT);
-
 
381
                if ($oid != null) $line .= str_pad(':= UUIDv5(NS_OID, "'.$oid.'")', 46, ' ', STR_PAD_RIGHT);
-
 
382
                if ($oid == null) $line .= str_pad(':= UUIDv5(NS_PHPNAME, "'.$algo.'")', 46, ' ', STR_PAD_RIGHT);
-
 
383
                $line .= '= "'.$space.'".';
-
 
384
                if (!$available) $line .= " (Currently not available on this system)";
-
 
385
                $line .= "\n";
-
 
386
                $tmp[$friendlyName] = $line;
356
        $tmp[$friendlyName] = $line;
387
        }
357
}
388
}
-
 
389
ksort($tmp);
-
 
390
foreach ($tmp as $line) {
-
 
391
        echo $line;
-
 
392
}
-
 
393
 
-
 
394
?>
-
 
395
 
-
 
396
<u>As defined by ViaThinkSoft for all other algorithms:</u>
-
 
397
HashSpaceUuid&lt;<i>HashAlgo</i>&gt;     := UUIDv5(NS_OID, OID[<i>HashAlgo</i>]).
-
 
398
or in case no OID can be found:
-
 
399
HashSpaceUuid&lt;<i>HashAlgo</i>&gt;     := UUIDv5(NS_PHPNAME="1ee317e2-1853-64b2-8fe9-3c4a92df8582", <a href="https://www.php.net/manual/de/function.hash-algos.php">PhpName</a>[<i>HashAlgo</i>]).
-
 
400
which results in the following UUIDs:
-
 
401
<?php
-
 
402
 
-
 
403
$tmp = [];
-
 
404
foreach (get_uuidv8_hash_space_ids() as list($algo,$space,$friendlyName,$author,$available,$oid)) {
-
 
405
        if (strpos($author,'ViaThinkSoft') !== false) {
-
 
406
                $line = str_pad('HashSpaceUuid&lt;'.htmlentities($friendlyName).'&gt;', 34, ' ', STR_PAD_RIGHT);
-
 
407
                if ($oid != null) $line .= str_pad(':= UUIDv5(NS_OID,     "'.$oid.'")', 53, ' ', STR_PAD_RIGHT);
-
 
408
                if ($oid == null) $line .= str_pad(':= UUIDv5(NS_PHPNAME, "'.$algo.'")', 53, ' ', STR_PAD_RIGHT);
-
 
409
                $line .= '= "'.$space.'".';
-
 
410
                if (!$available) $line .= " (Currently not available on this system)";
-
 
411
                $line .= "\n";
-
 
412
                $tmp[$friendlyName] = $line;
-
 
413
        }
-
 
414
}
-
 
415
ksort($tmp);
358
ksort($tmp);
416
foreach ($tmp as $line) {
359
foreach ($tmp as $line) {
417
        echo $line;
360
        echo $line;
418
}
361
}
419
 
362
 
420
?>
363
?>
421
 
364
 
-
 
365
<u>Custom implementation ("Raw Hash"):</u>
-
 
366
UUIDv8(<i>HashAlgo</i>, <i>NameSpaceUuid</i>, <i>Data</i>) := <abbr title="Adds UUID variant 0b10 and version 8">ConvertRawBytesToUuid_v8</abbr>(<i>HashAlgo</i>( Binary[<i>NameSpaceUuid</i>] || <i>Data</i> )).
422
 
367
 
423
</pre></p>
368
</pre></p>
424
 
369
 
425
<style>
370
<style>
426
label {
371
label {
Line 458... Line 403...
458
                        ksort($tmp);
403
                        ksort($tmp);
459
                        foreach ($tmp as $html) {
404
                        foreach ($tmp as $html) {
460
                                echo "\t\t$html\n";
405
                                echo "\t\t$html\n";
461
                        }
406
                        }
462
                }
407
                }
-
 
408
 
-
 
409
                echo "\t\t<option disabled>--- UUIDv8 (Raw Hash) ---</option>\n";
-
 
410
                $tmp = [];
-
 
411
                $algos = hash_algos();
-
 
412
                $algos[] = 'shake128';
-
 
413
                $algos[] = 'shake256';
-
 
414
                foreach ($algos as $algo) {
-
 
415
                        if ($algo == 'md5') continue; // use UUIDv3 instead
-
 
416
                        if ($algo == 'sha1') continue; // use UUIDv5 instead
-
 
417
                        $friendlyName = strtoupper($algo);
-
 
418
 
-
 
419
                        if ($algo == 'shake128') $bits = 999;
-
 
420
                        else if ($algo == 'shake256') $bits = 999;
-
 
421
                        else $bits = strlen(hash($algo, '', true)) * 8;
-
 
422
                        if ($bits < 128) $friendlyName .= " (Small hash size! $bits bits)";
-
 
423
 
-
 
424
                        $space = $algo;
-
 
425
                        $tmp[$friendlyName] = '<option value="8_namebased_'.$space.'">'.htmlentities($friendlyName).'</option>';
-
 
426
                }
-
 
427
                natsort($tmp);
-
 
428
                foreach ($tmp as $html) {
-
 
429
                        echo "\t\t$html\n";
-
 
430
                }
-
 
431
 
463
                ?>
432
                ?>
464
        </select><font size="-1"><span id="nb_hash_info"></span></font><br>
433
        </select><font size="-1"><span id="nb_hash_info"></span></font><br>
465
        <label>Namespace:</label><select name="namespace_choose" id="nb_nsc" onchange="javascript:nb_ns_choose();">
434
        <label>Namespace:</label><select name="namespace_choose" id="nb_nsc" onchange="javascript:nb_ns_choose();">
466
                <option value="dns">DNS</option>
435
                <option value="dns">DNS</option>
467
                <option value="url">URL</option>
436
                <option value="url">URL</option>
Line 480... Line 449...
480
<script>
449
<script>
481
function nb_version_choose() {
450
function nb_version_choose() {
482
        var ver = document.getElementById('nb_version').value;
451
        var ver = document.getElementById('nb_version').value;
483
        document.getElementById('nb_create_btn').value = 'Create UUIDv' + ver.substr(0,1);
452
        document.getElementById('nb_create_btn').value = 'Create UUIDv' + ver.substr(0,1);
484
        var x = ver.split('_namebased_');
453
        var x = ver.split('_namebased_');
485
        if (x.length == 2) {
454
        if ((x.length == 2) && (x[1].length == 36)) {
486
                document.getElementById('nb_hash_info').innerHTML = ' (UUIDv8 Hash Space ID: ' + x[1] + ')';
455
                document.getElementById('nb_hash_info').innerHTML = ' (UUIDv8 Hash Space ID: ' + x[1] + ')';
487
        } else {
456
        } else {
488
                document.getElementById('nb_hash_info').innerHTML = '';
457
                document.getElementById('nb_hash_info').innerHTML = '';
489
        }
458
        }
490
 
459