UUID Version 5 (SHA-1 Name-Based)
Learn about UUID v5: deterministic UUIDs generated from a namespace and name using SHA-1 hashing. Same input always produces the same UUID output.
Detailed Explanation
UUID Version 5 generates deterministic identifiers by hashing a namespace UUID and a name string using SHA-1. Given the same namespace and name, it always produces the same UUID, making it ideal for creating reproducible identifiers from existing data.
Algorithm:
- Concatenate the 16 bytes of the namespace UUID with the UTF-8 bytes of the name
- Compute the SHA-1 hash (20 bytes output)
- Take the first 16 bytes of the hash
- Set the version bits (bits 48-51) to
0101(5) - Set the variant bits (bits 64-65) to
10
Predefined namespaces (from RFC 4122):
6ba7b810-9dad-11d1-80b4-00c04fd430c8— DNS6ba7b811-9dad-11d1-80b4-00c04fd430c8— URL6ba7b812-9dad-11d1-80b4-00c04fd430c8— OID (ISO Object Identifier)6ba7b814-9dad-11d1-80b4-00c04fd430c8— X.500 DN
For example, uuid5(DNS, "example.com") always produces cfbff0d1-9375-5685-968c-48ce8b15ae17.
When to use v5 over v4: Use v5 when you need the same input to always map to the same UUID. This is useful for deduplication, idempotent operations, and creating stable identifiers from natural keys. Use v4 when you need globally unique IDs with no relation to input data.
v5 vs v3: UUID v3 uses MD5 instead of SHA-1. RFC 9562 recommends v5 over v3 because SHA-1 provides better hash distribution and stronger collision resistance than MD5. However, neither v3 nor v5 should be used for cryptographic security since only 122 bits of the hash output are retained.
Code examples:
import uuid
# Python has built-in v5 support
id = uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")
# cfbff0d1-9375-5685-968c-48ce8b15ae17
// Node.js with the 'uuid' package
import { v5 as uuidv5 } from 'uuid';
const id = uuidv5('example.com', uuidv5.DNS);
Storage note: Because v5 UUIDs are deterministic, you can recompute them instead of storing them. This can save storage in systems where the namespace and name are already known.
Use Case
UUID v5 is commonly used to generate stable identifiers for URL-based resources, configuration keys, or content-addressable storage where the same logical entity should always map to the same UUID.