stellar_ch

UUID v4 Generator

Oct 24th, 2021 (edited)
599
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // UUID v4 Generator
  2. //
  3. #include <iostream>
  4. #include <cstdint>
  5. #include <random>
  6. #include <array>
  7.  
  8. struct UUID
  9. {
  10.     uint32_t a;
  11.     uint16_t b;
  12.     uint16_t c;
  13.     std::array<uint8_t, 2> d;
  14.     std::array<uint8_t, 6> e;
  15. };
  16. std::ostream& operator<<(std::ostream& stream, const UUID& object)
  17. {
  18.     auto flags = stream.flags();
  19.     stream << std::hex;
  20.     stream.width(8);
  21.     stream.fill('0');
  22.     stream << object.a << "-";
  23.     stream.width(4);
  24.     stream.fill('0');
  25.     stream << object.b << "-" << object.c << "-";
  26.     for(auto i: object.d)
  27.     {
  28.         stream.width(2);
  29.         stream.fill('0');
  30.         stream << int(i);
  31.     }
  32.     stream << "-";
  33.     for(auto i: object.e)
  34.     {
  35.         stream.width(2);
  36.         stream.fill('0');
  37.         stream << int(i);
  38.     }
  39.     stream.flags(flags);
  40.     return stream;
  41. }
  42.  
  43. class UUID4Generator
  44. {
  45.     UUID4Generator() = delete;
  46.     public:
  47.     template<typename G>
  48.     static UUID generate(G& generator)
  49.     {
  50.         auto result = UUID();
  51.         auto rv_a = std::uniform_int_distribution<uint32_t>();
  52.         result.a = rv_a(generator);
  53.         auto rv_b = std::uniform_int_distribution<uint16_t>();
  54.         result.b = rv_b(generator);
  55.         auto rv_c = std::uniform_int_distribution<uint16_t>(0, 0x0FFF);
  56.         result.c = rv_c(generator) | 0x4000;
  57.         auto rv_d = std::uniform_int_distribution<uint8_t>();
  58.         result.d[0] = (rv_d(generator) & 0x3F) | 0x80;
  59.         result.d[1] = rv_d(generator);
  60.         for(auto& i: result.e)
  61.         {
  62.             i = rv_d(generator);
  63.         }
  64.         return result;
  65.     }
  66. };
  67.  
  68. int main(void)
  69. {
  70.     std::random_device rd;
  71.     auto rnd = std::mt19937_64(rd());
  72.     auto uuid = UUID4Generator::generate(rnd);
  73.     std::cout << "UUID: " << uuid << std::endl;
  74.     union { UUID uuid; std::array<int32_t, 4> d; } data = {uuid};
  75.     std::cout << "Minecraft NBT: [I;" << int32_t(data.d[0]) << "," <<
  76.         int32_t(data.d[1] >> 16 & 0xFFFF | data.d[1] << 16 & 0xFFFF0000) << "," <<
  77.         int32_t(data.d[2] >> 24 & 0xFF | data.d[2] >> 8 & 0xFF00 | data.d[2] << 8 & 0xFF0000 | data.d[2] << 24 & 0xFF000000) << "," <<
  78.         int32_t(data.d[3] >> 24 & 0xFF | data.d[3] >> 8 & 0xFF00 | data.d[3] << 8 & 0xFF0000 | data.d[3] << 24 & 0xFF000000) << "]" << std::endl;
  79.     std::cout << "HEX: 0x" << std::hex << uint32_t(data.d[0]) <<
  80.         uint32_t(data.d[1] >> 16 & 0xFFFF | data.d[1] << 16 & 0xFFFF0000) <<
  81.         uint32_t(data.d[2] >> 24 & 0xFF | data.d[2] >> 8 & 0xFF00 | data.d[2] << 8 & 0xFF0000 | data.d[2] << 24 & 0xFF000000) <<
  82.         uint32_t(data.d[3] >> 24 & 0xFF | data.d[3] >> 8 & 0xFF00 | data.d[3] << 8 & 0xFF0000 | data.d[3] << 24 & 0xFF000000) << std::endl;
  83.     return 0;
  84. }
  85.  
RAW Paste Data