PHP5 hash creation and verification methods in the

Source: Internet
Author: User
Tags hash

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, option. 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 have not given this hash any options. 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 is more secure.

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

Now that the hash is created, we can see that the new hash is worth the information by Password_get_info (). Password_get_info () requires an argument--a 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 (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 option. This is useful when your database is compromised and you need to adjust the hash. By using Password_needs_rehash () to check each hash value, we can see whether the saved hash value matches the new parameter and affects only those values created with the old parameter.

Finally, we've created our hash value, looked at how it was created, looked up whether it needed to be hashed, and now we need to validate it. To validate plain text to its hash value, we must use Password_verify (), which requires two parameters, a password and a hash value, and returns TRUE or FALSE. Let's check the hashed we've got 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

<?php
The Password_hash () example to the "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 the above knowledge, you can quickly and securely create a hash password in the new PHP 5.5.0 version.

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.