Back to cheatsheets
Cryptography
UUID Generation
Generate RFC 4122 v4 and v7 UUIDs in the browser with crypto.getRandomValues().
What you'll learn
- UUID v4
- UUID v7 (time-ordered)
- crypto.randomUUID() (built-in)
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
- Try the interactive related tool to see the concept in action.
Code Snippets
Copy-paste code snippets
3 ready-to-use snippets for uuid generation. Click copy and paste into your project.
UUID v4
function uuidv4() {
const arr = new Uint8Array(16);
crypto.getRandomValues(arr);
arr[6] = (arr[6] & 0x0f) | 0x40; // version 4
arr[8] = (arr[8] & 0x3f) | 0x80; // variant 1
const h = [...arr].map(b =>
b.toString(16).padStart(2, '0'));
return `${h[0]}${h[1]}${h[2]}${h[3]}-${h[4]}${h[5]}-${h[6]}${h[7]}-${h[8]}${h[9]}-${h[10]}${h[11]}${h[12]}${h[13]}${h[14]}${h[15]}`;
}
UUID v7 (time-ordered)
function uuidv7() {
const arr = new Uint8Array(16);
crypto.getRandomValues(arr);
// First 48 bits = Unix timestamp in ms
const ts = Date.now();
const view = new DataView(arr.buffer);
view.setUint32(0, ts >> 16);
view.setUint16(4, ts & 0xFFFF);
arr[6] = (arr[6] & 0x0f) | 0x70; // version 7
arr[8] = (arr[8] & 0x3f) | 0x80; // variant 1
const h = [...arr].map(b =>
b.toString(16).padStart(2, '0'));
return h.slice(0,4).join('') + '-' +
h.slice(4,6).join('') + '-' +
h.slice(6,8).join('') + '-' +
h.slice(8,10).join('') + '-' +
h.slice(10,16).join('');
}
crypto.randomUUID() (built-in)
// Modern browsers have built-in UUID v4
const id = crypto.randomUUID();
// "550e8400-e29b-41d4-a716-446655440000"
// Bulk generation
const ids = Array.from(
{ length: 1000 },
() => crypto.randomUUID()
);
Tool
Related ShieldX tool
Try this concept hands-on with the interactive tool.
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site