为什么PHP crypt()函数为两个不同的字符串返回相同的东西?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了为什么PHP crypt()函数为两个不同的字符串返回相同的东西?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1323字,纯文字阅读大概需要2分钟。
内容图文
![为什么PHP crypt()函数为两个不同的字符串返回相同的东西?](/upload/InfoBanner/zyjiaocheng/704/b9f330aa71e0473f9d23c0b9dc81f2b0.jpg)
我正在使用PHP的crypt函数进行密码散列/加密,但我不认为我做得对,因为“nathan12”和“nathan123”都允许我在我的系统上登录我的帐户(实际密码是“nathan123” “,因此”nathan12“或其他任何东西都不允许我登录).
这是我的系统在用户注册时所执行的操作:
[...]
$salt = uniqid(mt_rand(), true);
$password = crypt($password, $salt); // '$password' is the inputted password
$insertUserStmt = $mysqli->prepare("INSERT INTO users (name,
username,
password,
password_salt,
email,
created) VALUES (?, ?, ?, ?, ?, ?)");
$insertUserStmt->bind_param("sssssi", $name, $username, $password, $salt, $email, time());
$insertUserStmt->execute();
[...]
它将散列/加密密码($password)与$salt一起插入数据库.
当有人尝试登录时,会执行以下操作来检查用户是否输入了他们输入的用户名的正确密码:
[...]
// $password_salt is from DB; $password is inputted password
$password_crypt = crypt($password, $password_salt);
// $login_password is from DB
if($password_crypt == $login_password) {
[...]
我可能甚至没有正确使用crypt函数,但根据PHP文档,第一个参数是字符串(密码),第二个是salt.
解决方法:
The standard DES-based
crypt()
[…] only uses the first eight characters ofstr
, so longer strings that start with the same eight characters will generate the same result (when the same salt is used).
使用以$< algo> $开头的盐来使用除DES之外的其他内容.有关详细信息,请参阅crypt()文档.
内容总结
以上是互联网集市为您收集整理的为什么PHP crypt()函数为两个不同的字符串返回相同的东西?全部内容,希望文章能够帮你解决为什么PHP crypt()函数为两个不同的字符串返回相同的东西?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。