【问题标题】:Encrypt the password in the database加密数据库中的密码
【发布时间】:2023-04-08 23:13:01
【问题描述】:

我正在寻找一种方法来加密数据库中的密码。

我在这里发现了一个与我类似的问题: How do I create and store md5 passwords in mysql

但我不知道盐是什么?我是否需要在用户和密码列旁边的表格中添加,我应该给它什么数据类型?

$escapedName = mysql_real_escape_string($_POST['name']); # use whatever escaping function your db requires this is very important.
$escapedPW = mysql_real_escape_string($_POST['password']);

# generate a random salt to use for this account
$salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));

$saltedPW =  $escapedPW . $salt;
$hashedPW = hash('sha256', $saltedPW);

$query = "insert into user (name, password, salt) values ('$escapedName', '$hashedPW', '$salt'); ";

【问题讨论】:

  • 通常你不存储加密密码,你散列密码。

标签: php mysql hash passwords


【解决方案1】:

但我不知道盐是什么?

The primary function of salts is to defend against dictionary attacks versus a list of password hashes and against pre-computed rainbow table attacks.

我需要在用户和密码列旁边的表格中添加吗

是的,它应该保存在您的users 表中。 因为要检查密码,例如登录时,您需要使用存储在数据库中的盐为该用户使用相同的算法从表单输入中加密键入的密码:

$typedPW = mysql_real_escape_string($_POST['password']); // from form input
$saltedPW =  $typedPW . $salt;  // $salt from db
$typedHashedPW = hash('sha256', $saltedPW); // compare it with db "password" value

并将这个计算得到的字符串与 db 中 password 字段(来自您的代码)中存储的字符串进行比较。

我应该给它什么数据类型

这是一个普通的字符串

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2017-06-12
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多