forked from eteran/cpp-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUUID.h
298 lines (238 loc) · 6.93 KB
/
UUID.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Evan Teran
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef UUID_H_
#define UUID_H_
#include <string>
#include <random>
#include <cassert>
#include <algorithm>
#include "MD5.h"
#include "SHA1.h"
class UUID {
private:
UUID() {
}
public:
static UUID from_string(std::string uuid) {
assert(is_valid(uuid));
// once we know it's valid, we can just strip out the optional
// stuff we don't really need
uuid.erase(std::remove(uuid.begin(), uuid.end(), '{'), uuid.end());
uuid.erase(std::remove(uuid.begin(), uuid.end(), '}'), uuid.end());
uuid.erase(std::remove(uuid.begin(), uuid.end(), '-'), uuid.end());
assert(uuid.size() == 32);
UUID r;
for(int i = 0; i < 16; ++i) {
const char ch[3] = { uuid[i * 2], uuid[i * 2 + 1], '\0' };
const uint8_t v = strtoul(ch, nullptr, 16);
r.v_[i] = v;
}
return r;
}
static UUID v3(const UUID &ns, const std::string &name) {
// To determine the version 3 UUID of a given name, the UUID of the namespace (e.g., 6ba7b810-9dad-11d1-80b4-00c04fd430c8 for a domain)
// is transformed to a string of bytes corresponding to its hexadecimal digits, concatenated with the input name, hashed with
// MD5 yielding 128 bits. Six bits are replaced by fixed values, four of these bits indicate the version, 0011 for version 3. Finally,
// the fixed hash is transformed back into the hexadecimal form with hyphens separating the parts relevant in other UUID versions.
UUID r;
std::vector<uint8_t> bin;
bin.reserve(16 + name.size());
for(int i = 0; i < 16; ++i) {
bin.push_back(ns[i]);
}
for(char ch : name) {
bin.push_back(ch);
}
auto digest = hash::MD5(bin.begin(), bin.end()).finalize();
auto bytes = digest.bytes();
for(int i = 0; i < 16; ++i) {
r.v_[i] = bytes[i];
}
// ensure correctness
r.v_[6] = (r.v_[6] & 0x0f) | 0x30; // make version 3
r.v_[8] = (r.v_[8] & 0x3f) | 0x80;
return r;
}
static UUID v4() {
UUID r;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0x00, 0xff);
r.v_[0] = dis(gen);
r.v_[1] = dis(gen);
r.v_[2] = dis(gen);
r.v_[3] = dis(gen);
r.v_[4] = dis(gen);
r.v_[5] = dis(gen);
r.v_[6] = (dis(gen) & 0x0f) | 0x40; // make version 4
r.v_[7] = dis(gen);
r.v_[8] = (dis(gen) & 0x3f) | 0x80; // make version 4
r.v_[9] = dis(gen);
r.v_[10] = dis(gen);
r.v_[11] = dis(gen);
r.v_[12] = dis(gen);
r.v_[13] = dis(gen);
r.v_[14] = dis(gen);
r.v_[15] = dis(gen);
return r;
}
static UUID v5(const UUID &ns, const std::string &name) {
// To determine the version 5 UUID of a given name, the UUID of the namespace (e.g., 6ba7b810-9dad-11d1-80b4-00c04fd430c8 for a domain)
// is transformed to a string of bytes corresponding to its hexadecimal digits, concatenated with the input name, hashed with
// SHA1 yielding 160 bits. Six bits are replaced by fixed values, four of these bits indicate the version, 0011 for version 5. Finally,
// the first 128 bits of the fixed hash is transformed back into the hexadecimal form with hyphens separating the parts relevant in other UUID versions.
UUID r;
std::vector<uint8_t> bin;
bin.reserve(16 + name.size());
for(int i = 0; i < 16; ++i) {
bin.push_back(ns[i]);
}
for(char ch : name) {
bin.push_back(ch);
}
auto digest = hash::SHA1(bin.begin(), bin.end()).finalize();
auto bytes = digest.bytes();
for(int i = 0; i < 16; ++i) {
r.v_[i] = bytes[i];
}
// ensure correctness
r.v_[6] = (r.v_[6] & 0x0f) | 0x50; // make version 5
r.v_[8] = (r.v_[8] & 0x3f) | 0x80;
return r;
}
static bool is_valid(const std::string &uuid) {
bool has_braces = false;
bool has_dashes = false;
int digit_index = 0;
auto it = uuid.begin();
// first test to see if we are dealing with a brace wrapped string
// if so, consume the brace and note it
if(it != uuid.end()) {
if(*it == '{') {
has_braces = true;
++it;
}
}
// consume digits as we go
while(it != uuid.end()) {
// at the very least, we expect a hex digit
if(!isxdigit(*it)) {
break;
}
// digits 12 and 16 have specific valid values
if(digit_index == 12) {
const char *p = strchr("12345", *it);
if(!p) {
return false;
}
} else if(digit_index == 16) {
const char *p = strchr("89abAB", *it);
if(!p) {
return false;
}
}
++digit_index;
++it;
// if we are at digit 8, then we test to see if there
// is a dash, if we see one, we want one at 12,16,20 too
if(it != uuid.end()) {
switch(digit_index) {
case 8:
if(*it == '-') {
has_dashes = true;
++it;
}
break;
case 12:
case 16:
case 20:
if(has_dashes) {
if(*it == '-') {
++it;
} else {
return false;
}
}
break;
}
}
}
// handle the closing brace
if(digit_index == 32 && has_braces) {
if(it == uuid.end()) {
return false;
}
if(*it++ != '}') {
return false;
}
}
// did we consume the whole string and get 32 digits?
return (digit_index == 32) && (it == uuid.end());
}
int version() const {
return (v_[6] >> 4) & 0x0f;
}
bool operator==(const UUID &rhs) const {
return std::memcmp(v_, rhs.v_, 16) == 0;
}
bool operator!=(const UUID &rhs) const {
return !(*this == rhs);
}
bool operator<(const UUID &rhs) const {
return std::memcmp(v_, rhs.v_, 16) < 0;
}
public:
std::string to_string() const {
char buffer[37];
snprintf(buffer, sizeof(buffer), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
v_[0],
v_[1],
v_[2],
v_[3],
v_[4],
v_[5],
v_[6],
v_[7],
v_[8],
v_[9],
v_[10],
v_[11],
v_[12],
v_[13],
v_[14],
v_[15]);
return buffer;
}
uint8_t operator[](size_t index) const {
assert(index < sizeof(v_));
return v_[index];
}
uint8_t& operator[](size_t index) {
assert(index < sizeof(v_));
return v_[index];
}
private:
uint8_t v_[16];
};
#endif