Php 加密数据库中的密码

Php 加密数据库中的密码,php,mysql,hash,passwords,Php,Mysql,Hash,Passwords,我正在寻找一种方法来加密数据库中的密码 我在这里发现了一个与我类似的问题: 但我不知道盐是什么?我是否需要在我的表中用户和密码列旁边添加,以及我应该为其提供什么数据类型 $escapedName = mysql_real_escape_string($_POST['name']); # use whatever escaping function your db requires this is very important. $escapedPW = mysql_real_escape_st

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

我在这里发现了一个与我类似的问题:

但我不知道盐是什么?我是否需要在我的表中用户和密码列旁边添加,以及我应该为其提供什么数据类型

$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'); ";
但我不知道盐是什么

我是否需要在“用户”和“密码”列旁边的表中添加

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

$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
并将此计算字符串与数据库中代码的密码字段中存储的字符串进行比较

我应该给它什么数据类型


它是一个普通字符串

通常不存储加密密码,而是散列密码。