微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我刚刚写了这段代码,我现在正在为一个新项目复兴,但它似乎没有用,我不能为我的生活弄清楚为什么它不会验证哈希.

注册一个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()?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐