// To hash the password, usepassword_hash("MySuperSafePassword!",PASSWORD_DEFAULT)// To compare hash with plain text, usepassword_verify("MySuperSafePassword!",$hashed_password)
<?php/**
* For the VAST majority of use-cases, let password_hash generate the salt randomly for you.
*/$password='idkWhatToUse';$hashedPassword=password_hash($password,PASSWORD_DEFAULT);?>
<?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)
*/echopassword_hash("rasmuslerdorf",PASSWORD_DEFAULT);?>
<?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,];echopassword_hash("rasmuslerdorf",PASSWORD_BCRYPT,$options);?>
/* New password. */$password=$_POST['password'];/* Remember to validate the password. *//* Create the new password hash. */$hash=password_hash($password,PASSWORD_DEFAULT);
<?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,];echopassword_hash("rasmuslerdorf",PASSWORD_BCRYPT,$options);?>
<?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)
* Other algorithms such as PASSWORD_BCRYPT and PASSWORD_ARGON2ID may be used
* instead of PASSWORD_DEFAULT
*/echopassword_hash("rasmuslerdorf",PASSWORD_DEFAULT);?>
/* Password. */$password='my secret password';/* Set the "cost" parameter to 12. */$options=['cost'=>12];/* Create the hash. */$hash=password_hash($password,PASSWORD_DEFAULT,$options);