Uuid::v3()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Platine Stdlib
5
 *
6
 * Platine Stdlib is a the collection of frequently used php features
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Stdlib
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Uuid.php
33
 *
34
 * The UUID helper class
35
 *
36
 * The following class generates VALID RFC 4211 COMPLIANT Universally Unique IDentifiers (UUID)
37
 * version 3, 4 and 5.
38
 * Version 3 and 5 UUIDs are named based. They require a name space (another valid UUID) and a value
39
 * (the name). Given the same name space and name, the output is always the same.
40
 * Version 4 UUIDs are pseudo-random.
41
 * UUIDs generated below validates using OSSP UUID Tool, and output for named-based UUIDs
42
 * are exactly the same. This is a pure PHP implementation.
43
 *
44
 * @see "Andrew Moore" contribution note on https://www.php.net/manual/en/function.uniqid.php
45
 *
46
 *  @package    Platine\Stdlib\Helper
47
 *  @author Platine Developers Team
48
 *  @copyright  Copyright (c) 2020
49
 *  @license    http://opensource.org/licenses/MIT  MIT License
50
 *  @link   https://www.platine-php.com
51
 *  @version 1.0.0
52
 *  @filesource
53
 */
54
55
declare(strict_types=1);
56
57
namespace Platine\Stdlib\Helper;
58
59
use InvalidArgumentException;
60
61
/**
62
 * @class Uuid
63
 * @package Platine\Stdlib\Helper
64
 */
65
class Uuid
66
{
67
    /**
68
     * Generate UUID v3
69
     * @param string $namespace
70
     * @param string $name
71
     * @return string
72
     */
73
    public static function v3(string $namespace, string $name): string
74
    {
75
        return self::v3v5($namespace, $name, 'v3');
76
    }
77
78
    /**
79
     * Generate UUID v4
80
     * @return string
81
     */
82
    public static function v4(): string
83
    {
84
        return sprintf(
85
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
86
            // 32 bits for "time_low"
87
            mt_rand(0, 0xffff),
88
            mt_rand(0, 0xffff),
89
            // 16 bits for "time_mid"
90
            mt_rand(0, 0xffff),
91
            // 16 bits for "time_hi_and_version",
92
            // four most significant bits holds version number 4
93
            mt_rand(0, 0x0fff) | 0x4000,
94
            // 16 bits, 8 bits for "clk_seq_hi_res",
95
            // 8 bits for "clk_seq_low",
96
            // two most significant bits holds zero and one for variant DCE1.1
97
            mt_rand(0, 0x3fff) | 0x8000,
98
            // 48 bits for "node"
99
            mt_rand(0, 0xffff),
100
            mt_rand(0, 0xffff),
101
            mt_rand(0, 0xffff)
102
        );
103
    }
104
105
    /**
106
     * Generate UUID v5
107
     * @param string $namespace
108
     * @param string $name
109
     * @return string
110
     */
111
    public static function v5(string $namespace, string $name): string
112
    {
113
        return self::v3v5($namespace, $name, 'v5');
114
    }
115
116
117
    /**
118
     * Whether the given UUID is valid
119
     * @param string $uuid
120
     * @return bool
121
     */
122
    public static function isValid(string $uuid): bool
123
    {
124
        return preg_match(
125
            '/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i',
126
            $uuid
127
        ) === 1;
128
    }
129
130
    /**
131
     * Generate UUID v3 and v5
132
     * @param string $namespace
133
     * @param string $name
134
     * @param string $type can be v3 or v5
135
     * @return string
136
     */
137
    protected static function v3v5(string $namespace, string $name, string $type = 'v3'): string
138
    {
139
        if (self::isValid($namespace) === false) {
140
            throw new InvalidArgumentException(sprintf('Invalid namespace [%s] provided', $namespace));
141
        }
142
143
        // Get hexadecimal components of namespace
144
        $nhex = str_replace(['-', '{', '}'], '', $namespace);
145
146
        // Binary Value
147
        $binaryStr = '';
148
149
        // Convert Namespace UUID to bits
150
        for ($i = 0; $i < strlen($nhex); $i += 2) {
151
            $binaryStr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
152
        }
153
154
        // Calculate hash value
155
        if ($type === 'v3') {
156
            $hash = md5($binaryStr . $name);
157
        } else {
158
            $hash = sha1($binaryStr . $name);
159
        }
160
161
        return sprintf(
162
            '%08s-%04s-%04x-%04x-%12s',
163
            // 32 bits for "time_low"
164
            substr($hash, 0, 8),
165
            // 16 bits for "time_mid"
166
            substr($hash, 8, 4),
167
            // 16 bits for "time_hi_and_version",
168
            // four most significant bits holds version number 3 and 5
169
            $type === 'v3' ?
170
                (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000 : (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
171
            // 16 bits, 8 bits for "clk_seq_hi_res",
172
            // 8 bits for "clk_seq_low",
173
            // two most significant bits holds zero and one for variant DCE1.1
174
175
            (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
176
            // 48 bits for "node"
177
            substr($hash, 20, 12)
178
        );
179
    }
180
}
181