1

我创建了一个注册和一个登录脚本,它用完全相同的方式用盐对密码进行哈希处理,但是当用户尝试使用他们的密码登录时,哈希登录密码和存储在数据库中的密码不同,它工作了几天以前,我没有更改登录和注册脚本中的任何内容。

这是存储的凭据

数据库邮箱:jd@gmail.com

数据库密码: addb18f27b6970082727069aa5853116223c5ab46f46a7b07340757804670aef61311ff0254ec45ea78d9ea6d8afb2cefdf3afd6bd4947f6fc558f46703fac1c

以下是用户插入的凭据:

电子邮件:jd@gmail.com

密码:4123363f30664825356a238fe7a568910315e6f6aa8a57d0264844c641e856ab207200f4c75a532b2ebecdbd062bff31da101d973ab0f83eaefd2323a39a4a88

它们使用以下方法进行散列:

$salt   = "salinger";
$hashed = hash_hmac("sha512", $password, $salt);

完整的注册功能(我知道它很乱,但它可以工作(直到现在):

function registerUser($firstname, $surname, $email, $password, $secretQ, $secretA,    $address, $city, $postcode) {
    $flag = array();
    $validEmail = validateEmail($email);
    if (($validEmail) == true) {
         //Do not flag
    } else {
        array_push($flag, 1);
    }
    if ((textOnly("First name", $firstname) == true) || ((textOnly("Surname", $surname)) == true) || ((textOnly("City", $city)) == true)) {
        array_push($flag, 1);
    }
    if ((emptyField($firstname)) || (emptyField($surname)) || (emptyField($email)) || (emptyField($password)) || (emptyField($secretA)) || (emptyField($address)) || (emptyField($city)) || (emptyField($postcode))) {
        array_push($flag, 1);
    }
    if (validPostcode($postcode) == false) {
        array_push($flag, 1);
    }
    if (duplicateEmail($email) == true) {
        array_push($flag, 1);
    }
    if (validatePassword($password) == false) {
        array_push($flag, 1);
    } else {
        $password = validatePassword($password);
    }
    switch ($secretQ) {
        case 1:
            $secretQ = "Your mothers maiden name?";
            break;
        case 2:
            $secretQ = "Name of your first pet?";
            break;
        case 3:
            $secretQ = "The name of your high school?";
            break;
        case 4:
            $secretQ = "Your favourite instrument?";
            break;
    }

    $salt   = "salinger";
    $hashed = hash_hmac("sha512", $password, $salt);

    if (!empty($flag)) {
        echo "There are errors with your registration, go back and ammend it. <br /> <a href=\"register.php\">&lt;&lt; Back</a>";
    } else {
        if ((isset($firstname)) && (isset($surname)) && (isset($email)) && (isset($password)) && (isset($secretQ)) && (isset($secretA)) && (isset($address)) && (isset($city)) && (isset($postcode))) {
            $sql = "INSERT INTO customer (forename, surname, email, password, secretQ, secretA, address_street, address_city, address_postcode, member_type) VALUES ('$firstname', '$surname', '$email', '$hashed', '$secretQ', '$secretA', '$address', '$city', '$postcode', 'User');";
            header("Location: index.php");
        } else {
            array_push($flag, 1);
        }
    }
    $result = mysql_query($sql);
    if (!$result) {
        die(mysql_error());
    }
}

登录功能:

function loginUser($email, $password) {
    if (validateEmail($email) == true) {
        $sql    = "SELECT customerid, forename, email, password, secretA, member_type FROM customer WHERE email = '$email'";

        $result = mysql_query($sql);

        while ($record = mysql_fetch_array($result)) {
            $DBid       = $record['customerid'];
            $DBemail    = $record['email'];
            $DBpassword = $record['password'];
            $DBforename = $record['forename'];
            $DBsecretA  = $record['secretA'];
            $DBmember   = $record['member_type'];
        }

        if (!$result) {
            die(mysql_error());
        }

        $salt   = "salinger";
        $hashed = hash_hmac("sha512", $password, $salt);

        echo "DBEMAIL: $DBemail   DBPASSWORD: $DBpassword <br/>";
        echo "UEMAIL: $email  UPASSWORD: $hashed <br/>";

        if (($email == $DBemail) && ($hashed == $DBpassword)) {
            $match = true;
        } else {
            $match = false;
        }

        if ($match == true) {
            session_start();
            $_SESSION['userid']   = $DBid;
            $_SESSION['Active']   = true;
            $_SESSION['forename'] = $DBforename;
            $_SESSION['type']     = $DBmember;
            header("Location: member.php");
        } else {
            echo "Incorrect credentials.";
        }
    } else {
        echo "Invalid email address!";
    }
return true;
}
4

1 回答 1

1

在 registerUser 中,我会仔细研究一下:

...
if (validatePassword($password) == false) {
    array_push($flag, 1);
} else {
    $password = validatePassword($password);
}
...

$password will be overwritten, it appears, if it is a valid password. If all the passwords are the same in the database, then it's likely that $password is being set to true, and that's the value that's salted. Depending on how you use validatePassword, you may be able to remove the else-clause, leaving this:

...
if (validatePassword($password) == false) {
    array_push($flag, 1);
}
...
于 2012-12-11T17:06:44.790 回答