Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 散列密码的最佳方式是什么?_Php_Hash_Password Protection_Password Encryption - Fatal编程技术网

Php 散列密码的最佳方式是什么?

Php 散列密码的最佳方式是什么?,php,hash,password-protection,password-encryption,Php,Hash,Password Protection,Password Encryption,我在一个网站上工作,应该是非常安全的用户,所以我需要哈希密码。通常我用的是MD5,但我读到它不再安全了。所以我试过PHPass,但后来我看到它也被破解了。因此,我尝试了PHP5.5的password\u hash(),但我使用了HostGator,那里的PHP是5.4。此外,我希望能够在不知情的情况下添加盐(如time()*userid()),如password\u hash() 散列强度对我来说非常重要,因为我想100%确保我的用户是安全的。那么,有没有一种方法非常安全,而不是像SHA这样的东

我在一个网站上工作,应该是非常安全的用户,所以我需要哈希密码。通常我用的是MD5,但我读到它不再安全了。所以我试过PHPass,但后来我看到它也被破解了。因此,我尝试了PHP5.5的
password\u hash()
,但我使用了HostGator,那里的PHP是5.4。此外,我希望能够在不知情的情况下添加盐(如
time()*userid()
),如
password\u hash()

散列强度对我来说非常重要,因为我想100%确保我的用户是安全的。那么,有没有一种方法非常安全,而不是像SHA这样的东西很快就会被黑客入侵呢?

看看

你应该考虑使用盐来防止彩虹表攻击。
您可以在这里找到一个教程:

PHP带有内置的哈希算法,如MD5、SHA1等。但是,从安全角度来看,不建议使用这些函数来哈希密码,因为它们很容易通过使用Passwordpro等工具的暴力攻击被破解

如果你使用盐渍作为保护密码的一种方式,效果会更好。以下是一个例子:

$password = 'yourpassword';
$salt = 'randomstr!ng';
$password = md5($salt.$password);
生成盐的更好方法是首先对其进行散列:

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

这样做的好处是salt值是随机的,每个密码都会发生变化,几乎不可能被破解。

我认为最好的办法是使用库来管理密码。
如果您不能使用php5.5,您可以尝试这个适用于php5.3+的库,看看这个项目:

使用它提供了与
密码*
功能的前向兼容性

用法示例:

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 */
}

sha256目前仍然是一个相当可靠的安全单向散列函数,适用于密码存储。看这里:@halfer答案建议他使用PHPass,但正如我在这里写的,它不再安全了。这在2012年很好,但在2014年已经(几乎)…@Vlad:公认的答案包括为还没有5.5版本的用户提供
password\u hash
的库。因此,我认为这个问题仍然是重复的。旁白:通常不建议您使用
密码\u散列添加自己的salt,除非您了解其背后的密码。它会自行添加自己的盐-请参阅PHP手册页。
因此我尝试了PHPass,但随后我阅读了它也已被破解的消息
您使用了任何框架吗?@Christiangiumponi否。它与PHP5.5中的
密码\u hash()
相同,还是较弱?你建议使用什么成本(我在HostGator中托管,所以我猜他们的服务器很强大)?@VladGincher是的,它是一样的。。。我从来没用过,所以我不知道要花多少钱。。。我想您可以使用默认值。