@@ -24,16 +24,18 @@ final class Uuid
24
24
const UUID_VARIANT_OTHER = 3 ;
25
25
const UUID_TYPE_DEFAULT = 0 ;
26
26
const UUID_TYPE_TIME = 1 ;
27
- const UUID_TYPE_DCE = 4 ;
28
- const UUID_TYPE_NAME = 1 ;
27
+ const UUID_TYPE_MD5 = 3 ;
28
+ const UUID_TYPE_DCE = 4 ; // Deprecated alias
29
+ const UUID_TYPE_NAME = 1 ; // Deprecated alias
29
30
const UUID_TYPE_RANDOM = 4 ;
31
+ const UUID_TYPE_SHA1 = 5 ;
30
32
const UUID_TYPE_NULL = -1 ;
31
33
const UUID_TYPE_INVALID = -42 ;
32
34
33
35
public static function uuid_create ($ uuid_type = UUID_TYPE_DEFAULT )
34
36
{
35
37
if (!\is_int ($ uuid_type )) {
36
- trigger_error (sprintf (" uuid_create() expects parameter 1 to be int, %s given " , gettype ($ uuid_type )), E_USER_WARNING );
38
+ trigger_error (sprintf (' uuid_create() expects parameter 1 to be int, %s given ' , gettype ($ uuid_type )), E_USER_WARNING );
37
39
38
40
return null ;
39
41
}
@@ -48,14 +50,97 @@ public static function uuid_create($uuid_type = UUID_TYPE_DEFAULT)
48
50
return self ::uuid_generate_random ();
49
51
default :
50
52
trigger_error (sprintf ("Unknown/invalid UUID type '%d' requested, using default type instead " , $ uuid_type ), E_USER_WARNING );
53
+
51
54
return self ::uuid_generate_random ();
52
55
}
53
56
}
54
57
58
+ public static function uuid_generate_md5 ($ uuid_ns , $ name )
59
+ {
60
+ if (!\is_string ($ uuid_ns )) {
61
+ trigger_error (sprintf ('uuid_generate_md5() expects parameter 1 to be string, %s given ' , gettype ($ uuid_ns )), E_USER_WARNING );
62
+
63
+ return null ;
64
+ }
65
+
66
+ if (!\is_string ($ name )) {
67
+ trigger_error (sprintf ('uuid_generate_md5() expects parameter 2 to be string, %s given ' , gettype ($ name )), E_USER_WARNING );
68
+
69
+ return null ;
70
+ }
71
+
72
+ if (null === self ::uuid_parse_as_array ($ uuid_ns )) {
73
+ return false ;
74
+ }
75
+
76
+ $ ctx = hash_init ('md5 ' );
77
+ hash_update ($ ctx , self ::uuid_parse ($ uuid_ns ));
78
+ hash_update ($ ctx , $ name );
79
+ $ hash = hash_final ($ ctx );
80
+
81
+ return sprintf ('%08s-%04s-%04x-%04x-%012s ' ,
82
+ // 32 bits for "time_low"
83
+ substr ($ hash , 0 , 8 ),
84
+ // 16 bits for "time_mid"
85
+ substr ($ hash , 8 , 4 ),
86
+ // 16 bits for "time_hi_and_version",
87
+ // four most significant bits holds version number 3
88
+ hexdec (substr ($ hash , 12 , 4 )) & 0x0fff | 0x3000 ,
89
+ // 16 bits:
90
+ // * 8 bits for "clk_seq_hi_res",
91
+ // * 8 bits for "clk_seq_low",
92
+ hexdec (substr ($ hash , 16 , 4 )) & 0x3fff | 0x8000 ,
93
+ // 48 bits for "node"
94
+ substr ($ hash , 20 , 12 )
95
+ );
96
+ }
97
+
98
+ public static function uuid_generate_sha1 ($ uuid_ns , $ name )
99
+ {
100
+ if (!\is_string ($ uuid_ns )) {
101
+ trigger_error (sprintf ('uuid_generate_sha1() expects parameter 1 to be string, %s given ' , gettype ($ uuid_ns )), E_USER_WARNING );
102
+
103
+ return null ;
104
+ }
105
+
106
+ if (!\is_string ($ name )) {
107
+ trigger_error (sprintf ('uuid_generate_sha1() expects parameter 2 to be string, %s given ' , gettype ($ name )), E_USER_WARNING );
108
+
109
+ return null ;
110
+ }
111
+
112
+ if (null === self ::uuid_parse_as_array ($ uuid_ns )) {
113
+ return false ;
114
+ }
115
+
116
+ $ ctx = hash_init ('sha1 ' );
117
+ hash_update ($ ctx , self ::uuid_parse ($ uuid_ns ));
118
+ hash_update ($ ctx , $ name );
119
+ $ hash = hash_final ($ ctx );
120
+
121
+ return sprintf ('%08s-%04s-%04x-%04x-%012s ' ,
122
+ // 32 bits for "time_low"
123
+ substr ($ hash , 0 , 8 ),
124
+ // 16 bits for "time_mid"
125
+ substr ($ hash , 8 , 4 ),
126
+ // 16 bits for "time_hi_and_version",
127
+ // four most significant bits holds version number 5
128
+ hexdec (substr ($ hash , 12 , 4 )) & 0x0fff | 0x5000 ,
129
+ // 16 bits:
130
+ // * 8 bits for "clk_seq_hi_res",
131
+ // * 8 bits for "clk_seq_low",
132
+ // WARNING: On old libuuid version, there is a bug. 0x0fff is used instead of 0x3fff
133
+ // See https://github.com/karelzak/util-linux/commit/d6ddf07d31dfdc894eb8e7e6842aa856342c526e
134
+ (hexdec (substr ($ hash , 16 , 4 )) & 0x3fff ) | 0x8000 ,
135
+ // 48 bits for "node"
136
+ substr ($ hash , 20 , 12 )
137
+ );
138
+ }
139
+
55
140
public static function uuid_is_valid ($ uuid )
56
141
{
57
142
if (!\is_string ($ uuid )) {
58
- trigger_error (sprintf (" uuid_is_valid() expects parameter 1 to be string, %s given " , gettype ($ uuid )), E_USER_WARNING );
143
+ trigger_error (sprintf (' uuid_is_valid() expects parameter 1 to be string, %s given ' , gettype ($ uuid )), E_USER_WARNING );
59
144
60
145
return null ;
61
146
}
@@ -66,13 +151,13 @@ public static function uuid_is_valid($uuid)
66
151
public static function uuid_compare ($ uuid1 , $ uuid2 )
67
152
{
68
153
if (!\is_string ($ uuid1 )) {
69
- trigger_error (sprintf (" uuid_compare() expects parameter 1 to be string, %s given " , gettype ($ uuid1 )), E_USER_WARNING );
154
+ trigger_error (sprintf (' uuid_compare() expects parameter 1 to be string, %s given ' , gettype ($ uuid1 )), E_USER_WARNING );
70
155
71
156
return null ;
72
157
}
73
158
74
159
if (!\is_string ($ uuid2 )) {
75
- trigger_error (sprintf (" uuid_compare() expects parameter 2 to be string, %s given " , gettype ($ uuid2 )), E_USER_WARNING );
160
+ trigger_error (sprintf (' uuid_compare() expects parameter 2 to be string, %s given ' , gettype ($ uuid2 )), E_USER_WARNING );
76
161
77
162
return null ;
78
163
}
@@ -99,7 +184,7 @@ public static function uuid_compare($uuid1, $uuid2)
99
184
public static function uuid_is_null ($ uuid )
100
185
{
101
186
if (!\is_string ($ uuid )) {
102
- trigger_error (sprintf (" uuid_is_null() expects parameter 1 to be string, %s given " , gettype ($ uuid )), E_USER_WARNING );
187
+ trigger_error (sprintf (' uuid_is_null() expects parameter 1 to be string, %s given ' , gettype ($ uuid )), E_USER_WARNING );
103
188
104
189
return null ;
105
190
}
@@ -110,7 +195,7 @@ public static function uuid_is_null($uuid)
110
195
public static function uuid_type ($ uuid )
111
196
{
112
197
if (!\is_string ($ uuid )) {
113
- trigger_error (sprintf (" uuid_type() expects parameter 1 to be string, %s given " , gettype ($ uuid )), E_USER_WARNING );
198
+ trigger_error (sprintf (' uuid_type() expects parameter 1 to be string, %s given ' , gettype ($ uuid )), E_USER_WARNING );
114
199
115
200
return null ;
116
201
}
@@ -129,7 +214,7 @@ public static function uuid_type($uuid)
129
214
public static function uuid_variant ($ uuid )
130
215
{
131
216
if (!\is_string ($ uuid )) {
132
- trigger_error (sprintf (" uuid_variant() expects parameter 1 to be string, %s given " , gettype ($ uuid )), E_USER_WARNING );
217
+ trigger_error (sprintf (' uuid_variant() expects parameter 1 to be string, %s given ' , gettype ($ uuid )), E_USER_WARNING );
133
218
134
219
return null ;
135
220
}
@@ -158,7 +243,7 @@ public static function uuid_variant($uuid)
158
243
public static function uuid_time ($ uuid )
159
244
{
160
245
if (!\is_string ($ uuid )) {
161
- trigger_error (sprintf (" uuid_time() expects parameter 1 to be string, %s given " , gettype ($ uuid )), E_USER_WARNING );
246
+ trigger_error (sprintf (' uuid_time() expects parameter 1 to be string, %s given ' , gettype ($ uuid )), E_USER_WARNING );
162
247
163
248
return null ;
164
249
}
@@ -185,7 +270,7 @@ public static function uuid_time($uuid)
185
270
public static function uuid_mac ($ uuid )
186
271
{
187
272
if (!\is_string ($ uuid )) {
188
- trigger_error (sprintf (" uuid_mac() expects parameter 1 to be string, %s given " , gettype ($ uuid )), E_USER_WARNING );
273
+ trigger_error (sprintf (' uuid_mac() expects parameter 1 to be string, %s given ' , gettype ($ uuid )), E_USER_WARNING );
189
274
190
275
return null ;
191
276
}
@@ -204,7 +289,7 @@ public static function uuid_mac($uuid)
204
289
public static function uuid_parse ($ uuid )
205
290
{
206
291
if (!\is_string ($ uuid )) {
207
- trigger_error (sprintf (" uuid_parse() expects parameter 1 to be string, %s given " , gettype ($ uuid )), E_USER_WARNING );
292
+ trigger_error (sprintf (' uuid_parse() expects parameter 1 to be string, %s given ' , gettype ($ uuid )), E_USER_WARNING );
208
293
209
294
return null ;
210
295
}
@@ -221,7 +306,7 @@ public static function uuid_parse($uuid)
221
306
public static function uuid_unparse ($ uuidAsBinary )
222
307
{
223
308
if (!\is_string ($ uuidAsBinary )) {
224
- trigger_error (sprintf (" uuid_unparse() expects parameter 1 to be string, %s given " , gettype ($ uuidAsBinary )), E_USER_WARNING );
309
+ trigger_error (sprintf (' uuid_unparse() expects parameter 1 to be string, %s given ' , gettype ($ uuidAsBinary )), E_USER_WARNING );
225
310
226
311
return null ;
227
312
}
@@ -342,12 +427,12 @@ private static function uuid_parse_as_array($uuid)
342
427
return null ;
343
428
}
344
429
345
- return array (
430
+ return [
346
431
'time_low ' => hexdec ($ matches ['time_low ' ]),
347
432
'time_mid ' => hexdec ($ matches ['time_mid ' ]),
348
433
'time_hi_and_version ' => hexdec ($ matches ['time_hi_and_version ' ]),
349
434
'clock_seq ' => hexdec ($ matches ['clock_seq ' ]),
350
435
'node ' => hexdec ($ matches ['node ' ]),
351
- ) ;
436
+ ] ;
352
437
}
353
438
}
0 commit comments