generateUUIDv4 static method
Generates a version 4 (random) UUID (Universally Unique Identifier).
Implementation
static String generateUUIDv4() {
/// Generate random bytes for the UUIDv4.
final bytes = List<int>.generate(16, (i) {
if (i == 6) {
return (QuickCrypto.generateRandomInt(16) & 0x0f) | 0x40;
} else if (i == 8) {
return (QuickCrypto.generateRandomInt(4) & 0x03) | 0x08;
} else {
return QuickCrypto.generateRandomInt(256);
}
});
/// Set the 6th high-order bit of the 6th byte to indicate version 4.
bytes[6] = (bytes[6] & 0x0f) | 0x40;
/// Set the 7th high-order bit of the 8th byte to indicate variant RFC4122.
bytes[8] = (bytes[8] & 0x3f) | 0x80;
/// Convert bytes to a hexadecimal string with hyphen-separated groups.
final List<String> hexBytes =
bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).toList();
return '${hexBytes.sublist(0, 4).join('')}-${hexBytes.sublist(4, 6).join('')}-'
'${hexBytes.sublist(6, 8).join('')}-${hexBytes.sublist(8, 10).join('')}-'
'${hexBytes.sublist(10).join('')}';
}