| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #include "common/uuid.hpp"
- #include <openssl/rand.h>
- #include <cstring>
- namespace smartbotic::common {
- thread_local std::mt19937_64 UUID::generator_{std::random_device{}()};
- std::string UUID::generate() {
- unsigned char bytes[16];
- RAND_bytes(bytes, 16);
- // Set version 4 (random)
- bytes[6] = (bytes[6] & 0x0f) | 0x40;
- // Set variant (RFC 4122)
- bytes[8] = (bytes[8] & 0x3f) | 0x80;
- std::ostringstream ss;
- ss << std::hex << std::setfill('0');
- for (int i = 0; i < 16; ++i) {
- if (i == 4 || i == 6 || i == 8 || i == 10) {
- ss << '-';
- }
- ss << std::setw(2) << static_cast<int>(bytes[i]);
- }
- return ss.str();
- }
- std::string UUID::generatePrefixed(const std::string& prefix) {
- return prefix + "_" + generate();
- }
- bool UUID::isValid(const std::string& uuid) {
- if (uuid.length() != 36) {
- return false;
- }
- for (size_t i = 0; i < uuid.length(); ++i) {
- char c = uuid[i];
- if (i == 8 || i == 13 || i == 18 || i == 23) {
- if (c != '-') return false;
- } else {
- if (!std::isxdigit(c)) return false;
- }
- }
- return true;
- }
- } // namespace smartbotic::common
|