PHP | Password hashing

| By Webner

Password hashing is the new recommended way to store the password using hashing method (PHP 5 >= PHP 5.5, PHP7). Password hashing is used to encrypt the password similar to the way md5, sha1, sha256 etc do. But this method follows stronger hashing algorithm comparatively. There are two methods that are used in password hashing:


Password_hash method can be used with

PASSWORD_DEFAULT: Password hashing includes default method in which different salts can be used to produce hashed password. Every time if you run this algorithm it will give you different output.

For Example:

echo password_hash("testing_password", PASSWORD_DEFAULT,$optional);

This algorithm takes execution time based on cost parameter value. By default the value of cost is 10. Cost value of 1 and 2 are not good for creating password hash.

The time execution for the password_hash method is more than other algorithms.

For Example:

    $time_start_hashing = microtime(true);
    password_hash("testing_password", PASSWORD_DEFAULT,array('cost'=>10));
    $time_stop_hashing = microtime(true);
    $time_start_md5_hashing = microtime(true);
    $time_stop_md5_hashing = microtime(true);
    $time_start_sha1_hashing = microtime(true);
    $time_stop_sha1_hashing = microtime(true);
    echo "Time of execution for<br>";
    echo "password_hash is : ".($time_stop_hashing-$time_start_hashing)."<br>";
    echo "md5 is :".($time_stop_md5_hashing-$time_start_md5_hashing)."<br>";
    echo "sha1 is :".($time_stop_sha1_hashing-$time_start_sha1_hashing)."<br>";

Time of execution for
password_hash is : 0.059037923812866
md5 is :4.0531158447266E-6
sha1 is :4.0531158447266E-6

password_verify method is used to verify the entered password without converting into hashing. And while checking the password from database should be enclosed into single quotes if you use double quotes then it will make it as three variables.

With double quotes:


With single quotes:


For Example:

if(password_verify("testing_password", $password_to_verify)){
echo "Password verified successfully";
else {
echo "Password not verified";

Password verified successfully

Leave a Reply

Your email address will not be published. Required fields are marked *