如何在PHP 5.5中使用password_needs_rehash函数

Ash*_*esh 6 php mysql string-hashing php-password-hash

我在我的数据库中有一组密码,我之前使用sha512进行了哈希,现在我已将服务器升级到PHP 5.5,我想使用bcrypt密码哈希.所以我的想法是让用户登录,然后调用此处描述的password_needs_rehash函数来检查密码,然后更新数据库中的密码哈希:

http://php.net/manual/en/function.password-needs-rehash.php

我不知道如何使用这个函数,这里没有列出的例子,它并没有真正说明选项数组的用途.我只需要像这样调用password_needs_rehash函数:

if (password_needs_rehash ($current_hash, PASSWORD_BCRYPT)) {
  // update the password using password_hash
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ton 5

是的,这是一般的想法.

如果需要重新设置密码,那么您只需调用password_hash()rehash密码即可.当然,将新哈希保存在数据库中.

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
  ...
}
Run Code Online (Sandbox Code Playgroud)