为什么哈希密码与数据库中的值不匹配?

时间:2016-06-05 20:59:36

标签: javascript php mysql hash sha256

我有一个注册表单,其代码段如下 -

$email=$_GET['email'];
$pass=$_GET['pass'];
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
        $password = hash('sha256', $pass.$salt);
        for($round = 0; $round < 65536; $round++) 
        { 
            $password = hash('sha256', $password.$salt);
        }
        mysql_query("INSERT INTO user_tbl(email, password, salt) VALUES ('$email','$password','$salt')");

对于字符串后面的特定密码,存储在db -

df22e53c7fb2d599d64597a04fd28ca47bc79675ac50a2381c9a17fd4e07b263

现在我还有一个登录表单。其代码片段如下 -

$email=$_GET['email'];
$pass=$_GET['password'];
$result=mysql_query("SELECT * from user_tbl where email='$email'");
$row=mysql_fetch_array($result);
$salt = $row['salt'];
$password = hash('sha256', $pass.$salt);
for($round = 0; $round < 65536; $round++) 
   { 
       $password = hash('sha256', $password.$salt);
   }
if(strcmp($row['password'],$password)!=0)
{
    echo "wrongpassword";
    exit();
}
else
{
    echo "Success";
}

现在我可以看到登录表单正在评估的哈希密码也是df22e53c7fb2d599d64597a04fd28ca47bc79675ac50a2381c9a17fd4e07b263。这与提交到数据库的注册表格相同。 他们应该匹配。但他们不是。字符串比较测试总是失败。 这两个字段的长度,即密码和盐,每个都是200,并且是VARCHAR类型,这是足够的,我认为因为上面的algo将只生成一个64字符长的字符串。还有什么问题?请帮帮我。

1 个答案:

答案 0 :(得分:0)

您只需要在IF条件下进行简单的修正:

if(strcmp($row['password'],$password)!==0) // change here :)
{
    echo "wrongpassword";
    exit();
}
else
{
    echo "Success";
}

请参阅文档:http://php.net/manual/en/function.strcmp.php

相关问题