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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)