我正在创建一个带有数据库的登录系统,并想询问有关哈希密码的问题。我目前在 PHP 中使用函数password_hash(),此外我还添加了一个 20 个字符的自定义随机字符串。看起来像这样:
$salt = generateRandomString();
$hashedPwd = password_hash($pwd + $salt, PASSWORD_DEFAULT);
函数:
function generateRandomString($length = 20) {
$characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+{}|[]/?~`';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
稍后我还将随机字符串发送到数据库,并在登录时与密码一起验证。
我的问题是我是否需要额外的字符串?请记住,我希望它尽可能安全。
最佳答案
首先,不要生成随机字符串并在哈希/存储过程中包含它。
它将失败并显示 password_verify()
在验证过程中,因为它不知道添加的盐是什么,因为它不是其核心过程的一部分。
My question is whether I need the extra string?
答:不,我已经在上面说过了。
为什么?首先它不起作用,而且也不需要它。
password_hash()
生成自己的。
如果您确实想向其中添加自己的盐,请删除您正在使用的添加到哈希中的方法。手册中列出了。
不过你应该小心,因为,我引用:
"Warning 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."
注意:您应该注意手册注释中的警告:
Caution
It is strongly recommended that you do not generate your own salt for this function. It will create a secure salt automatically for you if you do not specify one.
As noted above, providing the salt option in PHP 7.0 will generate a deprecation warning. Support for providing a salt manually may be removed in a future PHP release.
因为你在这方面似乎是新手;如果您尚未使用准备好的语句来存储和检索数据,那么我强烈建议您考虑使用它们。
这将有助于防止可能的 SQL 注入(inject)。
<小时/>Keep in mind that I want this to be as secure as possible.
有一个东西你可以使用,它是 PHP 7.2.0 中引入的,那就是 Argon2。
有关此内容的更多信息,请参阅以下内容:
- https://www.argon2.com/
- https://en.wikipedia.org/wiki/Argon2
- Password hashing security of argon2 versus bcrypt/PBKDF2?
- https://secure.php.net/manual/en/function.password-hash.php
它并没有那么安全。
关于php - 我应该将自定义随机生成方法中的盐添加到我的哈希密码中吗? PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48480987/