【问题标题】:Trouble validating md5 hashed password with randomly generated salt?无法使用随机生成的盐验证 md5 哈希密码?
【发布时间】:2015-08-22 16:00:35
【问题描述】:

我意识到这并不安全,但我想这样做。

我有这段代码可以根据用户的输入生成密码 ($password)...

$salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
$new_password = md5($salt . $password);
$new_password = $salt . $new_password;

这就是我尝试检查保存的密码的方式:

$split_salt = substr($saved_password, 0, 22);
$incomplete_password = md5($split_salt . $current_password);
$hashed_password = $split_salt . $incomplete_password;

if ($saved_password != $hashed_password) {

    $error = "error";

} else {

     //Validated

}

据我所知,这应该可行。但是,我得到的是错误而不是验证。这是否与 MCRYPT 不能准确生成 22 个字符有关?

【问题讨论】:

  • 我将$password$current_password 都硬编码为相同的值。并将$split_salt = substr($saved_password, 0, 22) 更改为$split_salt = substr($new_password, 0, 22) 并通过验证。您输入的$current _password$saved_password' from database 可能有问题??

标签: php hash md5 salt password-hash


【解决方案1】:

我知道这不是您想听到的,但您的方案非常不安全,而且一个好的解决方案实施起来非常简单,您应该重新考虑:

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);

你的实际问题是盐,mcrypt_create_iv() 将返回一个二进制字符串,它可以很好地包含 \0 字符。因此,无论您的方法是否有效,纯属运气。

【讨论】:

  • 客户端服务器上的 PHP 已经很老了。它不支持密码哈希函数。他存储在这里的数据无论如何都不是很重要
  • 他宁愿使用这种方法也不愿让公司升级服务器。不要问我为什么。这就是为什么我特别说我知道它并不那么安全。什么是IV的替代品?我之前使用过相同的过程,包括 substr(),使用 AES 加密,效果很好。
  • @Allenph - 即使您的网站不存储敏感数据,您也可能危及用户密码,因为他们会在更重要的网站上重复使用密码。关于这个话题有一个很好的cartoon
  • @Allenph - 因为你可以使用参数MCRYPT_DEV_URANDOM,你也应该可以使用compatibility pack。它适用于 PHP 5.3.7 及更高版本,但稍作改动后,它甚至适用于 5.3。
猜你喜欢
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多