Skip to content

Instantly share code, notes, and snippets.

@Mauryashubham
Last active August 3, 2017 05:04
Show Gist options
  • Save Mauryashubham/ab4c9e3ce768c2dce131516fd58ffb28 to your computer and use it in GitHub Desktop.
Save Mauryashubham/ab4c9e3ce768c2dce131516fd58ffb28 to your computer and use it in GitHub Desktop.
Password Hashing and Crypting Using PHP
Hi all , Welcome to shubhammaurya.com , Today we are going to discuss ,
how to Hash Password and Crypt Password Using PHP.
We are going to use 2 Methods.
1.crypt() and 2.password_hash()
1.Make a file in notepad and save it as index.php and paste the below code.
<?php
echo “<br><br>Password crypt()<br>”;
//Password crypt()
$pass=”password”; //You can take input from user also
$pass_check=”password”;
$hashed_password = crypt($pass,'st'); // let the salt be automatically generated
echo “Hashed Password : “.$hashed_password;
if (hash_equals($hashed_password, crypt($pass_check, $hashed_password)))
{
echo “<br>Password verified!”;
}
echo “<br><br>Password Hashing<br>”;
//Password Hashing$options = [‘cost’ => 12];
$new_password = password_hash($pass, PASSWORD_DEFAULT, $options);
echo “<br>New Password : “.$new_password;
if(password_verify($pass,$new_password))
{
echo “<br>Password verified!”;
}
else
{
echo “<br>Password Not verified!”;
}
?>
Output :
Password crypt()
Hashed Password : $1$9J1.cg1.$Jn4Wqanhwrb1ftqvJPH4e0
Password verified!
Password Hashing
New Password : $2y$12$GawqdMGynl9DbjEXmxNkLuL.Bwq6OIgd3zrIDBDd.71QgYEKbGX9G
Password verified!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment