3

I have been trying to find Javascript solutions for generating UUIDs in the browser. The ones I have found generate UUIDs following the version 4 standard.

Example:

function uuidv4() {
  return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
    (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  );
}

console.log(uuidv4());

or even

let uuid = self.crypto.randomUUID();
console.log(uuid);

Are the other versions impossible to generate in the browser because they use MAC addresses (and those are inaccessible in JS due to privacy concerns)?

As I understand it, V6 is mostly UUIDv1 with some reordering (Draft), so it's not possible to use either. But how about V7 and V8? Is there an aspect preventing online (client-side) use? Or could the V4 code be adapted to produce those?

oelna
  • 2,210
  • 3
  • 22
  • 40

0 Answers0