UserCake密码哈希/sha1()函数不起作用


UserCake password hashing / sha1() function isnt working

我正在使用UserCake,遇到了一个问题。由于某种原因,generateHash()函数不再持续工作。我看到的是:

funcs.php<--功能所在位置

function generateHash($plainText, $salt = null) {
    if ($salt === null) {
        $salt = substr(md5(uniqid(rand(), true)), 0, 25);
    } else {
        $salt = substr($salt, 0, 25);
    }
    return $salt . sha1($salt . $plainText);
}

class.newuser.php<--其中调用函数以创建密码

//Construct a secure hash for the plain text password
$secure_pass = generateHash($this->clean_password);

login.php<--调用该函数以比较密码

//Hash the password and use the salt from the database to compare the password.
$entered_pass = generateHash($password,$userdetails["password"]);
if($entered_pass != $userdetails["password"]) {
    $errors[] = lang("ACCOUNT_USER_OR_PASS_INVALID");
} else {
    //Passwords match! we're good to go'
}

我可以成功创建一个新帐户。但是当我登录时,login.php创建的哈希密码与新用户类创建的密码不同。例如,当我登录时,我将print_r放在输入的哈希密码和数据库中的哈希密码上,返回的内容如下:

$entered_pass = 62b8ce100193434601929323a13a4d95bd3c6535b014e6444516af13f605f36f7
database pass = 62b8ce100193434601929323a153564aaeb4ad75d57b353ee8918cd9829cb5e1b

我唯一能想到的是,散列密码在第26个字符开始偏离,$salt看起来有25个字符(假设这是最大长度?)。所有这些都是库存UserCake的东西,所以我不明白为什么它如此不一致。

我会注意到,如果我复制散列的$entered_pass(第一个)并将其粘贴到数据库中,我将成功登录。

编辑>>

在看了更多之后,我认为问题归结为sha1($salt . $plainText);。在第一次$salt之后,情况似乎开始有所不同。此外,当我删除它完美登录的sha1()函数时,我只是想知道这是否会对安全性产生重大影响。

我也遇到了同样的问题。经过一番研究,我发现使用password_hash()函数更为先进。

我将class.newuser.php中的$secure_pass变量更改为。。。

        //Construct a secure hash for the plain text password
        $secure_pass = password_hash("$this->clean_password", PASSWORD_DEFAULT);

class.user.php

    //Update a users password
public function updatePassword($pass)
{
    global $mysqli,$db_table_prefix;
    $secure_pass = password_hash("$pass", PASSWORD_DEFAULT);
    $this->hash_pw = $secure_pass;
    $stmt = $mysqli->prepare("UPDATE ".$db_table_prefix."users
        SET
        password = ? 
        WHERE
        id = ?");
    $stmt->bind_param("si", $secure_pass, $this->user_id);
    $stmt->execute();
    $stmt->close(); 
}

login.php

                // Use built in PHP password hashing
            if (!password_verify($password, $userdetails["password"])) {
                // Login Error Attempt Handler
                login_attm_hand();
                //Again, we know the password is at fault here, but lets not give away the combination incase of someone bruteforcing
                $errors[] = lang("ACCOUNT_USER_OR_PASS_INVALID");
            }

我想这就是我必须在我的网站上更新的一切。如果你有任何错误,请告诉我,我可以尽力提供帮助。