复位后为什么散列不同?

时间:2011-02-08 17:43:37

标签: php html encryption passwords

我已经创建了一个整洁的系统来对用户密码进行加密和哈希处理,如果他们忘记密码就会向他们发送电子邮件以提示重置。

我可以动态哈希$_POST并将其与存储在其行中的用户唯一salt一起加盐,并将其与存储的哈希密码相匹配并对其进行签名。当他们重置密码并尝试时要重新登录,他们输入的$ _POST与存储的pw不匹配。这是完全相同的过程。

知道为什么会这样吗?

以下是该剧本的相关部分:

$query =  "SELECT `encrypted_password`,`salt` FROM `Users` WHERE `Email` = '" . stripslashes(mysql_real_escape_string($_POST['email'])) . "'";
    $request = mysql_query($query,$connection) or die(mysql_error());
    $result = mysql_fetch_array($request);


    $salty_password = sha1($result['salt'] . stripslashes(mysql_real_escape_string($_POST['password'])));

    // SEE HOW THEY COMPARE
    echo "Users real salted pass: " . $result['encrypted_password'] . " / Salty Password to check: " . $salty_password . "<br />";

    $query2 = "SELECT * FROM `Users` WHERE `Email` = '". stripslashes(mysql_real_escape_string($_POST['email'])."' AND `encrypted_password` = '$salty_password'";
    $request2 = mysql_query($query2,$connection) or die(mysql_error());
    $result = mysql_fetch_array($request2);

- 编辑---

可能有助于查看密码的重置方式?

$query = "SELECT * FROM `Password_Reset` ORDER BY `id` DESC LIMIT 1";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);

$token = $result['token'];

$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";
$rand = str_shuffle($alpha);
$salt = substr($rand,0,40);
$hashed_password = sha1($salt . stripslashes(mysql_real_escape_string($_POST['Password'])));
$user_email = $result['email'];

    if(isset($_POST['sub_settings'])){


        if(empty($_POST['Password'])) {
            $valid = false;
            $error_msgs[] = 'Whoops! You must enter a password.';
        }

        if($_POST['Password'] != $_POST['passwordConfirm'] || empty($_POST['Password'])) {
            $valid = false;
            $error_msgs[] = "Your password entries didn't match...was there a typo?";
        }

        if($valid) {
            $query = "UPDATE `Users` SET `encrypted_password` = '$hashed_password' WHERE `Email` = '$user_email'";

            mysql_query($query,$connection);

1 个答案:

答案 0 :(得分:2)

重置密码时,您似乎会生成一个随机的新盐,但不会将其存储在数据库中。密码检查然后使用前一个盐。


此外,在将密码输入sha1之前,无需对密码进行清理。实际上,使用stripslashesmysql_real_escape_string可能会导致问题,因为这两个函数都可能根据PHP版本和配置对密码进行不同的转换,从而产生不同的散列。