|
| 1 | +import 'package:uuid/uuid.dart'; |
| 2 | + |
| 3 | +class UuidUtils { |
| 4 | + static String generateV4() { |
| 5 | + return const Uuid().v4(); |
| 6 | + } |
| 7 | + |
| 8 | + static String generateV5(String namespace, String name) { |
| 9 | + return const Uuid().v5(namespace, name); |
| 10 | + } |
| 11 | + |
| 12 | + static String generateV6() { |
| 13 | + return const Uuid().v6(); |
| 14 | + } |
| 15 | + |
| 16 | + static String generateV7() { |
| 17 | + return const Uuid().v7(); |
| 18 | + } |
| 19 | + |
| 20 | + static String generateV8() { |
| 21 | + return const Uuid().v8(); |
| 22 | + } |
| 23 | + |
| 24 | + static String generateV1() { |
| 25 | + return const Uuid().v1(); |
| 26 | + } |
| 27 | + |
| 28 | + /// Decodes a UUID and extracts its version, variant, timestamp, clock sequence, and node. |
| 29 | + /// |
| 30 | + /// - param `String` uuid - The UUID to decode. |
| 31 | + /// - return `Tuple` A tuple containing the version, variant, timestamp, clock sequence, and node. |
| 32 | + static (int, String, int, int, String) decode(String uuid) { |
| 33 | + // Parse the UUID to a list of bytes |
| 34 | + List<int> bytes = Uuid.parse(uuid); |
| 35 | + |
| 36 | + // Extracting information from the UUID |
| 37 | + var version = bytes[6] >> 4; // Version is in the 7th byte |
| 38 | + var variant = (bytes[8] & 0xC0) >> 6; // Variant is in the 9th byte |
| 39 | + |
| 40 | + // Extract timestamp (60 bits: 4 bits from byte 6, all bits from byte 7, 5, 4, 3, 2, and 1) |
| 41 | + var timestamp = ((bytes[6] & 0x0F) << 56) | |
| 42 | + (bytes[7] << 48) | |
| 43 | + (bytes[4] << 40) | |
| 44 | + (bytes[5] << 32) | |
| 45 | + (bytes[0] << 24) | |
| 46 | + (bytes[1] << 16) | |
| 47 | + (bytes[2] << 8) | |
| 48 | + bytes[3]; |
| 49 | + |
| 50 | + // UUID timestamp is in 100-nanosecond intervals since 1582-10-15 |
| 51 | + // Convert to Unix epoch (1970-01-01) |
| 52 | + const UUID_EPOCH = 0x01B21DD213814000; |
| 53 | + timestamp -= UUID_EPOCH; |
| 54 | + var millisecondsSinceEpoch = timestamp ~/ 10000; // Convert to milliseconds |
| 55 | + |
| 56 | + // Extract clock sequence |
| 57 | + var clockSeq = ((bytes[8] & 0x3F) << 8) | bytes[9]; |
| 58 | + |
| 59 | + // Extract node (MAC address) |
| 60 | + var node = [ |
| 61 | + bytes[10].toRadixString(16).padLeft(2, '0'), |
| 62 | + bytes[11].toRadixString(16).padLeft(2, '0'), |
| 63 | + bytes[12].toRadixString(16).padLeft(2, '0'), |
| 64 | + bytes[13].toRadixString(16).padLeft(2, '0'), |
| 65 | + bytes[14].toRadixString(16).padLeft(2, '0'), |
| 66 | + bytes[15].toRadixString(16).padLeft(2, '0') |
| 67 | + ].join(':'); |
| 68 | + |
| 69 | + String variantName = ''; |
| 70 | + |
| 71 | + if (variant == 0) { |
| 72 | + variantName = '0 (NCS backward compatibility)'; |
| 73 | + } else if (variant == 1) { |
| 74 | + variantName = '1 (RFC 4122/DCE 1.1)'; |
| 75 | + } else if (variant == 2) { |
| 76 | + variantName = '2 (Microsoft\'s GUIDs)'; |
| 77 | + } else if (variant == 3) { |
| 78 | + variantName = '3 (Reserved for future use)'; |
| 79 | + } else { |
| 80 | + variantName = 'Unknown'; |
| 81 | + } |
| 82 | + |
| 83 | + return (version, variantName, millisecondsSinceEpoch, clockSeq, node); |
| 84 | + } |
| 85 | +} |
0 commit comments