-
Notifications
You must be signed in to change notification settings - Fork 31
/
Uuid.cpp
47 lines (40 loc) · 1.14 KB
/
Uuid.cpp
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
#include"Util/Str.hpp"
#include"Util/make_unique.hpp"
#include"Uuid.hpp"
#include<algorithm>
#include<basicsecure.h>
#include<stdexcept>
namespace {
std::uint8_t const zero[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
}
Uuid Uuid::random() {
auto rv = Uuid();
rv.pimpl = Util::make_unique<Impl>();
BASICSECURE_RAND(rv.pimpl->data, sizeof(rv.pimpl->data));
return rv;
}
bool Uuid::operator==(Uuid const& o) const {
auto a = pimpl ? pimpl->data : zero;
auto b = o.pimpl ? o.pimpl->data : zero;
return basicsecure_eq(a, b, 16);
}
Uuid::operator bool() const {
if (!pimpl)
return false;
return !basicsecure_eq(pimpl->data, zero, 16);
}
Uuid::operator std::string() const {
if (!pimpl)
return "00000000000000000000000000000000";
return Util::Str::hexdump(pimpl->data, 16);
}
Uuid::Uuid(std::string const& s) {
pimpl = Util::make_unique<Impl>();
auto buf = Util::Str::hexread(s);
if (buf.size() != 16)
throw Util::BacktraceException<std::invalid_argument>("Uuid: invalid input string.");
std::copy(buf.begin(), buf.end(), pimpl->data);
}
bool Uuid::valid_string(std::string const& s) {
return Util::Str::ishex(s) && s.size() == 32;
}