drd99007 2016-03-26 15:09
浏览 101
已采纳

Bcrypt,你如何用随机盐验证?

I got this code off the PHP website. I can make this work without the Salt. But how do you verify with the salt - or does it have to be stored into a variable then you use that later? Not sure how to proceed to the next step to verify. Lots of tutorials on how to make a hash, but to verify is another thing. Thank you.

$options = [
'cost' => 11,
'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."
";


// See the password_hash() example to see where this came from.
$hash = '$2y$11$nJp/w0OC41I0m44T9OQKBuWUrQi63PrJuvDc68KI6oDBdnZK01kiW ';

if (password_verify('rasmuslerdorf', $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
  • 写回答

1条回答 默认 最新

  • dongtan6336 2016-03-26 16:27
    关注

    Note that password_hash() returns the algorithm, cost and salt as part of the returned hash. Therefore, all information that's needed to verify the hash is included in it. This allows the verify function to verify the hash without needing separate storage for the salt or algorithm information.

    Source: http://php.net/manual/en/function.password-verify.php

    just use the function as you did above, it will automatically detect the salt.

    If omitted, a random salt will be generated by password_hash() for each password hashed. This is the intended mode of operation.

    source:http://php.net/manual/en/function.password-hash.php

    Even if you don't add a salt, password_hash will automatically add a random generated one, so you shouldn't have any problem verifying a password that has been salted.

    Also note that:

    The salt option has been deprecated as of PHP 7.0.0. It is now preferred to simply use the salt that is generated by default.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试