Domanda

What is the best way to generate random (pseudo-unique) ID that will include: 1) small chars 2) cap chars 3) digits with JavaScript (browser and node).

I know the solution with small chars register.

return ("00000" + (Math.random()*Math.pow(36,idLength) << 0).toString(36)).substr(-idLength);

I wonder if there is a like straightforward solution for ItXaK9gw like IDs.

È stato utile?

Soluzione

You seem to already know how to generate a pseudo-random integer, so then all you need to do is base64 encode it.

Be aware though that JavaScript's pseudo-random number generator is not cryptographically strong so an attacker who can run JS in the browser at the same time you're generating your ID may be able to guess with relatively high likelihood the number you generate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top