Before marking the answer as duplicate, I tried the answer given here and it works. However I want to try with one of the answers given there using openssl_random_pseudo_bytes. This is the code I am using
public static function GenerateRandomPassword($length)
{
$randomBytes = self::GetRandomBytes ($length + 1);
return substr(preg_replace("/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]/", "", base64_encode($randomBytes)),0,$length);
}
public static function GetRandomBytes($nbBytes = 32)
{
$bytes = openssl_random_pseudo_bytes($nbBytes, $strong);
if (false !== $bytes && true === $strong)
{
return $bytes;
}
}
What I am trying to do is create a password that is 8 alphanumeric characters with at least 1 number and 1 special character. I know I am doing something wrong in the code but couldn't figure out what is wrong. How to modify this function to successfully generate a password with my requirements? Any idea? If this is not possible, I would switch to using the accepted answers given in the other question.
Aucun commentaire:
Enregistrer un commentaire