Skip to content

Instantly share code, notes, and snippets.

@themattharris
Created February 1, 2010 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themattharris/292166 to your computer and use it in GitHub Desktop.
Save themattharris/292166 to your computer and use it in GitHub Desktop.
/**
* makeSalt()
* generates a random salt up to the requested length
*
* @param int $length Length of salt to produce
*/
function makeSalt($length=45) {
$sequence = array_merge(range(0,9), range('A','Z'), range('a','z'), range('!','.'));
// remove unsafe characters
$unsafe = array('"',"'");
$sequence = array_diff($sequence, $unsafe);
$length = $length > count($sequence) ? count($sequence) : $length;
shuffle($sequence);
$sequence = implode($sequence);
return substr($sequence, 0, $length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment