sdi_toolBox
uuid.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2026 - SD-Innovation S.A.S. - FRANCE
3*/
4
5/*
6ver: 2.x.x - build: 2026-04-28
7*/
8
9/*
10The zlib License Copyright (c) 2026 SD-Innovation S.A.S. This software is provided ‘as-is’, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
11*/
12
13#pragma once
14
15#include <array>
16#include <chrono>
17#include <format>
18#include <iomanip>
19#include <random>
20#include <sstream>
21#include <string>
22
24{
25//--------------------------------------------------------------
36std::string uniqid(bool moreEntropy = false);
37
47std::string v4();
48
61std::string v7();
62//--------------------------------------------------------------
63
64//--------------------------------------------------------------
65/* Generates a unique identifier based on the current time in
66 * microseconds. If the `moreEntropy` parameter is set to
67 * `true`, additional random data is appended to the identifier
68 * to increase its uniqueness. The generated identifier is
69 * returned as a string. */
70inline std::string uniqid(const bool moreEntropy)
71{
72 const auto now = std::chrono::system_clock::now(); // Get current time point
73 const auto epoch = now.time_since_epoch(); // Get duration since epoch
74 const auto us_since_epoch = std::chrono::duration_cast<std::chrono::microseconds>(epoch).count();
75
76 // PHP format: "%08x%05x" -> 8 hex chars (sec) + 5 hex chars (usec fraction)
77 // sec = full seconds since epoch
78 // usec = microseconds fraction within the current second (0..999999)
79 const auto sec = static_cast<uint32_t>(us_since_epoch / 1'000'000);
80 const auto usec = static_cast<uint32_t>(us_since_epoch % 1'000'000);
81 std::string result = std::format("{:08x}{:05x}", sec, usec);
82
83 // PHP moreEntropy: appends a random float in [0, 10) with 8 decimal places
84 // Example output: "5f68b2056fbac7.12345678"
85 if (moreEntropy)
86 {
87 thread_local auto rng = std::mt19937{ std::random_device{}() };
88 auto f_dist = std::uniform_real_distribution<double>(0.0, 10.0);
89
90 // Append the random data in hexadecimal format
91 result = result + std::format("{:.8f}", f_dist(rng));
92 }
93
94 return result;
95}
96//--------------------------------------------------------------
97/* Generates a random UUID (version 4) and returns it as a
98 * string. The UUID is generated using random numbers, and it
99 * follows the standard format of 8-4-4-4-12 hexadecimal
100 * characters. */
101inline std::string v4()
102{
103 /*
104 * UUID version 4 is a randomly generated UUID.
105 * It's structured as follows:
106 * - 122 bits are random
107 * - 6 bits are fixed to indicate the version and variant
108 * The total is 128 bits, formatted as 8-4-4-4-12 hexadecimal characters.
109 *
110 * The version (4) is set in the 7th byte (index 6) and the variant
111 * is set in the 9th byte (index 8).
112 */
113 std::array<uint8_t, 16> bytes;
114
115 // Fast thread-local RNG seeded from random_device once per thread
116 thread_local auto rng = std::mt19937{ std::random_device{}() };
117 auto dist = std::uniform_int_distribution<uint32_t>(0, 0xFF);
118
119 // Fill the random bytes
120 for (auto &byte : bytes)
121 byte = static_cast<uint8_t>(dist(rng)); // Generate random bytes (8 bits each)
122
123 // Set version to 4 -> xxxx0100 in the 7th byte (index 6)
124 bytes[6] = static_cast<uint8_t>((bytes[6] & 0x0F) | 0x40);
125
126 // Set variant to 10xxxxxx in the 9th byte (index 8)
127 bytes[8] = static_cast<uint8_t>((bytes[8] & 0x3F) | 0x80);
128
129 // Format as 8-4-4-4-12 hex
130 std::ostringstream oss;
131 oss << std::hex << std::nouppercase << std::setfill('0');
132 for (int i = 0; i < 16; ++i)
133 {
134 oss << std::setw(2) << static_cast<int>(bytes[i]);
135 if (i == 3 || i == 5 || i == 7 || i == 9)
136 oss << '-';
137 }
138 return oss.str();
139}
140//--------------------------------------------------------------
141/* Generates a UUID (version 7) based on the current timestamp
142 * and random data. The UUID is generated using a combination of
143 * the current time and random numbers, and it follows the
144 * standard format of 8-4-4-4-12 hexadecimal characters. */
145inline std::string v7()
146{
147 /*
148 * UUID version 7 is a time-ordered UUID that combines a timestamp with random bits.
149 * It's structured as follows:
150 * - 48 bits for the timestamp (milliseconds since Unix epoch)
151 * - 12 bits for the version (7)
152 * - 62 bits for random data (to ensure uniqueness)
153 * The total is 128 bits, formatted as 8-4-4-4-12 hexadecimal characters.
154 *
155 * The version (7) is set in the 7th byte (index 6) and the variant
156 * is set in the 9th byte (index 8).
157 */
158 std::array<uint8_t, 16> bytes;
159
160 // Fast thread-local RNG seeded from random_device once per thread
161 thread_local auto rng = std::mt19937{ std::random_device{}() };
162 auto dist = std::uniform_int_distribution<uint32_t>(0, 0xFF);
163
164 const auto now = std::chrono::system_clock::now(); // Get current time point
165 const auto epoch = now.time_since_epoch(); // Get duration since epoch
166 const uint64_t ms_since_epoch = std::chrono::duration_cast<std::chrono::milliseconds>(epoch).count();
167
168 // Fill timestamp (Big-endian)
169 bytes[0] = static_cast<uint8_t>((ms_since_epoch >> 40) & 0xFF);
170 bytes[1] = static_cast<uint8_t>((ms_since_epoch >> 32) & 0xFF);
171 bytes[2] = static_cast<uint8_t>((ms_since_epoch >> 24) & 0xFF);
172 bytes[3] = static_cast<uint8_t>((ms_since_epoch >> 16) & 0xFF);
173 bytes[4] = static_cast<uint8_t>((ms_since_epoch >> 8) & 0xFF);
174 bytes[5] = static_cast<uint8_t>(ms_since_epoch & 0xFF);
175
176 // Generate randomness for the rest
177 for (size_t i = 6; i < 16; ++i)
178 bytes[i] = static_cast<uint8_t>(dist(rng)); // Generate random bytes (8 bits each)
179
180 // Set Version 7 (bits 48-51 -> 0111)
181 bytes[6] = static_cast<uint8_t>((bytes[6] & 0x0F) | 0x70);
182
183 // Set Variant (bits 64-65 -> 10)
184 bytes[8] = static_cast<uint8_t>((bytes[8] & 0x3F) | 0x80);
185
186 // Format as 8-4-4-4-12 hex
187 std::ostringstream oss;
188 oss << std::hex << std::nouppercase << std::setfill('0');
189 for (int i = 0; i < 16; ++i)
190 {
191 oss << std::setw(2) << static_cast<int>(bytes[i]);
192 if (i == 3 || i == 5 || i == 7 || i == 9)
193 oss << '-';
194 }
195 return oss.str();
196}
197//--------------------------------------------------------------
198} // namespace sdi_toolBox::desktop::utils::uuid
Utility functions for UUID and unique identifier generation.
Definition uuid.h:24
std::string uniqid(bool moreEntropy=false)
Generate a unique identifier based on the current time (microseconds).
Definition uuid.h:70
std::string v4()
Generate a random UUID (version 4).
Definition uuid.h:101
std::string v7()
Generate a time-ordered UUID (version 7).
Definition uuid.h:145