哈希密码的最佳方法是什么?

 智亚康-Scorpio 发布于 2023-02-07 18:26

我正在开发一个对用户来说非常安全的网站,所以我需要哈希密码.通常我正在使用MD5,但我读到它不再安全了.所以我尝试了PHPass,但后来我读到它也被破解了.所以我尝试password_hash()了PHP 5.5,但我使用的是HostGator,而PHP则是5.4.此外,我希望能够在不知道它的情况下添加盐(如time() * userid()),就像在password_hash().

散列强度对我来说非常重要,因为我希望100%确定我的用户是安全的.那么有一种非常安全的方式,而不是很快会被黑客攻击的东西吗?

2 个回答
  • PHP带有内置的哈希算法,例如MD5,SHA1等。但是,从安全角度来看,不建议使用这些函数对密码进行哈希处理,因为使用PasswordPro等工具可以通过蛮力攻击轻松破解密码。

    如果您使用盐析作为保护密码的一种方法,那就更好了。下面是一个例子:

    $password = 'yourpassword';
    $salt = 'randomstr!ng';
    $password = md5($salt.$password);
    

    生成盐的更好方法是先将其哈希:

    $password = 'yourpassword';
    $salt = sha1(md5($password));
    $password = md5($password.$salt);
    

    这样做的好处是,盐值是随机的,并且每个密码都会更改,几乎不可能破解。

    2023-02-07 18:28 回答
  • 使用此库可提供与password_*功能的向前兼容性.

    用法示例:

    require_once("password.php"); // imports the library, assuming it's in the same directory as the current script
    
    $password = "HelloStackOverflow"; // example password
    
    $hash = password_hash($password, PASSWORD_BCRYPT); // here's the hash of the previous password
    
    $hash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 10)); // you can set the "complexity" of the hashing algorithm, it uses more CPU power but it'll be harder to crack, even though the default is already good enough
    
    if (password_verify($password, $hash)) { // checking if a password is valid
        /* Valid */
    } else {
        /* Invalid */
    }
    

    2023-02-07 18:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有