Skip to content

Instantly share code, notes, and snippets.

@bkilshaw
Created January 25, 2011 02:37
Show Gist options
  • Save bkilshaw/794416 to your computer and use it in GitHub Desktop.
Save bkilshaw/794416 to your computer and use it in GitHub Desktop.
<?php
function hash_password($password, $salt = false) {
if(!$salt) {
$gen_salt = generate_random_string();
$hashed['salt'] = '$6$rounds=150000$' . $gen_salt . '$';
} else {
$hashed['salt'] = '$6$rounds=150000$' . $salt . '$';
}
$hashed['password'] = crypt($password, $hashed['salt']);
return $hashed;
}
function generate_random_string($length = 16){
$characterList = "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789";
$string = "";
for ($i = 1; $i <= $length; $i++) {
$string .= $characterList[mt_rand(0, strlen($characterList) - 1)];
}
return $string;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment