Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

uuid.h
Go to the documentation of this file.
1 /*
2 * UUID type
3 * (C) 2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #ifndef BOTAN_UUID_H__
9 #define BOTAN_UUID_H__
10
11 #include <botan/secmem.h>
12 #include <botan/hex.h>
13 #include <sstream>
14
15 namespace Botan {
16
17 // TODO: move to util?
18 class UUID
19 {
20 public:
21 // Represents an unassigned UUID object
22 UUID() : m_uuid(0) {}
23
24 UUID(RandomNumberGenerator& rng)
25 {
26 m_uuid.resize(16);
27 rng.randomize(m_uuid.data(), m_uuid.size());
28
29 // Mark as a random UUID (RFC 4122 sec 4.4)
30 m_uuid[6] = 0x40 | (m_uuid[6] & 0x0F);
31
32 // Set two reserved bits
33 m_uuid[8] = 0xC0 | (m_uuid[8] & 0x3F);
34 }
35
36 UUID(const std::vector<uint8_t>& blob)
37 {
38 if(blob.size() != 16)
39 {
40 throw Invalid_Argument("Bad UUID blob " + hex_encode(blob));
41 }
42
43 m_uuid = blob;
44 }
45
46 UUID(const std::string& uuid_str)
47 {
48 if(uuid_str.size() != 36 ||
49 uuid_str[8] != '-' ||
50 uuid_str[14] != '-' ||
51 uuid_str[19] != '-' ||
52 uuid_str[24] != '-')
53 {
54 throw Invalid_Argument("Bad UUID '" + uuid_str + "'");
55 }
56
57 std::string just_hex;
58 for(size_t i = 0; i != uuid_str.size(); ++i)
59 {
60 char c = uuid_str[i];
61
62 if(c == '-')
63 continue;
64
65 just_hex += c;
66 }
67
68 m_uuid = hex_decode(just_hex);
69
70 if(m_uuid.size() != 16)
71 {
72 throw Invalid_Argument("Bad UUID '" + uuid_str + "'");
73 }
74 }
75
76
77 std::string to_string() const
78 {
79 std::string h = hex_encode(m_uuid);
80
81 h.insert(8, "-");
82 h.insert(14, "-");
83 h.insert(19, "-");
84 h.insert(24, "-");
85
86 return h;
87 }
88
89 const std::vector<uint8_t> binary_value() const { return m_uuid; }
90
91 bool operator==(const UUID& other)
92 {
93 return m_uuid == other.m_uuid;
94 }
95
96 bool operator!=(const UUID& other) { return !(*this == other); }
97
98 bool is_valid() const { return m_uuid.size() == 16; }
99
100 private:
101 std::vector<uint8_t> m_uuid;
102 };
103
104 }
105
106 #endif
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::UUID::operator==
bool operator==(const UUID &other)
Definition: uuid.h:91
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0
Botan::UUID::is_valid
bool is_valid() const
Definition: uuid.h:98
Botan::UUID::operator!=
bool operator!=(const UUID &other)
Definition: uuid.h:96
Botan::UUID::binary_value
const std::vector< uint8_t > binary_value() const
Definition: uuid.h:89
Botan::UUID::to_string
std::string to_string() const
Definition: uuid.h:77
Botan::UUID::UUID
UUID(const std::vector< uint8_t > &blob)
Definition: uuid.h:36
Botan::UUID::UUID
UUID(RandomNumberGenerator &rng)
Definition: uuid.h:24
Botan::UUID
Definition: uuid.h:18
Botan
Definition: alg_id.cpp:13
Botan::hex_encode
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
Botan::hex_decode
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:49
Botan::UUID::UUID
UUID(const std::string &uuid_str)
Definition: uuid.h:46
Botan::UUID::UUID
UUID()
Definition: uuid.h:22