【问题标题】:Blowfish encryption - hash is created but will not verifyBlowfish 加密 - 已创建哈希但不会验证
【发布时间】:2016-03-31 21:45:20
【问题描述】:

我不久前编写了这段代码,现在我正在为一个新项目重新启动它,但它似乎不起作用,我终其一生都无法弄清楚为什么它不会验证哈希值。

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

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

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

// 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

【问题讨论】:

  • 密码列的长度和哈希字符串的长度是多少?
  • 顺便说一句,你的退出抵消了你的回报。
  • 如果您知道如何 ping 某人,很好。您的问题没有资格获得神奇的答案。
  • 你真的不应该在密码哈希上使用你自己的盐,你真的应该使用 PHP 的 built-in functions 来处理密码安全。如果您使用的 PHP 版本低于 5.5,则可以使用 password_hash() compatibility pack
  • 它为您执行随机加盐,并且需要更少的代码。

标签: php login blowfish


【解决方案1】:

“列长是什么?40?50?60?其他?$2a$10$023d3086e8462207a1fecueWH4Ub40MWbQJ7F9 意味着太短了。-Fred -ii-”

“啊 45 在数据库中 – Tom C”

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

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

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

$2a$10$023d3086e8462207a1fecueWH4Ub40MWbQJ7F9hapWU3lYxlg3AAa

60 长


脚注:

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

另请参阅 Stack 上的问答:

【讨论】:

  • 很棒的伙伴,谢谢你,它所需要的只是一双新鲜的眼睛。重建数据库时没有考虑字段长度。再次感谢
  • @TomC 不客气,汤姆。当我看到你的代码并且看起来合法时,我的 Spidey 感觉 对专栏的长度感到刺痛。 干杯
  • 哈哈检查列长度已直接进入我的清单以备将来使用,你的蜘蛛侠意识是正确的!
  • @TomC 必须始终倾听他们的小声音 ;-) 顺便说一句,如果您想重新加载它,我已经对我的答案进行了编辑。我添加了一些您可能会感兴趣的附加信息。 干杯
  • 感谢 Fred :) 如果我能投票更多,我将不胜感激:D
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
相关资源
最近更新 更多