Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP5.5中使用password_needs_rehash函数_Php_Mysql_Php Password Hash_String Hashing - Fatal编程技术网

如何在PHP5.5中使用password_needs_rehash函数

如何在PHP5.5中使用password_needs_rehash函数,php,mysql,php-password-hash,string-hashing,Php,Mysql,Php Password Hash,String Hashing,我的数据库中有一组密码,我之前使用sha512进行过哈希运算,现在我已经将服务器升级到PHP5.5,我想使用bcrypt密码哈希运算。因此,我的想法是让用户登录,然后调用此密码\u需要\u rehash函数来检查密码,然后更新数据库中的密码哈希: 但是我不知道如何使用这个函数,这里没有列出示例,也没有真正说明选项数组的用途。我是否只需要像这样调用password\u needs\u rehash函数: if (password_needs_rehash ($current_hash, PASS

我的数据库中有一组密码,我之前使用sha512进行过哈希运算,现在我已经将服务器升级到PHP5.5,我想使用bcrypt密码哈希运算。因此,我的想法是让用户登录,然后调用此密码\u需要\u rehash函数来检查密码,然后更新数据库中的密码哈希:

但是我不知道如何使用这个函数,这里没有列出示例,也没有真正说明选项数组的用途。我是否只需要像这样调用password\u needs\u rehash函数:

if (password_needs_rehash ($current_hash, PASSWORD_BCRYPT)) {
  // update the password using password_hash
}

是的,这是总的想法

如果需要重新设置密码,则只需调用以重新设置密码。当然,还要在数据库中保存新的哈希

if (password_needs_rehash ($current_hash, PASSWORD_BCRYPT)) {
  // update the password using password_hash
  $new_hash = password_hash($cleartext_password, PASSWORD_BCRYPT)
  // update the database
  ...
}

是的,没错。您可能希望设置的唯一选项是“cost”,表示生成哈希需要多少工作(因此破解有多困难)。bcrypt的成本默认为10,但可以增加以使哈希更难破解。因此,您可以在这里将“cost”设置为11,并在生成新哈希时使用相同的值。这样做的好处是,您可以稍后将其更改为12,它将升级bcrypt上已有的哈希值,但成本仅为11。

尝试以下方法:

$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
    }
}

另外,如果你想自己做盐。。。请不要那样做。您不会比原生密码\u散列(…)php函数做得更好。只需设置成本,在检查速度和安全性之间提供平衡,以防止暴力。如果将选项留空,则默认情况下成本将设置为10。

从外观上看,是的,这是正确的