Skip to content

Instantly share code, notes, and snippets.

@sli
Created April 20, 2011 21:28
Show Gist options
  • Save sli/932916 to your computer and use it in GitHub Desktop.
Save sli/932916 to your computer and use it in GitHub Desktop.
A crypt() wrapper that takes named arguments.
<?php
function smart_crypt($str, $salt, $algo=CRYPT_STD_DES, $rounds=5000, $cost=7) {
switch ($algo) {
case CRYPT_EXT_DES:
$salt = substr($salt, 0, 4);
return crypt($str, '_?..' . $salt);
case CRYPT_MD5:
$salt = substr($salt, 0, 12);
return crypt($str, '$1$' . $salt . '$');
case CRYPT_BLOWFISH:
if ($cost < 10) { $cost = '0' . $cost; }
if ($cost > 99) { $cost = substr($cost, 0, 2); };
$salt = substr($salt, 0, 22);
return crypt($str, '$2a$' . $cost . '$' . $salt . '$');
case CRYPT_SHA256:
$salt = substr($salt, 0, 16);
return crypt($str, '$5$rounds=' . $rounds . '$' . $salt . '$');
case CRYPT_SHA512:
$salt = substr($salt, 0, 16);
return crypt($str, '$6$rounds=' . $rounds . '$' . $salt . '$');
case CRYPT_STD_DES:
default:
$salt = substr($salt, 0, 2);
return crypt($str, $salt);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment