1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Helpers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class UUID for creation unique ids |
7
|
|
|
* @package DrMVC\Helpers |
8
|
|
|
*/ |
9
|
|
|
class UUID |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Generate identifier of 3rd version |
13
|
|
|
* |
14
|
|
|
* @param string $namespace |
15
|
|
|
* @param string $name |
16
|
|
|
* @return bool|string |
17
|
|
|
*/ |
18
|
|
|
public static function v3($namespace, $name) |
19
|
|
|
{ |
20
|
|
|
if (!Validate::isValidUUID($namespace)) { |
21
|
|
|
return false; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// Get hexadecimal components of namespace |
25
|
|
|
$nhex = str_replace(array('-', '{', '}'), '', $namespace); |
26
|
|
|
|
27
|
|
|
// Binary Value |
28
|
|
|
$nstr = ''; |
29
|
|
|
|
30
|
|
|
// Convert Namespace UUID to bits |
31
|
|
|
for ($i = 0; $i < strlen($nhex); $i += 2) { |
32
|
|
|
$nstr .= \chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Calculate hash value |
36
|
|
|
$hash = md5($nstr . $name); |
37
|
|
|
|
38
|
|
|
return sprintf('%08s-%04s-%04x-%04x-%12s', |
39
|
|
|
|
40
|
|
|
// 32 bits for "time_low" |
41
|
|
|
substr($hash, 0, 8), |
42
|
|
|
|
43
|
|
|
// 16 bits for "time_mid" |
44
|
|
|
substr($hash, 8, 4), |
45
|
|
|
|
46
|
|
|
// 16 bits for "time_hi_and_version", |
47
|
|
|
// four most significant bits holds version number 3 |
48
|
|
|
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
49
|
|
|
|
50
|
|
|
// 16 bits, 8 bits for "clk_seq_hi_res", |
51
|
|
|
// 8 bits for "clk_seq_low", |
52
|
|
|
// two most significant bits holds zero and one for variant DCE1.1 |
53
|
|
|
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
54
|
|
|
|
55
|
|
|
// 48 bits for "node" |
56
|
|
|
substr($hash, 20, 12) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Generate identifier of 3rd version |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public static function v4(): string |
66
|
|
|
{ |
67
|
|
|
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
68
|
|
|
|
69
|
|
|
// 32 bits for "time_low" |
70
|
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
71
|
|
|
|
72
|
|
|
// 16 bits for "time_mid" |
73
|
|
|
mt_rand(0, 0xffff), |
74
|
|
|
|
75
|
|
|
// 16 bits for "time_hi_and_version", |
76
|
|
|
// four most significant bits holds version number 4 |
77
|
|
|
mt_rand(0, 0x0fff) | 0x4000, |
78
|
|
|
|
79
|
|
|
// 16 bits, 8 bits for "clk_seq_hi_res", |
80
|
|
|
// 8 bits for "clk_seq_low", |
81
|
|
|
// two most significant bits holds zero and one for variant DCE1.1 |
82
|
|
|
mt_rand(0, 0x3fff) | 0x8000, |
83
|
|
|
|
84
|
|
|
// 48 bits for "node" |
85
|
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Generate identifier of 5th version |
91
|
|
|
* |
92
|
|
|
* @param string $namespace |
93
|
|
|
* @param string $name |
94
|
|
|
* @return bool|string |
95
|
|
|
*/ |
96
|
|
|
public static function v5(string $namespace, string $name) |
97
|
|
|
{ |
98
|
|
|
if (!Validate::isValidUUID($namespace)) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Get hexadecimal components of namespace |
103
|
|
|
$nhex = str_replace(array('-', '{', '}'), '', $namespace); |
104
|
|
|
|
105
|
|
|
// Binary Value |
106
|
|
|
$nstr = ''; |
107
|
|
|
|
108
|
|
|
// Convert Namespace UUID to bits |
109
|
|
|
for ($i = 0; $i < strlen($nhex); $i += 2) { |
110
|
|
|
$nstr .= \chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Calculate hash value |
114
|
|
|
$hash = sha1($nstr . $name); |
115
|
|
|
|
116
|
|
|
return sprintf('%08s-%04s-%04x-%04x-%12s', |
117
|
|
|
|
118
|
|
|
// 32 bits for "time_low" |
119
|
|
|
substr($hash, 0, 8), |
120
|
|
|
|
121
|
|
|
// 16 bits for "time_mid" |
122
|
|
|
substr($hash, 8, 4), |
123
|
|
|
|
124
|
|
|
// 16 bits for "time_hi_and_version", |
125
|
|
|
// four most significant bits holds version number 5 |
126
|
|
|
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
127
|
|
|
|
128
|
|
|
// 16 bits, 8 bits for "clk_seq_hi_res", |
129
|
|
|
// 8 bits for "clk_seq_low", |
130
|
|
|
// two most significant bits holds zero and one for variant DCE1.1 |
131
|
|
|
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
132
|
|
|
|
133
|
|
|
// 48 bits for "node" |
134
|
|
|
substr($hash, 20, 12) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|