Botan 3.11.0
Crypto and TLS for C&
Botan::UUID Class Referencefinal

#include <uuid.h>

Public Member Functions

const std::vector< uint8_t > & binary_value () const
bool is_valid () const
bool operator!= (const UUID &other) const
bool operator== (const UUID &other) const
std::string to_string () const
 UUID ()=default
BOTAN_FUTURE_EXPLICIT UUID (const std::vector< uint8_t > &blob)
BOTAN_FUTURE_EXPLICIT UUID (RandomNumberGenerator &rng)
BOTAN_FUTURE_EXPLICIT UUID (std::string_view uuid_str)

Detailed Description

Definition at line 21 of file uuid.h.

Constructor & Destructor Documentation

◆ UUID() [1/4]

Botan::UUID::UUID ( )
default

Create an uninitialized UUID object

References BOTAN_FUTURE_EXPLICIT, to_string(), and UUID().

Referenced by operator!=(), operator==(), and UUID().

◆ UUID() [2/4]

Botan::UUID::UUID ( RandomNumberGenerator & rng)

Create a random UUID

Definition at line 18 of file uuid.cpp.

18 {
19 m_uuid.resize(16);
20 rng.randomize(m_uuid.data(), m_uuid.size());
21
22 // Mark as a random v4 UUID (RFC 4122 sec 4.4)
23 m_uuid[6] = 0x40 | (m_uuid[6] & 0x0F);
24
25 // Set reserved bits
26 m_uuid[8] = 0x80 | (m_uuid[8] & 0x3F);
27}

References Botan::RandomNumberGenerator::randomize().

◆ UUID() [3/4]

Botan::UUID::UUID ( const std::vector< uint8_t > & blob)

Load a UUID from a 16 byte vector

Definition at line 29 of file uuid.cpp.

29 {
30 if(blob.size() != 16) {
31 throw Invalid_Argument("Bad UUID blob " + hex_encode(blob));
32 }
33
34 m_uuid = blob;
35}
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:34

References Botan::hex_encode().

◆ UUID() [4/4]

Botan::UUID::UUID ( std::string_view uuid_str)

Decode a UUID string

Definition at line 37 of file uuid.cpp.

37 {
38 if(uuid_str.size() != 36 || uuid_str[8] != '-' || uuid_str[13] != '-' || uuid_str[18] != '-' ||
39 uuid_str[23] != '-') {
40 throw Invalid_Argument(fmt("Bad UUID '{}'", uuid_str));
41 }
42
43 std::string just_hex;
44 for(const char c : uuid_str) {
45 if(c == '-') {
46 continue;
47 }
48
49 just_hex += c;
50 }
51
52 m_uuid = hex_decode(just_hex);
53
54 if(m_uuid.size() != 16) {
55 throw Invalid_Argument(fmt("Bad UUID '{}'", uuid_str));
56 }
57}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition hex.cpp:72

References Botan::fmt(), and Botan::hex_decode().

Member Function Documentation

◆ binary_value()

const std::vector< uint8_t > & Botan::UUID::binary_value ( ) const
inline

Definition at line 48 of file uuid.h.

48{ return m_uuid; }

◆ is_valid()

bool Botan::UUID::is_valid ( ) const
inline

Definition at line 54 of file uuid.h.

54{ return m_uuid.size() == 16; }

Referenced by to_string().

◆ operator!=()

bool Botan::UUID::operator!= ( const UUID & other) const
inline

Definition at line 52 of file uuid.h.

52{ return !(*this == other); }

References UUID().

◆ operator==()

bool Botan::UUID::operator== ( const UUID & other) const
inline

Definition at line 50 of file uuid.h.

50{ return m_uuid == other.m_uuid; }

References UUID().

◆ to_string()

std::string Botan::UUID::to_string ( ) const

Convert the UUID to a string

Definition at line 59 of file uuid.cpp.

59 {
60 if(!is_valid()) {
61 throw Invalid_State("UUID object is empty cannot convert to string");
62 }
63
64 const std::string raw = hex_encode(m_uuid);
65
66 std::ostringstream formatted;
67
68 for(size_t i = 0; i != raw.size(); ++i) {
69 if(i == 8 || i == 12 || i == 16 || i == 20) {
70 formatted << "-";
71 }
72 formatted << raw[i];
73 }
74
75 return formatted.str();
76}
bool is_valid() const
Definition uuid.h:54

References Botan::hex_encode(), and is_valid().

Referenced by UUID().


The documentation for this class was generated from the following files: