加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:00:11 所属栏目:PHP教程 来源:网络整理
导读:我刚刚写了这段代码,我现在正在为一个新项目复兴,但它似乎没有用,我不能为我的生活弄清楚为什么它不会验证哈希. 当注册第一个passwordEncrypt()函数时,下面运行2个函数. 当尝试登录时,调用checkPassword()函数,而不是登录并回显“是”,它将进入回声“否”的
我刚刚写了这段代码,我现在正在为一个新项目复兴,但它似乎没有用,我不能为我的生活弄清楚为什么它不会验证哈希.

当注册第一个passwordEncrypt()函数时,下面运行2个函数.

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

所以,如果一双新鲜的眼睛可以提前看到许多感谢!

// Encrypt user password
function passwordEncrypt($password) {
    // set the salt
    $salt = substr(md5(time()),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,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.
轻微校正:255是password_hash()手册建议使用的.但是,最好实际使用255,因为手册也建议将来记住,并认为它是“一个好的选择”.

您需要清除行,将列更改为60或更高,然后创建新哈希并再次登录.

$2a$10$023d3086e8462207a1fecueWH4Ub40MWbQJ7F9hapWU3lYxlg3AAa

是60长

脚注:

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

请参阅此堆栈上的Q& A:

> What is the output length of PHP crypt()?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读