Skip to content

Instantly share code, notes, and snippets.

@ichiriac
Created August 21, 2012 21:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichiriac/3419581 to your computer and use it in GitHub Desktop.
Save ichiriac/3419581 to your computer and use it in GitHub Desktop.
A strong hashing function
<?php
/**
* This function is distributed under the MIT Open Source License.
* @author Ioan CHIRIAC
* @link https://github.com/ichiriac
*/
function generate_password($password, $salt = 'your-secret-salt', $algo = 'sha256')
{
// split the password
$password = str_split($password, ceil(strlen($password)/2));
// make a variable length salt
$salt =
$password[0]
. hash($algo, $password[0])
. $salt
. hash($algo, $password[1])
. $password[1]
;
// split the salt into 2 parts
$salt = str_split($salt,ceil(strlen($salt)/2));
// generating the hash with variable length generated salt
return hash(
$algo,
$salt[0]
. $password[0]
. $salt[1]
. $password[1]
);
}
@ichiriac
Copy link
Author

Usage :
echo generate_password('test', 'secret', 'sha256');

Advantages :

  • This function is safe from generated rainbow tables
  • It costs 3x times more to generate a rainbow table (using 3 hash functions)
  • The salt is hidden from hashes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment