blob: c81cae6657c72557134bc0ee842ee553f8f1e008 (
plain)
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
|
/*
* UUID type
* (C) 2015,2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/uuid.h>
#include <botan/rng.h>
#include <botan/hex.h>
#include <sstream>
namespace Botan {
UUID::UUID(RandomNumberGenerator& rng)
{
m_uuid.resize(16);
rng.randomize(m_uuid.data(), m_uuid.size());
// Mark as a random v4 UUID (RFC 4122 sec 4.4)
m_uuid[6] = 0x40 | (m_uuid[6] & 0x0F);
// Set reserved bits
m_uuid[8] = 0x80 | (m_uuid[8] & 0x3F);
}
UUID::UUID(const std::vector<uint8_t>& blob)
{
if(blob.size() != 16)
{
throw Invalid_Argument("Bad UUID blob " + hex_encode(blob));
}
m_uuid = blob;
}
UUID::UUID(const std::string& uuid_str)
{
if(uuid_str.size() != 36 ||
uuid_str[8] != '-' ||
uuid_str[13] != '-' ||
uuid_str[18] != '-' ||
uuid_str[23] != '-')
{
throw Invalid_Argument("Bad UUID '" + uuid_str + "'");
}
std::string just_hex;
for(size_t i = 0; i != uuid_str.size(); ++i)
{
char c = uuid_str[i];
if(c == '-')
continue;
just_hex += c;
}
m_uuid = hex_decode(just_hex);
if(m_uuid.size() != 16)
{
throw Invalid_Argument("Bad UUID '" + uuid_str + "'");
}
}
std::string UUID::to_string() const
{
if(is_valid() == false)
throw Invalid_State("UUID object is empty cannot convert to string");
std::string h = hex_encode(m_uuid);
h.insert(8, "-");
h.insert(13, "-");
h.insert(18, "-");
h.insert(23, "-");
return h;
}
}
|