Botan 3.0.0
Crypto and TLS for C&
Public Member Functions | List of all members
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
 
UUIDoperator= (const UUID &other)=default
 
bool operator== (const UUID &other) const
 
std::string to_string () const
 
 UUID ()
 
 UUID (const std::vector< uint8_t > &blob)
 
 UUID (const UUID &other)=default
 
 UUID (RandomNumberGenerator &rng)
 
 UUID (std::string_view uuid_str)
 

Detailed Description

Definition at line 21 of file uuid.h.

Constructor & Destructor Documentation

◆ UUID() [1/5]

Botan::UUID::UUID ( )
inline

Create an uninitialized UUID object

Definition at line 27 of file uuid.h.

27: m_uuid() {}

◆ UUID() [2/5]

Botan::UUID::UUID ( RandomNumberGenerator rng)

Create a random UUID

Definition at line 16 of file uuid.cpp.

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

References Botan::RandomNumberGenerator::randomize().

◆ UUID() [3/5]

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

Load a UUID from a 16 byte vector

Definition at line 28 of file uuid.cpp.

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

References Botan::hex_encode().

◆ UUID() [4/5]

Botan::UUID::UUID ( const UUID other)
default

◆ UUID() [5/5]

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

Decode a UUID string

Definition at line 38 of file uuid.cpp.

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

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 52 of file uuid.h.

52{ return m_uuid; }

◆ is_valid()

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

Definition at line 61 of file uuid.h.

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

Referenced by Botan::TPM_PrivateKey::register_key(), and to_string().

◆ operator!=()

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

Definition at line 59 of file uuid.h.

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

◆ operator=()

UUID & Botan::UUID::operator= ( const UUID other)
default

◆ operator==()

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

Definition at line 54 of file uuid.h.

55 {
56 return m_uuid == other.m_uuid;
57 }

◆ to_string()

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

Convert the UUID to a string

Definition at line 66 of file uuid.cpp.

67 {
68 if(is_valid() == false)
69 throw Invalid_State("UUID object is empty cannot convert to string");
70
71 const std::string raw = hex_encode(m_uuid);
72
73 std::ostringstream formatted;
74
75 for(size_t i = 0; i != raw.size(); ++i)
76 {
77 if(i == 8 || i == 12 || i == 16 || i == 20)
78 formatted << "-";
79 formatted << raw[i];
80 }
81
82 return formatted.str();
83 }
bool is_valid() const
Definition: uuid.h:61

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

Referenced by Botan::TPM_PrivateKey::register_key().


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