Uuidv7 NULL character
-
Hello
on Android this method ha created a uuid v7 with NULL values, Claude he advised me to put
quint64 big = qToBigEndian(random);
What you think
Thanks?
PS Source code is uuiv7 on versione Qt6.8QUuid result; std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tp = std::chrono::system_clock::now(); using namespace std::chrono; const nanoseconds nsecSinceEpoch = tp.time_since_epoch(); const auto msecSinceEpoch = floor<milliseconds>(nsecSinceEpoch); const quint64 frac = (nsecSinceEpoch - msecSinceEpoch).count(); // Lower 48 bits of the timestamp const quint64 msecs = quint64(msecSinceEpoch.count()) & 0xffffffffffff; result.data1 = uint(msecs >> 16); result.data2 = ushort(msecs); // rand_a: use a 12-bit sub-millisecond timestamp for additional monotonicity // https://datatracker.ietf.org/doc/html/rfc9562#monotonicity_counters (Method 3) // "frac" is a number between 0 and 999,999, so the lowest 20 bits // should be roughly random. Use the high 12 of those for additional // monotonicity. result.data3 = frac >> 8; result.data3 &= 0x0FFF; result.data3 |= ushort(7) << 12; // rand_b: 62 bits of random data (64 - 2 bits for the variant) const quint64 random = QRandomGenerator::system()->generate64(); quint64 big = qToBigEndian(random); memcpy(result.data4, &big, sizeof(quint64)); result.data4[0] = (result.data4[0] & 0x3F) | 0x80; // UV_DCE return result;
-
What do you think
Not much to do with Qt here. You are using QUuid as a dumb container and QRandomGenerator to obtain a single number. Everything else is bit bashing (I have not checked its correctness).
For a UUID v7 the 48-bits of timestamp should a big endian (i.e. data1 and data2 combined). Endianness of the 74-bit random component seems to me largely irrelevant.
Whatever is converting your binary data to the printable string you posted is broken. If the byte that resulted in "NUL NUL" in the string is indeed zero then it should be generating "00" in the string. Since we cannot see that code we really cannot help with that.
If you were using Qt 6.9+ then QUuid::createUuidV7() would generate a correct UUID v7 value for you.