Generate UUID v5

Generate a name-based UUID v5 using SHA-1 hashing of namespace and name.

Code

Utilities
const ns = namespace.replace(/-/g, '').match(/.{2}/g).map(b => parseInt(b, 16));
const data = new Uint8Array([...ns, ...new TextEncoder().encode(name)]);
const hash = Array.from(new Uint8Array(await crypto.subtle.digest('SHA-1', data))).slice(0, 16);
hash[6] = (hash[6] & 0x0f) | 0x50;
hash[8] = (hash[8] & 0x3f) | 0x80;
return hash.map(b => b.toString(16).padStart(2, '0')).join('').replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');

Parameters

Namespace UUID (use DNS, URL, OID, or X500 namespace).

Name to hash within the namespace.

Browser·fetch() may be limited by CORS

What is UUID v5?

UUID v5 is deterministic — the same namespace + name always produces the same UUID. It uses SHA-1 hashing (stronger than v3's MD5).

uuid_v5(DNS_NAMESPACE, "example.com") → always "cfbff0d1-9375-5685-968c-48ce8b15ae17"

Standard Namespaces

NamespaceUUIDUse For
DNS6ba7b810-9dad-11d1-80b4-00c04fd430c8Domain names
URL6ba7b811-9dad-11d1-80b4-00c04fd430c8URLs
OID6ba7b812-9dad-11d1-80b4-00c04fd430c8ISO OIDs
X5006ba7b814-9dad-11d1-80b4-00c04fd430c8X.500 DNs

When to Use UUID v5

Use CaseExample
Idempotent APIsPOST /users with same email returns same user ID
Content-addressable storageFile ID derived from file path
Deterministic test dataSame test input = same IDs across runs
Legacy data migrationConvert old IDs to UUIDs predictably
Distributed systemsGenerate same ID on any node without coordination

Real-World Example

const DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";

// Same domain always gets same UUID - anywhere, anytime
uuid_v5(DNS, "github.com")    // → "a]c34b28-7b5d-5a4e-..."
uuid_v5(DNS, "github.com")    // → "a9c34b28-7b5d-5a4e-..." (identical!)
uuid_v5(DNS, "gitlab.com")    // → "different-uuid-..."

v5 vs v3

Always prefer v5 for new projects. v3 uses MD5 which has known weaknesses. While neither is used for cryptographic security, v5's SHA-1 provides better collision resistance.

Creating Custom Namespaces

For application-specific use cases, create your own namespace:

// Generate once, use forever
const MY_APP_NAMESPACE = crypto.randomUUID(); // Store this!

// Now use it for your domain
uuid_v5(MY_APP_NAMESPACE, "user:123");
uuid_v5(MY_APP_NAMESPACE, "order:456");

More JavaScript Snippets