使用PBKDF2在MySQL数据库中存储密码和salt


Storing password and salt in MySQL DB using PBKDF2

我一直在尝试使用pbkdf2使我的用户密码真正安全。

密码散列可以很好地进入数据库,但salt则不然。

salt似乎包含mysql列不喜欢的外来字符。

我的"users"表中的所有列都是UTF8_unicode_ci。

这是我的密码散列:

$size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB);
$salt = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
$passHash = pbkdf2('SHA512', $pass, $salt, 8192, 256) ;
include("dbconnect.php") ;
$result = $dbh->prepare("INSERT INTO users (name, email, qq, password, salt)VALUES(?, ?, ?, ?, ?)") ;
    $result->bindParam(1, $name, PDO::PARAM_STR) ;
    $result->bindParam(2, $email, PDO::PARAM_STR) ;
    $result->bindParam(3, $qq, PDO::PARAM_STR) ;
    $result->bindParam(4, $passHash, PDO::PARAM_STR) ;
    $result->bindParam(5, $salt, PDO::PARAM_STR) ;
$result->execute() ;

和pbkdf2:

/*
 * PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
 * $algorithm - The hash algorithm to use. Recommended: SHA256
 * $password - The password.
 * $salt - A salt that is unique to the password.
* $count - Iteration count. Higher is better, but slower. Recommended: At least 1000.
* $key_length - The length of the derived key in bytes.
* $raw_output - If true, the key is returned in raw binary format. Hex encoded otherwise.
* Returns: A $key_length-byte key derived from the password and salt.
*
* Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
*
* This implementation of PBKDF2 was originally created by https://defuse.ca
* With improvements by http://www.variations-of-shadow.com
*/
function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false){
$algorithm = strtolower($algorithm);
if(!in_array($algorithm, hash_algos(), true))
    die('PBKDF2 ERROR: Invalid hash algorithm.');
if($count <= 0 || $key_length <= 0)
    die('PBKDF2 ERROR: Invalid parameters.');
$hash_length = strlen(hash($algorithm, "", true));
$block_count = ceil($key_length / $hash_length);
$output = "";
for($i = 1; $i <= $block_count; $i++) {
    // $i encoded as 4 bytes, big endian.
    $last = $salt . pack("N", $i);
    // first iteration
    $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
    // perform the other $count - 1 iterations
    for ($j = 1; $j < $count; $j++) {
        $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
    }
    $output .= $xorsum;
}
if($raw_output)
    return substr($output, 0, $key_length);
else
    return bin2hex(substr($output, 0, $key_length));
}

此外,我刚刚注意到,它为相同的密码存储了完全不同的哈希。

我这样做对吗?

对于"我做得对吗?"这样宽泛的问题,很难给出确切的答案我可以说,在散列之前对密码进行盐析的全部目的是为了使生成的散列在用户之间是唯一的。因此,对于相同的输入获得不同的输出是一件好事。有关原因的更多信息,请查阅"字典攻击"。

至于您的代码,听起来您真正想知道的是为什么您的salt没有存储到数据库中。调试步骤我能想到,没有更多的具体细节

  • $salt可能为false,mcrypt_create_iv在出现错误时返回false(不太可能,因为您上面提到的哈希输出差异,但值得检查
  • 输出字符无法识别为您所怀疑的字符。在添加到prepare之前,您可以尝试将数据库列类型转换为varbinary,并使用字符串转换为二进制或十六进制解码器
  • 试着用不同字符编码的列类型进行实验,看看你的盐可以变成什么列类型。UTF-8为每个字符使用可变数量的字节,这让我在处理绝对的事情时感到不舒服。salt和hash通常被认为是用位字段固定的,为了方便起见,通常用十六进制格式表示

如果你提供了你的服务器环境、php版本、mysql版本等,以及一些没有正确存储的盐样本,我可能会缩小问题的范围。

在存储到varchar列之前,应该将结果转换为base64编码。Base64编码基本上将字节数组转换为ASCII范围内的可显示(因此也可存储SQL)字符。