Skip to content

Instantly share code, notes, and snippets.

@skoshy
Created November 29, 2016 19:49
Show Gist options
  • Save skoshy/2c6742c0e2c3dd621f77cad8169fb4a5 to your computer and use it in GitHub Desktop.
Save skoshy/2c6742c0e2c3dd621f77cad8169fb4a5 to your computer and use it in GitHub Desktop.
<?
/*
* How to Use
*
* Supply a message to encrypt, optionally supply a salt as well, otherwise a random one will be created
* Salt MUST be 22 characters to work reliably
* You will receive an array with $arr['salt'] being the salt and $arr['digest'] being the encrypted result
*/
// Generates a random salt and encrypts a string with Blowfish using specified number of rounds
function crypt_blowfish($message, $salt = "none") {
// Must be TWO digits between 04-31
$rounds = "09";
if ($salt == "none")
$salt = bin2hex(openssl_random_pseudo_bytes(11));
// Remove null characters from message
$message = str_replace("\x00", "", $message)
$crypted = crypt($message, '$2a$'.$rounds.'$'.$salt.'$');
$toReturn = array();
$toReturn['salt'] = $salt;
$toReturn['digest'] = substr($crypted, 28);
return $toReturn;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment