gpt4 book ai didi

php - Blowfish 加密 - 哈希已创建但不会验证

转载 作者:可可西里 更新时间:2023-10-31 23:17:27 25 4
gpt4 key购买 nike

我不久前写了这段代码,现在我正在为一个新项目恢复它,但它似乎不起作用,我无法弄清楚为什么它不会验证哈希。

当注册第一个 passwordEncrypt() 函数时,运行的 2 个函数如下所示。

当尝试登录时调用 checkPassword() 函数,而不是登录并回显"is",而是进入回显“否”的部分。

所以,如果有新的眼睛可以看看,请提前多谢!

// Encrypt user password
function passwordEncrypt($password) {
// set the salt
$salt = substr(md5(time()), 0, 22);

// encrypt using blowfish with a load of 10
$password = crypt($password, '$2a$10$' . $salt);

// return the encrypted hash
return $password;
}

/*
Check password function when logging in
first we select the password from the supplied username from the database
// get the row and set the hash to the currect password from the database
//run the salts etc and check to see if the passwords match
*/
function checkPassword($userName, $password, $db){
$sql = 'SELECT password FROM users WHERE userName = :userName';
$stmt = $db->prepare($sql);
$stmt->bindValue(':userName', $userName, PDO::PARAM_STR);
$stmt->execute();

$numRows = $stmt->rowCount();

if ($numRows > 0) {
$row = $stmt->fetch();
$hash = $row['password'];

// run the hash function on $password 
$fullSalt = substr($hash, 0, 29);
$new_hash = crypt($password, $fullSalt);

// Check that the password matches
if($hash == $new_hash) {
echo 'yes';
exit;
return true;
} else {
echo 'no';
exit;
return false;
}
} else {
echo 'way';
exit;
return false;
}
}

我已经注册了一个密码,然后尝试了它,这就是它返回的内容

密码:$2a$10$023d3086e8462207a1fecueWH4Ub40MWbQJ7F9输入:$2a$10$023d3086e8462207a1fecueWH4Ub40MWbQJ7F9hapWU3lYxlg3AAa没有

所以它在 hapWU3lYxlg3AAa 上添加

最佳答案

"column length is what? 40? 50? 60? other? $2a$10$023d3086e8462207a1fecueWH4Ub40MWbQJ7F9 implies being too short. – Fred -ii-"

"ah 45 in the database – Tom C"

给你。列的长度太短,需要为 60。

手册建议255。
稍作更正: password_hash() 上的手册建议使用 255。但是,最好实际使用 255,因为手册还建议将来记住并认为它是“一个不错的选择”

您需要清除您的行,将您的列更改为 60 或更大,然后创建一个新的散列并再次登录。

$2a$10$023d3086e8462207a1fecueWH4Ub40MWbQJ7F9hapWU3lYxlg3AAa

长60


脚注:

据说有些人发现很难使用 crypt(),使用 password_hash() 或兼容包(如果 PHP < 5.5)https://github.com/ircmaxell/password_compat/ 是实际上更容易。 选择权在你

另请参阅 Stack 上的此问答:

关于php - Blowfish 加密 - 哈希已创建但不会验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com