PHP5.5.0 is officially released, and brings a complete list of new features and functions. One of the new APIs is PasswordHashingAPI. It contains four functions: password_get_info (), password_hash (), password_needs_rehash (), and password_verify ().
PHP 5.5.0 is officially released, 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:
$ Password = 'foo ';
$ Hash = password_hash ($ password, PASSWORD_BCRYPT );
// $ 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. To add options, you need to create an associated array.
$ Options = ['cost' => 10,
'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.
$ Hash = password_hash ($ password, PASSWORD_BCRYPT, $ options );
// $ 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.
Var_dump (password_get_info ($ hash ));
/*
Array (3 ){
["Algo"] =>
Int (1)
["AlgoName"] =>
String (6) "bcrypt"
["Options"] =>
Array (1 ){
["Cost"] =>
Int (10)
}
}
*/
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.
$ Authenticate = password_verify ('foo', '$ 2y $10 $ JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22 ');
// TRUE
$ Authenticate = password_verify ('bar', '$ 2y $10 $ JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22 ');
// FALSE
With the above knowledge, you can quickly and securely create a hash password in the new PHP 5.5.0 version.