I am trying to create random 5-digit number that is not in database as PASSWORD_HASH.
This is an example I have tried to make:
function check($num, $pwdcheck) {
if(password_verify($num, $pwdcheck)) {
return "exist";
} else {
return $num;
}
}
function createLoginPassword() {
/* Passwords from database */$pwds = array('$2y$10$gPenW2YPLzoZOKb/PZ8SC.UFh6C0cLALoO11x/8hjP3GeefMJ6sOu' /*<- Number 1*/, '$2y$10$iOOmAx4kJLNP5H91tfoaz.SarIA1byrUgEE8rtt9llqth5l4v5ACC' /*<- Number 2*/);
foreach ($pwds as $result) {
do {
$output_password = rand(0,4);
} while (check($output_password, $result) == "exist");
$out = $output_password;
}
return $out;
}
echo createLoginPassword();
But it doesn't seem to work, it doesn't generate unique number that is not in array
CodePudding user response:
Try this
function createLoginPassword() {
// Passwords from database
$pwds = [
'$2y$10$gPenW2YPLzoZOKb/PZ8SC.UFh6C0cLALoO11x/8hjP3GeefMJ6sOu' /*<- Number 1*/,
'$2y$10$iOOmAx4kJLNP5H91tfoaz.SarIA1byrUgEE8rtt9llqth5l4v5ACC' /*<- Number 2*/
];
// while password exist, generate new one
do {
// generate 5-digit number
$output = rand(10000, 99999);
} while( !empty(array_filter($pwds, fn($pwd) => password_verify($output, $pwd))) );
return $output;
}
echo createLoginPassword();