Skip to content

UUID loses 2 bits of entropy #559

Closed
@dchest

Description

UUIDv4 includes 4 bits as version number and 2 or 3 bits as variant. This code:

export default function generate(): string {
  return "00000000-0000-4000-8000-000000000000".replace(
    /[0]/g,
    (): string =>
      // random integer from 0 to 15 as a hex digit.
      (crypto.getRandomValues(new Uint8Array(1))[0] % 16).toString(16)
  );
}

takes 4 bits for variant instead (8 is hard-coded), so it can generate only 2^120 uuids instead of 2^122.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions