Create a secure password hash with SHA256 in php
• Jun 13th, 2012• Category: PHP
Most of the time PHP developers encrypt password with MD5. But this days we need more secure encrypted password.
Bellow I will show how to use SHA256 encryption method to create secure password hash.
We just need two simple function:
First function genenrate_salt create a random string which will be used as salt for password hashing and function genenrate_password do the rest
<?php
function genenrate_salt(){
$rndstring = "";
$length = 64;
$a = "";
$b = "";
$template = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
settype($length, "integer");
settype($rndstring, "string");
settype($a, "integer");
settype($b, "integer");
for ($a = 0; $a <= $length; $a++) {
$b = rand(0, strlen($template) - 1);
$rndstring .= $template[$b];
}
return $rndstring;
}
function genenrate_password($salt,$pass){
$password_hash = '';
$mysalt = $salt;
$password_hash= hash('SHA256', "-".$mysalt."-".$pass."-");
return $password_hash;
}
$salt = genenrate_salt();
$plain_pass = "hello123"; /* any password/text collected from user input */
echo "My Hash Password Is: ".genenrate_password($salt , $plain_pass);
?
Hope this will help some one.

Hello! This is my first comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading through your posts. Can you recommend any other blogs/websites/forums that go over the same subjects? Thanks for your time!