I am generating passwords with random_int
, but I noticed something weird. Sometimes (purely random), it generates a password less than the minimum value. For example, I set 10
and 15
the limits and once every circa 50-70
tries it pops out a 2-3
character long password. Is there something wrong with my script? It's hard to reproduce the output, just refresh the script till you get a similar result.
<?php
$r_number = random_int( 10 , 15 );
function random_str(
$length,
$keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,./<>?;:"|[]{}-=_+`~!@#$%^&*'
) {
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
if ($max < 1) {
throw new Exception('$keyspace must be at least two characters long');
}
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}
$password = random_str($r_number);
echo $password;
?>
Aucun commentaire:
Enregistrer un commentaire