V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cevincheung
V2EX  ›  PHP

PHP PBKDF2+sha512

  •  
  •   cevincheung · 2015-10-20 08:13:53 +08:00 · 3445 次点击
    这是一个创建于 3112 天前的主题,其中的信息可能已经有所发展或是发生改变。

    并没有什么卵用,都知道该咋用
    我就发着玩 2333333

    <?php
    function password($password,$salt='') {
        if (!$salt)
            $salt = mcrypt_create_iv(16,MCRYPT_DEV_URANDOM);
        $password = hash_pbkdf2("sha512",$password,$salt,10000);
        return sprintf('sha512$10000$%s$%s',base64_encode($salt),$password);
    }
    
    function verify($password,$hash) {
        list($algo,$iterations,$salt,$verify) = explode('$',$hash);
        return explode('$',password($password,base64_decode($salt)))[3] === $verify;
    }
    
    $password = password('password');
    var_dump($password);
    // "sha512$10000$LbcjzVgnLQ/Pg+vPMySCoA==$a89c104f76595bdce5677c8f2f3209207493e8c47abe3dffcfdf37c217c7544dbcca079a36f379d5e495cb73d53a99e86da22132ead0df6d94ef4660af17ba6a
    
    var_dump(verify('password',$password));
    // bool(true)
    

    iterations100000 次也就 0.1s 左右一般

    那么问题来了。不考虑计算成本的前提下,pbkdf2 bcrypt scrypt哪种方法更适合作为生产环境大规模应用的用户密码加密方法?

    第 1 条附言  ·  2015-10-21 13:46:26 +08:00
    PHP BCRYPT

    ```php
    password_hash('password_string', PASSWORD_BCRYPT, ['cost'=>12])
    ```

    PHP SCRYPT
    https://github.com/DomBlack/php-scrypt

    PHP Crypto Library
    https://github.com/vinpel/php-Crypto
    12 条回复    2015-10-20 20:36:47 +08:00
    nevernet
        1
    nevernet  
       2015-10-20 09:02:18 +08:00
    为啥很多都用 hash_hmac 呢?
    GPU
        2
    GPU  
       2015-10-20 09:13:28 +08:00
    看完刚刚的网易贴 。楼下讨论加密的人都高潮 。

    还在思考什么是 pbkdf2
    flyingfz
        3
    flyingfz  
       2015-10-20 09:30:14 +08:00
    easychen
        4
    easychen  
       2015-10-20 09:43:42 +08:00
    既然都用 PHP 了,干嘛不用原生的 API 。 http://php.net/manual/zh/faq.passwords.php
    cevincheung
        5
    cevincheung  
    OP
       2015-10-20 10:04:48 +08:00
    @easychen 自带的是 bcrypt 。 so~ pbkdf2 bcrypt scrypt~~~
    Mrun
        6
    Mrun  
       2015-10-20 11:42:13 +08:00
    @nevernet 因为普通的 hash ( passwd+salt ),会被长度扩展攻击
    windyboy
        7
    windyboy  
       2015-10-20 12:00:59 +08:00
    @flyingfz 感谢,学习了
    kiritoalex
        8
    kiritoalex  
       2015-10-20 12:17:58 +08:00
    建议使用 scrypt
    realpg
        9
    realpg  
       2015-10-20 12:46:17 +08:00
    hook php 源代码的 md5 后使用
    我的 md5 函数输出是 36 位……
    hahasong
        10
    hahasong  
       2015-10-20 13:51:20 +08:00
    @nevernet
    hash_hmac 平时用用就足够了,谷歌两步验证的算法也是用的这个。 python 官方文档也是建议使用此算法加密密码
    cevincheung
        11
    cevincheung  
    OP
       2015-10-20 20:24:39 +08:00
    @kiritoalex 一般 vps 都好慢好慢的。 :doge:
    kiritoalex
        12
    kiritoalex  
       2015-10-20 20:36:47 +08:00
    @cevincheung 慢是借口吧,把用户的安全放在第一位就应该使用 scrypt
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2977 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 09:13 · PVG 17:13 · LAX 02:13 · JFK 05:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.