如何使用php和mysql将密码安全地存储在数据库中?[复制]

How to store password as secure in database using php and mysql? [duplicate]

提问人:Sathiskumar S 提问时间:5/17/2023 更新时间:5/17/2023 访问量:137

问:

我在以下数据库中将密码以纯文本形式存储

Basic form for getting user entered values

Add the entered values into database

下面显示了上述代码的输出In a database the users tables containing the plain text password

我知道将密码以纯文本形式存储在数据库中是安全威胁。在数据库中存储为安全密码格式的最佳方法是什么?假设我们选择某种算法来保护密码,那么最好的算法是什么最好将密码安全地存储在数据库中?

PHP MySQL 安全 密码

评论

0赞 easleyfixed 5/17/2023
SHA-256 相当不错
0赞 Sathiskumar S 5/17/2023
但我认为使用 sha-256 并不安全,因为使用 rainbow table attack 密码可能是可以猜到的。
1赞 CBroe 5/17/2023
请不要上传代码/数据/错误的图像。
1赞 CBroe 5/17/2023
php.net/manual/en/faq.passwords.php
1赞 Developer 5/17/2023
@SathiskumarS我有详细的答案。试试这些功能

答:

0赞 Developer 5/17/2023 #1

密码哈希

您可以使用 PHP password_hash() 函数通过多种算法对密码进行哈希处理。下面是一个例子:-

$password = "dskjfk111!";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

您可以在第二个参数中提供不同的算法。我使用了使用 bcrypt 算法的PASSWORD_DEFAULT

密码验证

由 password_hash() 函数生成的哈希密码具有算法、盐和成本作为输出的一部分(哈希密码)。因此,通过使用 password_verify() 函数,您可以验证密码,而无需提供哈希期间使用的算法。下面是一个例子:-

$user_input = "dskjfk111!";
$result = password_verify($user_input);
if($result){
  echo "Password Valid"
}else{
  echo "Password Invalid"
}

md5() 或 sha1()

不要使用 md5() 或 sha1() 函数作为密码。正如您可以在 php.net 文档中阅读的那样。