This small piece of PHP code generates tokens with very good entropy. I would like to know if there is a function in JavaScript that can accomplish the same. Examples are welcome.
Here's the PHP
function getToken($length) { $token = ""; $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $codeAlphabet .= "abcdefghijklmnopqrstuvwxyz"; $codeAlphabet .= "0123456789"; $max = strlen($codeAlphabet); for ($i=0; $i < $length; $i++) { $token .= $codeAlphabet[random_int(0, $max-1)]; } return $token; } echo getToken(24);