doubi5127 2017-08-13 15:24
浏览 84

函数crypt()不返回PHP 7.0中的哈希值

I had something like this (copied from http://www.gregboggs.com/php-blowfish-random-salted-passwords/)

        $Blowfish_Pre = '$2a$05$';
        $Blowfish_End = '$';
        $bcrypt_salt = $Blowfish_Pre . $salt . $Blowfish_End;
        $hashed_password = crypt($password, $bcrypt_salt);

        echo $salt . '<br>';
        echo $password . '<br>';
        echo $bcrypt_salt . '<br>';
        echo $hashed_password . '<br>';
        echo "end";

When I test run it, it turns out that the value of $hashed_password is exactly the same as $bcrypt_salt, while $salt, $password, and $bcrypt_salt all return values as expected. How do I fix this?

  • 写回答

1条回答 默认 最新

  • dongyimeng3764 2017-08-13 21:49
    关注

    Sorry, that was wrong: The "05$" in "pre" is too much. Try without.

    The manual (http://php.net/manual/en/function.crypt.php) says:

    CRYPT_BLOWFISH - Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z".

    Using such a 22-char long salt works:

    echo crypt('secret', '$2a$05$1234567890123456789012$');
    $2a$05$123456789012345678901u.97m5mwuxOR3RvRKYm9sasohx5Mnzwq
    

    Always use a random hash, the above was just an example!

    If you don't need the "2a" version of blowfish, I'd recommend to use the following as it is compatible to crypt() and generates a random salt:

    echo password_hash("secret", PASSWORD_BCRYPT);
    

    P.S.: When reading things like this in the manual I'd tend to recommend not using PHP for anything at all anymore. Can't they just throw an Exception like any other sane API?

    Using characters outside of this range in the salt will cause crypt() to return a zero-length string.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝