Password Hashing 函数
在线手册:中文  英文

password_hash

(PHP 5 >= 5.5.0)

password_hashCreates a password hash

说明

string password_hash ( string $password , integer $algo [, array $options ] )

password_hash() creates a new password hash using a strong one-way hashing algorithm.

The following algorithms are currently supported:

参数

password

用户的密码。

algo

一个用来在散列密码时指示算法的密码算法常量

options

一个包含有选项的关联数组。目前支持两个选项:salt,在散列密码时加的盐(干扰字符串),以及cost,用来指明算法递归的层数。这两个值的例子可在 crypt() 页面找到。

If omitted, a random salt will be created and the default cost will be used.

返回值

Returns the hashed password, 或者在失败时返回 FALSE.

范例

Example #1 password_hash() example

<?php
/**
 * We just want to hash our password using the current DEFAULT algorithm.
 * This is presently BCRYPT, and will produce a 60 character result.
 *
 * Beware that DEFAULT may change over time, so you would want to prepare
 * By allowing your storage to expand past 60 characters (255 would be good)
 */
echo password_hash("rasmuslerdorf"PASSWORD_DEFAULT)."\n";
?>

以上例程会输出:

$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a

Example #2 password_hash() example setting cost manually

<?php
/**
 * In this case, we want to increase the default cost for BCRYPT to 12.
 * Note that we also switched to BCRYPT, which will always be 60 characters.
 */
$options = [
    
'cost' => 12,
];
echo 
password_hash("rasmuslerdorf"PASSWORD_BCRYPT$options)."\n";
?>

以上例程会输出:

$2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K

Example #3 password_hash() example setting salt manually

<?php
/**
 * Note that the salt here is randomly generated.
 * Never use a static salt or one that is not randomly generated.
 *
 * For the VAST majority of use-cases, let password_hash generate the salt randomly for you
 */
$options = [
    
'cost' => 11,
    
'salt' => mcrypt_create_iv(22MCRYPT_DEV_URANDOM),
];
echo 
password_hash("rasmuslerdorf"PASSWORD_BCRYPT$options)."\n";
?>

以上例程会输出:

$2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.

Example #4 password_hash() example finding a good cost

<?php
/**
 * This code will benchmark your server to determine how high of a cost you can
 * afford. You want to set the highest cost that you can without slowing down
 * you server too much. 10 is a good baseline, and more is good if your servers
 * are fast enough.
 */
$timeTarget 0.2

$cost 9;
do {
    
$cost++;
    
$start microtime(true);
    
password_hash("test"PASSWORD_BCRYPT, ["cost" => $cost]);
    
$end microtime(true);
} while ((
$end $start) < $timeTarget);

echo 
"Appropriate Cost Found: " $cost "\n";
?>

以上例程会输出:

Appropriate Cost Found: 11

注释

Caution

It is strongly recommended that you do not generate your own salt for this function. It will create a secure salt automatically for you if you do not specify one.

Note:

It is recommended that you should test this function on your servers, and adjust the cost parameter so that execution of the function takes approximately 0.1 to 0.5 seconds. The script in the above example will help you choose a good cost value for your hardware.

Note: Updates to supported algorithms by this function (or changes to the default one) must follow the follwoing rules:

  • Any new algorithm must be in core for at least 1 full release of PHP prior to becoming default. So if, for example, a new algorithm is added in 5.5.5, it would not be eligible for default until 5.7 (since 5.6 would be the first full release). But if a different algorithm was added in 5.6.0, it would also be eligible for default at 5.7.0.
  • The default should only change on a full release (5.6.0, 6.0.0, etc) and not on a revision release. The only exception to this is in an emergency when a critical security flaw is found in the current default.

参见


Password Hashing 函数
在线手册:中文  英文

用户评论:

chris at acsdi dot com (2013-06-21 16:29:03)

Alan is entirely wrong, please ignore his comment and/or vote it down. This method encodes the algorithm and other parameters into the returned hash.
Deliberately specifying the algorithm to use should only be done in a carefully considered scenario, and these functions are designed and policies have been decided to ensure wide compatibility.
The risk of forcing a particular algorithm is that your code will continue to use a weaker algorithm as newer ones are implemented and strengthened.

Alan (2013-06-20 20:31:21)

Fyi, I ran into an issue when not providing the salt to crypt() which coincides with the documentation on the crypt() page: "If not provided, the behavior is defined by the algorithm implementation and can lead to unexpected results." Encrypting text on a windows platform could not be decrypted on a linux platform because the default algorithms were different. I had to specifically provide a salt to ensure the same algorithm was used on both platforms. I would imagine this would occur with password_hash() as well.

martinstoeckli (2013-03-08 22:36:28)

In most cases it is best to omit the salt parameter. Without this parameter, the function will generate a cryptographically safe salt, from the random source of the operating system.

lekensteyn at gmail dot com (2012-12-29 15:11:02)

The length of the hash returned by password_hash() for for PASSWORD_BCRYPT is 60 chars.

martinstoeckli (2012-12-17 20:56:40)

There is a compatibility pack available for PHP versions 5.3.7 and later, so you don't have to wait on version 5.5 for using this function. It comes in form of a single php file:
https://github.com/ircmaxell/password_compat

易百教程