转换为十进制后比较密码

问题描述 投票:0回答:1

最近,我在做一个PHP应用程序,并在我的PHP应用程序中比较DB存储的密码(它是在C#应用程序中使用sha512生成和存储的)。

正如我所看到的,我不能用base64甚至是十六进制来比较密码。但是我把它们转换为十进制,我就可以比较两个密码(输入和存储)。

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

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

代码以这种方式返回true。

我的问题是,它们的base64值不一样,它们的十六进制也不一样,但它们的十进制值是一样的。这是比较密码的正确方法吗?我在这里有什么漏洞吗?

php hash password-hash sha512
1个回答
0
投票

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

这就是为什么密码哈希函数存在的原因,在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);
© www.soinside.com 2019 - 2024. All rights reserved.