我应该从自定义随机生成的方法中添加盐到我的散列密码中吗? PHP

Should I add salt from a custom randomly generated method to my hashed password? PHP

我正在创建一个带数据库的登录系统,想询问有关散列密码的问题。我目前在 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_hash 生成自己的盐值,因此无需提供一个

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

从 PHP 7.0.0 开始,salt 选项已被弃用,正如您在 PHP 的网站上可以找到的那样,有一条警告如下:

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.

这是使用PASSWORD_BCRYPT作为算法时提供的选项。另请注意:

Caution Using the PASSWORD_BCRYPT as the algorithm, will result in the password parameter being truncated to a maximum length of 72 characters.

我可以告诉你的一件事,虽然与安全无关,但你应该检查你的散列的正确 cost,以使其更高效和安全。

以上所有以及更多内容,包括用于检查什么是适合您的服务器性能的脚本 costcan be found here in the documentation

此外,还有一条注意事项:

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.

首先,不要在散列/存储过程中生成包含它的随机字符串

它会在验证期间失败并显示 password_verify(),因为它不知道添加的盐是什么,因为它不是其核心过程的一部分。

My question is whether I need the extra string?

A:没有,我已经在上面说过了。

为什么?首先不行,不需要。

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 注入。


Keep in mind that I want this to be as secure as possible.

在PHP 7.2.0 中引入了一些你可以使用的东西,那就是Argon2。

有关这方面的更多信息,请参阅以下内容:

它没有那么安全。

如果您担心安全性并想添加哈希,您需要手动创建一个并将其添加到您的哈希密码的两端,然后在检查数据库时将其包含在您的注册和登录中