我正在使用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创建的散列密码与新的user类创建的密码不同。例如,当我登录时,我在输入的散列pw和数据库中的散列pw上都放入了print_r,结果如下:
$entered_pass = 62b8ce100193434601929323a13a4d95bd3c6535b014e6444516af13f605f36f7
database pass = 62b8ce100193434601929323a153564aaeb4ad75d57b353ee8918cd9829cb5e1b我能想到的唯一一件事是,散列密码在第26个字符处开始偏离,并且$salt看起来有25个字符(假设这是最大长度?)。所有这些都是库存UserCake的东西,所以我不明白为什么它会如此不一致。
我会注意到,如果我复制散列的$entered_pass (那里的第一个)并将其粘贴到数据库中,我将成功登录。
编辑>>>
在进一步研究之后,我认为问题归结为sha1($salt . $plainText);。看起来似乎在第一次$salt之后,事情就开始不同了。另外,当我删除它完美登录的sha1()函数时,我想知道这是否对安全性有任何重大影响。
发布于 2015-02-15 23:32:18
我也有同样的问题。经过一些研究后,我发现使用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");
}我想这就是我要在我的网站上更新的所有内容。如果你有任何错误,请让我知道,我可以尝试和帮助。
https://stackoverflow.com/questions/26198581
复制相似问题