Skip to content

Commit e765431

Browse files
committed
✨ uuid v5
1 parent e503619 commit e765431

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/uuid.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,26 @@ export function fromBuffer(buffer: Buffer): string {
6060
hex.slice(20, 32)
6161
);
6262
}
63+
64+
export function v5(name: string, namespace: string): string {
65+
const namespaceBuffer = Buffer.from(namespace.replace(/-/g, ""), "hex");
66+
const nameBuffer = Buffer.from(name, "utf8");
67+
68+
const hash = crypto.createHash("sha1");
69+
hash.update(namespaceBuffer);
70+
hash.update(nameBuffer);
71+
const digest = hash.digest();
72+
73+
// Set version and variant bits for UUID v5
74+
digest[6] = (digest[6] & 0x0f) | 0x50; // Version 5
75+
digest[8] = (digest[8] & 0x3f) | 0x80; // Variant 1
76+
77+
// Format as UUID string
78+
return [
79+
digest.subarray(0, 4).toString("hex"),
80+
digest.subarray(4, 6).toString("hex"),
81+
digest.subarray(6, 8).toString("hex"),
82+
digest.subarray(8, 10).toString("hex"),
83+
digest.subarray(10, 16).toString("hex"),
84+
].join("-");
85+
}

0 commit comments

Comments
 (0)