转换为十进制后比较密码



最近我正在开发一个PHP应用程序,并在我的PHP应用程序中比较DB存储的密码(使用sha512生成并存储在C#应用程序中(。

正如我所看到的,我无法使用base64甚至十六进制值来比较密码。但后来我把它们转换成十进制,我可以比较和两个密码(输入和存储(。

下面是我使用的一个示例代码(两者都包含sha512二进制哈希(:

$storedpass = $DBModel->password;
if (hexdec($passwordHash) === hexdec($storedpass)) {
return true;
}

代码以这种方式返回true;

我的问题是,它们的base64值不相同,十六进制不相同,但十进制值相同。这是比较密码的正确方法吗?我在这里有什么弱点吗?

一个正确加盐的密码哈希永远无法进行比较,因为随机加盐会为每次计算产生不同的哈希。只有不安全存储的密码才能进行比较,这是不应该承担的安全风险。

这就是密码散列函数存在的原因,在PHP中,你可以这样做:

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($_POST['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($_POST['password'], $existingHashFromDb);

最新更新