@@ -31,7 +31,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
3232#include < string>
3333#include < vector>
34-
34+ #include < chrono>
35+ #include < cstdint>
36+ #include < stdexcept>
37+ #include < random>
3538
3639#ifdef _WIN32
3740#define _WIN32_LEAN_AND_MEAN
@@ -65,13 +68,61 @@ namespace AMCCommon {
6568 }
6669#endif // _WASM
6770
71+ inline std::uint64_t getCurrentUnixTimeStamp ()
72+ {
73+ using namespace std ::chrono;
74+ return static_cast <std::uint64_t >(
75+ duration_cast<milliseconds>(system_clock::now ().time_since_epoch ()).count ()
76+ );
77+ }
78+
79+ inline char makeUUIDHexChar (std::uint64_t nHexElement)
80+ {
81+ static constexpr char sHexDigits [] = " 0123456789abcdef" ;
82+
83+ if (nHexElement >= 16 )
84+ throw std::runtime_error (" invalid uuid hex character" );
85+
86+ return sHexDigits [nHexElement];
87+ }
88+
6889 std::string CUtils::createUUID ()
90+ {
91+ std::string sUUID = xg::newGuid ().str ();
92+
93+ if (sUUID .length () != 36 )
94+ throw std::runtime_error (" created invalid uuid String" );
95+
96+ // UUIDv7: first 48 bits (12 hex chars) are unix epoch milliseconds
97+ const std::uint64_t nUnixTimeMs = getCurrentUnixTimeStamp () & 0x0000FFFFFFFFFFFFULL ;
98+
99+ // Patch the first 12 hex digits: positions 0..7 and 9..12 (skip hyphen at position 8)
100+ for (int nHexDigitIndex = 0 ; nHexDigitIndex < 12 ; ++nHexDigitIndex)
101+ {
102+ const std::uint64_t nNibble = (nUnixTimeMs >> ((11 - nHexDigitIndex) * 4 )) & 0x0FULL ;
103+ const int nStringIndex = (nHexDigitIndex < 8 ) ? nHexDigitIndex : (nHexDigitIndex + 1 );
104+
105+ sUUID .at (nStringIndex) = makeUUIDHexChar (nNibble);
106+ }
107+
108+ // Version nibble: xxxxxxxx-xxxx-7xxx-....
109+ sUUID .at (14 ) = makeUUIDHexChar (7 );
110+
111+ // Variant nibble: ....-yxxx-.... must be 8..b (RFC 4122 variant).
112+ sUUID .at (19 ) = " 89ab" [static_cast <unsigned >(std::random_device{}() & 0x3 )];
113+
114+ return normalizeUUIDString (sUUID );
115+ }
116+
117+
118+ std::string CUtils::createUUIDV4 ()
69119 {
70120 auto guid = xg::newGuid ();
121+ std::string sUUID = guid.str ();
122+
71123 return normalizeUUIDString (guid.str ());
72124 }
73125
74-
75126 std::string CUtils::calculateRandomSHA256String (const uint32_t nIterations)
76127 {
77128 if ((nIterations == 0 ) || (nIterations > LIBMC_MAXRANDOMSTRINGITERATIONS ))
0 commit comments