我正在开发一个对用户来说非常安全的网站,所以我需要哈希密码.通常我正在使用MD5,但我读到它不再安全了.所以我尝试了PHPass,但后来我读到它也被破解了.所以我尝试password_hash()
了PHP 5.5,但我使用的是HostGator,而PHP则是5.4.此外,我希望能够在不知道它的情况下添加盐(如time() * userid()
),就像在password_hash()
.
散列强度对我来说非常重要,因为我希望100%确定我的用户是安全的.那么有一种非常安全的方式,而不是很快会被黑客攻击的东西吗?
PHP带有内置的哈希算法,例如MD5,SHA1等。但是,从安全角度来看,不建议使用这些函数对密码进行哈希处理,因为使用PasswordPro等工具可以通过蛮力攻击轻松破解密码。
如果您使用盐析作为保护密码的一种方法,那就更好了。下面是一个例子:
$password = 'yourpassword'; $salt = 'randomstr!ng'; $password = md5($salt.$password);
生成盐的更好方法是先将其哈希:
$password = 'yourpassword'; $salt = sha1(md5($password)); $password = md5($password.$salt);
这样做的好处是,盐值是随机的,并且每个密码都会更改,几乎不可能破解。
使用此库可提供与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 */ }