Subversion Repositories uuid_mac_utils

Rev

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

Rev 15 Rev 24
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-04-29
6
 * Version 2023-05-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 26... Line 26...
26
define('UUID_NAMEBASED_NS_DNS',     '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
26
define('UUID_NAMEBASED_NS_DNS',     '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
27
define('UUID_NAMEBASED_NS_URL',     '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
27
define('UUID_NAMEBASED_NS_URL',     '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
28
define('UUID_NAMEBASED_NS_OID',     '6ba7b812-9dad-11d1-80b4-00c04fd430c8');
28
define('UUID_NAMEBASED_NS_OID',     '6ba7b812-9dad-11d1-80b4-00c04fd430c8');
29
define('UUID_NAMEBASED_NS_X500_DN', '6ba7b814-9dad-11d1-80b4-00c04fd430c8');
29
define('UUID_NAMEBASED_NS_X500_DN', '6ba7b814-9dad-11d1-80b4-00c04fd430c8');
30
 
30
 
-
 
31
function _random_int($min, $max) {
-
 
32
        // This function tries a CSRNG and falls back to a RNG if no CSRNG is available
-
 
33
        try {
-
 
34
                return random_int($min, $max);
-
 
35
        } catch (Exception $e) {
-
 
36
                return mt_rand($min, $max);
-
 
37
        }
-
 
38
}
-
 
39
 
31
function uuid_valid($uuid) {
40
function uuid_valid($uuid) {
32
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
41
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
33
        $uuid = strtoupper($uuid);
42
        $uuid = strtoupper($uuid);
34
        #$uuid = trim($uuid);
43
        #$uuid = trim($uuid);
35
 
44
 
Line 406... Line 415...
406
 
415
 
407
        /*
416
        /*
408
         * We don't support saved state information and generate
417
         * We don't support saved state information and generate
409
         * a random clock sequence each time.
418
         * a random clock sequence each time.
410
         */
419
         */
411
        $uuid['clock_seq_hi'] = 0x80 | mt_rand(0, 64);
420
        $uuid['clock_seq_hi'] = 0x80 | _random_int(0, 64);
412
        $uuid['clock_seq_low'] = mt_rand(0, 255);
421
        $uuid['clock_seq_low'] = _random_int(0, 255);
413
 
422
 
414
        /*
423
        /*
415
         * Node should be set to the 48-bit IEEE node identifier
424
         * Node should be set to the 48-bit IEEE node identifier
416
         */
425
         */
417
        $mac = get_mac_address();
426
        $mac = get_mac_address();
Line 588... Line 597...
588
        }
597
        }
589
 
598
 
590
        # Make the UUID by ourselves
599
        # Make the UUID by ourselves
591
        # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php
600
        # Source: http://rogerstringer.com/2013/11/15/generate-uuids-php
592
        return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
601
        return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
593
                mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
602
                _random_int( 0, 0xffff ), _random_int( 0, 0xffff ),
594
                mt_rand( 0, 0xffff ),
603
                _random_int( 0, 0xffff ),
595
                mt_rand( 0, 0x0fff ) | 0x4000,
604
                _random_int( 0, 0x0fff ) | 0x4000,
596
                mt_rand( 0, 0x3fff ) | 0x8000,
605
                _random_int( 0, 0x3fff ) | 0x8000,
597
                mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
606
                _random_int( 0, 0xffff ), _random_int( 0, 0xffff ), _random_int( 0, 0xffff )
598
        );
607
        );
599
}
608
}
600
 
609
 
601
// Version 5 (SHA1 name based) UUID
610
// Version 5 (SHA1 name based) UUID
602
function gen_uuid_sha1_namebased($namespace_uuid, $name) {
611
function gen_uuid_sha1_namebased($namespace_uuid, $name) {