Uuid.nameUuidFromBytes constructor

Uuid.nameUuidFromBytes(
  1. Uuid namespace,
  2. List<int> nameBytes
)

Generates a name-based UUID using SHA-1 hashing (version 5).

Parameters:

  • namespace: Namespace UUID for the name
  • nameBytes: Byte representation of the name

Returns:

  • A deterministic UUID based on the namespace and name

Deterministic Properties

  • Same namespace and name always produce the same UUID
  • Different names in the same namespace produce different UUIDs
  • Uses SHA-1 hashing for cryptographic strength
  • Suitable for creating reproducible UUIDs from names

Example

final namespace = Uuid.NAMESPACE_DNS;
final name = utf8.encode('example.com');
final uuid1 = Uuid.nameUuidFromBytes(namespace, name);
final uuid2 = Uuid.nameUuidFromBytes(namespace, name);
print(uuid1 == uuid2); // true (deterministic)
print(uuid1.version); // 5

Implementation

factory Uuid.nameUuidFromBytes(Uuid namespace, List<int> nameBytes) {
  // Combine namespace UUID bytes with name bytes
  final namespaceBytes = namespace._toBytes();
  final combined = <int>[];
  combined.addAll(namespaceBytes);
  combined.addAll(nameBytes);

  // Generate SHA-1 hash
  final hash = _sha1Hash(combined);

  // Set version (5) and variant (2) bits
  hash[6] = (hash[6] & 0x0F) | 0x50; // Version 5
  hash[8] = (hash[8] & 0x3F) | 0x80; // Variant 2

  return Uuid._fromBytes(hash.take(16).toList());
}