Skip to content

Commit

Permalink
use crypto.getRandomValues
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascaro committed Jul 2, 2019
1 parent 2ff293d commit f9a184d
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions uuid/v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ export function validate(id: string): boolean {
}

export default function generate(): string {
// Generate 16 random bytes
// adjust 4 msb of 7th byte to 0100
// set 2 msb of 9th byte to 10
return "00000000-0000-4000-8000-000000000000".replace(
/[0]/g,
(): string =>
// random integer from 0 to 15
Math.floor(Math.random() * 16).toString(16) // as a hex digit.
// random integer from 0 to 15 as a hex digit.
(crypto.getRandomValues(new Uint8Array(1))[0] % 16).toString(16)
);
}

0 comments on commit f9a184d

Please sign in to comment.