尝试这个:
$passwordFromDatabase = "A1D292F556AA661B720847487960860F17086A0BD11A4320368E9447FF7139DE089AA88B6159420814F10194F1AA55A3379FB80EA26BA6397BA75CEC811B241A"; // sha512 hash of "somepassword"
$passwordFromForm = $_POST['password']; // $_POST['password'] == "somepassword"
if(password_needs_rehash($passwordFromDatabase, PASSWORD_BCRYPT, ["cost" => 12]) && hash("sha512", $passwordFromForm) === $passwordFromDatabase){
// generate new password hash
$newPasswordHash = password_hash($passwordFromForm, PASSWORD_BCRYPT, ["cost" => 12]);
// update hash from database - replace old hash $passwordFromDatabase with new hash $newPasswordHash
// after update login user
if(password_verify($passwordFromForm, $newPasswordHash)){
// user has logged in successful and hash was updated
// redirect to user area
}else{
// ups something went wrong Exception
}
}else{
if(password_verify($passwordFromForm, $passwordFromDatabase)){
// user password hash from database is already BCRYPTed no need to rehash
// user has logged in successfully
// redirect to user area
}else{
// wrong password
// no access granted - stay where you are
}
}
附言。如果您考虑设置自己的盐...请不要这样做。您不会比本机 password_hash(...) php 函数做得更好。只需设置在检查速度和针对暴力破解的安全性之间提供平衡的成本。如果您将选项留空,则默认情况下成本将设置为 10。