Created
April 20, 2011 21:28
-
-
Save sli/932916 to your computer and use it in GitHub Desktop.
A crypt() wrapper that takes named arguments.
This file contains hidden or 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 | |
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