Uuid class
A powerful, secure implementation of Universally Unique Identifiers (UUIDs) following RFC 4122 specifications.
The Uuid class provides comprehensive support for UUID generation, parsing, validation, and manipulation with cryptographically secure random number generation and multiple UUID versions.
Supported UUID Versions
- Version 1: Time-based UUIDs with MAC address
- Version 3: Name-based UUIDs using MD5 hashing
- Version 4: Random or pseudo-random UUIDs (most common)
- Version 5: Name-based UUIDs using SHA-1 hashing
Security Features
- Cryptographically secure random number generation
- Proper entropy collection for version 4 UUIDs
- Secure hashing for name-based UUIDs
- Protection against timing attacks
- Validation of UUID format and version compliance
Performance Characteristics
- Generation: O(1) for version 4, O(log n) for name-based versions
- Parsing: O(1) with input validation
- Comparison: O(1) lexicographic comparison
- Memory: 16 bytes storage + minimal overhead
Example Usage
// Generate random UUID (version 4)
final uuid1 = Uuid.randomUuid();
print(uuid1); // e.g., "550e8400-e29b-41d4-a716-446655440000"
// Parse from string
final uuid2 = Uuid.fromString('550e8400-e29b-41d4-a716-446655440000');
// Generate name-based UUID (version 5)
final namespace = Uuid.NAMESPACE_DNS;
final uuid3 = Uuid.nameUuidFromBytes(namespace, utf8.encode('example.com'));
// Generate time-based UUID (version 1)
final uuid4 = Uuid.timeBasedUuid();
// Comparison and validation
print(uuid1 == uuid2); // false
print(uuid1.compareTo(uuid2)); // -1, 0, or 1
print(Uuid.isValidUuid('invalid')); // false
// Access UUID components
print(uuid1.version); // 4
print(uuid1.variant); // 2 (RFC 4122)
print(uuid1.mostSignificantBits);
print(uuid1.leastSignificantBits);
Thread Safety
All UUID operations are thread-safe. The internal random number generator uses secure system entropy and can be safely called from multiple threads concurrently.
Format Compliance
Generated UUIDs strictly follow RFC 4122 format:
- 8-4-4-4-12 hexadecimal digit groups separated by hyphens
- Proper version and variant bits set according to specification
- Case-insensitive parsing with canonical lowercase output
- Implemented types
Constructors
- Uuid.fromBits(int mostSigBits, int leastSigBits)
-
Creates a UUID from its most and least significant bits.
factory
- Uuid.fromString(String uuidString)
-
Creates a UUID from a string representation.
factory
-
Uuid.nameUuidFromBytes(Uuid namespace, List<
int> nameBytes) -
Generates a name-based UUID using SHA-1 hashing (version 5).
factory
- Uuid.nameUuidFromString(Uuid namespace, String name)
-
Generates a name-based UUID from a string name.
factory
- Uuid.randomUuid()
-
Generates a random UUID (version 4).
factory
- Uuid.timeBasedUuid()
-
Generates a time-based UUID (version 1).
factory
Properties
- clockSequence → int
-
Gets the clock sequence from a time-based UUID (version 1).
no setter
- hashCode → int
-
The hash code for this object.
no setteroverride
- leastSignificantBits → int
-
Gets the least significant 64 bits of the UUID.
no setter
- mostSignificantBits → int
-
Gets the most significant 64 bits of the UUID.
no setter
- node → BigInteger
-
Gets the node identifier from a time-based UUID (version 1).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- timestamp → int
-
Gets the timestamp from a time-based UUID (version 1).
no setter
- variant → int
-
Gets the variant number of the UUID.
no setter
- version → int
-
Gets the version number of the UUID.
no setter
Methods
-
compareTo(
Uuid other) → int -
Compares this object to another object.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toBytes(
) → Uint8List - Converts the UUID to a byte array.
-
toCompactString(
) → String - Converts the UUID to a compact string without hyphens.
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override
Static Properties
- NAMESPACE_DNS → Uuid
-
Predefined namespace UUID for DNS names (RFC 4122)
final
- NAMESPACE_OID → Uuid
-
Predefined namespace UUID for ISO OID names (RFC 4122)
final
- NAMESPACE_URL → Uuid
-
Predefined namespace UUID for URL names (RFC 4122)
final
- NAMESPACE_X500 → Uuid
-
Predefined namespace UUID for X.500 DN names (RFC 4122)
final
Static Methods
-
isValidUuid(
String uuidString) → bool - Validates if a string is a valid UUID format.
-
setUuidRangeBuilder(
UuidRangeBuilder rangeBuilder) → void - Sets the random number generator for UUID generation.