diff options
author | Jack Lloyd <[email protected]> | 2018-09-30 13:46:34 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-09-30 13:46:34 -0400 |
commit | ebd05a25df636988ec41391a5bd218b799e40455 (patch) | |
tree | 522ee878e9ea05a8d4a40462765c4b06cf47c690 /src/lib/utils | |
parent | 4328b9f1a494c8e1d632c67d6c560d975df70f58 (diff) |
Move UUID to utils, test it, and fix bugs.
Fixes #1695
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/uuid/info.txt | 8 | ||||
-rw-r--r-- | src/lib/utils/uuid/uuid.cpp | 82 | ||||
-rw-r--r-- | src/lib/utils/uuid/uuid.h | 66 |
3 files changed, 156 insertions, 0 deletions
diff --git a/src/lib/utils/uuid/info.txt b/src/lib/utils/uuid/info.txt new file mode 100644 index 000000000..b078146e8 --- /dev/null +++ b/src/lib/utils/uuid/info.txt @@ -0,0 +1,8 @@ +<defines> +UUID -> 20180930 +</defines> + +<requires> +rng +hex +</requires> diff --git a/src/lib/utils/uuid/uuid.cpp b/src/lib/utils/uuid/uuid.cpp new file mode 100644 index 000000000..c81cae665 --- /dev/null +++ b/src/lib/utils/uuid/uuid.cpp @@ -0,0 +1,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; + } + +} diff --git a/src/lib/utils/uuid/uuid.h b/src/lib/utils/uuid/uuid.h new file mode 100644 index 000000000..9d02fc2cd --- /dev/null +++ b/src/lib/utils/uuid/uuid.h @@ -0,0 +1,66 @@ +/* +* UUID type +* (C) 2015,2018 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_UUID_H_ +#define BOTAN_UUID_H_ + +#include <botan/types.h> +#include <vector> + +namespace Botan { + +class RandomNumberGenerator; + +class BOTAN_UNSTABLE_API UUID final + { + public: + /** + * Create an uninitialized UUID object + */ + UUID() : m_uuid() {} + + /** + * Create a random UUID + */ + UUID(RandomNumberGenerator& rng); + + /** + * Load a UUID from a 16 byte vector + */ + UUID(const std::vector<uint8_t>& blob); + + UUID& operator=(const UUID& other) = default; + UUID(const UUID& other) = default; + + /** + * Decode a UUID string + */ + UUID(const std::string& uuid_str); + + /** + * Convert the UUID to a string + */ + std::string to_string() const; + + const std::vector<uint8_t>& binary_value() const { return m_uuid; } + + bool operator==(const UUID& other) const + { + return m_uuid == other.m_uuid; + } + + bool operator!=(const UUID& other) const { return !(*this == other); } + + bool is_valid() const { return m_uuid.size() == 16; } + + private: + std::vector<uint8_t> m_uuid; + }; + +} + +#endif |