Created
August 21, 2012 21:30
-
-
Save ichiriac/3419581 to your computer and use it in GitHub Desktop.
A strong hashing function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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] | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage :
echo generate_password('test', 'secret', 'sha256');
Advantages :