PHP5.5.0 was released yesterday and brings a complete list of new features and functions. One of the new APIs is PasswordHashingAPI. It contains four functions.
PHP 5.5.0 was released yesterday and brings a complete list of new features and functions. One of the new APIs is the Password Hashing API. It contains four functions: password_get_info (), password_hash (), password_needs_rehash (), and password_verify (). let's know each function step by step.
We will first discuss the password_hash () function. This will be used as the hash value for creating a new password. It contains three parameters: password, hash algorithm, and option. The first two items are required. You can use this function as follows:
1 $ password = 'foo ';
2 $ hash = password_hash ($ password, PASSWORD_BCRYPT );
3 // $ 2y $10 $ uOegXJ09qznQsKvPfxr61uWjpJBxVDH2KGJQVnodzjnglhs2WTwHu
You will notice that we have not added any options for this hash. Currently, two available options are available: cost and salt. You need to create an associated array.
1 $ options = ['cost' => 10,
2 'Salt' => mcrypt_create_iv (22, MCRYPT_DEV_URANDOM)];
After the option is added to the password_hash () function, our hash value is changed, which is more secure.
1 $ hash = password_hash ($ password, PASSWORD_BCRYPT, $ options );
2 // $ 2y $10 $ JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22
Now that the hash is created, you can use password_get_info () to view information about the hash value. Password_get_info () requires a parameter -- hash value -- and returns a contains algorithm (represented by an integer of the hash algorithm used), Algorithm Name (readable name of the hash algorithm used) and the associated array of options (which we use to create the hash value option.
01 var_dump (password_get_info ($ hash ));
02 /*
03 array (3 ){
04 ["algo"] =>
05 int (1)
06 ["algoName"] =>
07 string (6) "bcrypt"
08 ["options"] =>
09 array (1 ){
10 ["cost"] =>
11 int (10)
12}
13}
14 */
First, password_needs_rehash () is added to the Password Hashing API. it accepts three parameters: hash, hash algorithm, and option. The first two parameters are required. Password_needs_rehash () is used to check whether a hash value is created using a specific algorithm and option. This is useful when your database is damaged and you need to adjust the hash. By using password_needs_rehash () to check each hash value, we can see whether the existing hash value matches the new parameter, only the values created using the old parameter are affected.
Finally, we have created our hash value, checked how it is created, and checked whether it needs to be re-hash. now we need to verify it. To verify the plain text to its hash value, we must use password_verify (). it requires two parameters, password and hash value, and returns TRUE or FALSE. Let's check the hashed we obtained once to see if it is correct.
1 $ authenticate = password_verify ('foo', '$ 2y $10 $ JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22 ');
2 // TRUE
3 $ authenticate = password_verify ('bar', '$ 2y $10 $ JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22 ');
4 // FALSE
With the above knowledge, you can quickly and securely create a hash password in the new PHP 5.5.0 version.