Javascript Mistakes: UUID generation with Math.random()?

Pubblicato il: 03 maggio 2023
sul canale di: Th30z Code
525
12

Do not use Math.random() to generate UUIDs or any ID.

Using a pseudo-random number generator (PRNG) for generating UUID v4 is a bad idea because PRNGs have a limited period of randomness, which means that over time, the same sequence of values will be generated. This can lead to collisions and undermine the uniqueness of the UUIDs. Additionally, PRNGs may be predictable or susceptible to cryptographic attacks, which can compromise the security of the UUIDs. To ensure the uniqueness and security of UUIDs, a true random number generator should be used instead of a PRNG.

Read more about number generators: https://v8.dev/blog/math-random

The standard Crypto API has methods to generate random UUIDs and cryptographically strong random values:
https://developer.mozilla.org/en-US/d...
https://developer.mozilla.org/en-US/d...

Be aware that libraries published on NPM can have this kind of mistakes too:
See the deprecation warning of https://www.npmjs.com/package/uuid/v/... as an example.

#shorts #javascript #typescript #coding #programming #codingtips


In questa pagina del sito puoi guardare il video online Javascript Mistakes: UUID generation with Math.random()? della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Th30z Code 03 maggio 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 525 volte e gli è piaciuto 12 spettatori. Buona visione!