A detailed description of hashing creation and validation methods in PHP5 _php tutorial

Source: Internet
Author: User
If you use the php5.5 version, we have a much simpler approach to hash creation and validation, and PHP 5.5 provides us with 4 functions: Password_get_info (), Password_hash (), Password_needs_rehash () , and Password_verify (), with the four of them we can quickly implement hash creation and validation.

The Password_hash () function is discussed first. This will be used as a hash value for creating a new password. It contains three parameters: password, hash algorithm, options. The first two items are required. You can use this function according to the following example:

The code is as follows Copy Code

$password = ' Foo ';
$hash = Password_hash ($password, Password_bcrypt);
$2y$10$uoegxj09qznqskvpfxr61uwjpjbxvdh2kgjqvnodzjnglhs2wtwhu

You will notice that we do not add any options to this hash. The options available now are limited to two: cost and salt. Demon Add option you need to create an associative array.

The code is as follows Copy Code
$options = [' cost ' = = 10,
' Salt ' = Mcrypt_create_iv (mcrypt_dev_urandom)];

After adding the option to the Password_hash () function, our hash value is changed, which makes it more secure.

The code is as follows Copy Code
$hash = Password_hash ($password, Password_bcrypt, $options);
$2y$10$jdj5jdewjdhsthv6sgviquprrhzngqsuetlk8iem0okh6hpycoo22

Now that the hash has been created, we can view the new hash with Password_get_info () worth the information. Password_get_info () requires a parameter--the hash value--and returns an associative array that contains the algorithm (the integer representation of the hash algorithm used), the algorithm name (the readable name of the hash algorithm used), and the option (which we use to create the hash-worthy option).

The code is as follows Copy Code
Var_dump (Password_get_info ($hash));
/*
Array (3) {
["Algo"]=>
Int (1)
["AlgoName"]=>
String (6) "Bcrypt"
["Options"]=>
Array (1) {
["Cost"]=>
Int (10)
}
}

* * First one is added to the Password Hashing API is Password_needs_rehash (), it accepts three parameters, hash, hash algorithm and options, the first two are required. Password_needs_rehash () is used to check whether a hash value was created using a specific algorithm and options. This is useful if your database is damaged and you need to adjust the hash. By using Password_needs_rehash () to check each hash value, we can see if the existing hash value matches the new parameter, only those values created with the old parameter are affected.

Finally, we've created our hash value, looked at how it was created, checked to see if it needed to be hashed, and now we need to verify it. To verify plain text to its hash value, we must use Password_verify (), which requires two parameters, a password and a hash value, and will return TRUE or FALSE. Let's check if we get the hashed to see if it's right.

The code is as follows Copy Code

$authenticate = password_verify (' foo ', ' $2y$10$jdj5jdewjdhsthv6sgviquprrhzngqsuetlk8iem0okh6hpycoo22 ');
TRUE
$authenticate = password_verify (' Bar ', ' $2y$10$jdj5jdewjdhsthv6sgviquprrhzngqsuetlk8iem0okh6hpycoo22 ');
FALSE

Cases

Example #1 password_verify () Example

The code is as follows Copy Code

See the Password_hash () example to see where this came from.
$hash = ' $2Y$07$BCRYPTREQUIRES22CHRCTE/VLQH0PIJTJXL.0T1XKA8PW9DMXTPOQ ';

if (password_verify (' Rasmuslerdorf ', $hash)) {
Echo ' Password is valid! ';
} else {
echo ' Invalid password. ';
}
?>
The above routines will output:

Password is valid!

With this knowledge, you can quickly and securely create a hash password in the new version of PHP 5.5.0.

http://www.bkjia.com/PHPjc/632836.html www.bkjia.com true http://www.bkjia.com/PHPjc/632836.html techarticle If you use the php5.5 version, we have a much simpler approach to hash creation and validation, and PHP 5.5 provides us with 4 functions: Password_get_info (), Password_hash (), password_needs ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.