dsideal2015 2014-10-08 10:54
浏览 41
已采纳

使用password_hash [duplicate]验证密码是否已经哈希和盐渍

This question already has an answer here:

Let's hash and salt a password with:

<?php
$hash = password_hash('bonjour', PASSWORD_BCRYPT,  ['cost' => 12, ]);
// no salt option mentionned 
// then salt will be generated randomly, see password_hash documentation     
echo $hash;
?>

The result changes on each reload of the page, that's normal : the salt is randomly generated, it can be :

$2y$12$FlxBBjTjelKkGY.SJarlL.THUZBwcl7M6V35DmZmTmYJZRwhpRkIW
$2y$12$p2pkD116hBHNc/2nyQ2WyOkrn.h8xvWvM1.Lmvsnhms2Y6zsb.j1e
$2y$12$u4ipdQQM926jfanpXnwtkupv2CH/uWoPvK563tG7p.z35GcOBOZdS
etc.

In the previous code, the salt seems to be stored nowhere (am I wrong ?). But at the end password_verify is able to check the password with the hash, and it works with every result, regardless what the salt was.

<?php
echo password_verify('bonjour', '$2y$12$FlxBBjTjelKkGY.SJarlL.THUZBwcl7M6V35DmZmTmYJZRwhpRkIW') ? 'yes' : 'no';
// yes
echo password_verify('bonjour', '$2y$12$p2pkD116hBHNc/2nyQ2WyOkrn.h8xvWvM1.Lmvsnhms2Y6zsb.j1e') ? 'yes' : 'no';
// yes
?>     

Is it normal that password_verify is able to check the password without having to store the salt somewhere?

</div>
  • 写回答

1条回答 默认 最新

  • dongzhuzhou4504 2014-10-08 10:57
    关注

    From the PHP.net manual

    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.

    The salt is encoded in the hash, basically.

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

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?