【发布时间】:2014-09-25 04:11:08
【问题描述】:
我将用户名和加密密码存储在 mysql 数据库中。仅用于测试,我还将密码以未加密的形式存储在数据库中。
在下面的代码中,我从数据库中获取散列密码和未加密密码。然后我加密未加密的密码。
给定的密码没有通过存储哈希或新哈希的密码验证测试。
存储的密码确实通过了存储哈希和新哈希的密码验证测试。
对 strcmp 的调用表明存储的密码和给定的密码是相等的。
这怎么可能?
[edit] : 我从网页上的用户输入中传入 $password。
// get hashed password from database
$sql = "SELECT member_password FROM member WHERE member_username=:username;";
$stmt = $db->prepare($sql);
$stmt->bindParam("username", $username);
$stmt->execute();
$hash = $stmt->fetch(PDO::FETCH_ASSOC);
$hash = $hash["member_password"];
// get unencrypated password from database
$sql = "SELECT member_unencrypted FROM member WHERE member_username=:username;";
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("username", $username);
$stmt->execute();
$unencrypted = $stmt->fetch(PDO::FETCH_ASSOC);
$unencrypted = $unencrypted["member_unencrypted"];
// encrypt the unencrypted password that was retrieved from the database
$encrypted = password_hash($unencrypted, PASSWORD_DEFAULT);
// given password does not pass the new hash per this test
if(password_verify($password, $encrypted))
echo '<br>given password passed new hash';
else
echo '<br>given password did not pass new hash';
// stored password does pass the new hash per this test
if(password_verify($unencrypted, $encrypted))
echo '<br>stored password passed new hash';
else
echo '<br>stored password did not pass new hash';
// given password does not pass the stored hash per this test.
if(password_verify($password, $hash)){
echo '<br>given password passed stored hash';
else
echo '<br>given password did not pass stored hash';
// stored password does pass the stored hash per this test.
if(password_verify($unencrypted, $hash))
echo '<br>stored password passed stored hash';
else
echo '<br>stored password did not pass stored hash';
// stored and given passwords are equal per this test.
if(strcmp($unencrypted, $password))
echo '<br>stored and given passwords are equal';
else
echo '<br>stored and given passwords are not equal';
输出:
given password did not pass new hash
stored password passed new hash
given password did not pass stored hash
stored password passed stored hash
stored and given passwords are equal
【问题讨论】:
-
用于存储哈希的列的结构是什么?
-
另外,我不确定您是否只是因为测试而这样做,但您可以轻松地组合这两个查询。并且选择一个值时,您应该使用 @987654323 @而不是 @987654324 @之类的东西
-
感谢迈克的提示!已经有一段时间没有做很多sql了,这很有用。哈希值存储在 VARCHAR(255) 中。
-
嗯,那我猜这排除了哈希被截断
标签: php mysql hash passwords password-encryption