Refactoring the crypt class.

This commit is contained in:
Taylor Otwell 2011-08-08 08:51:58 -05:00
parent bc51ec3d83
commit 1916e27de1
1 changed files with 3 additions and 9 deletions

View File

@ -24,8 +24,7 @@ class Crypt {
*/
public static function encrypt($value)
{
// If the system random number generator is being used, we need to seed
// it to get adequately random results.
// Seed the system random number generator so it will produce random results.
if (($random = static::randomizer()) === MCRYPT_RAND) mt_srand();
$iv = mcrypt_create_iv(static::iv_size(), $random);
@ -50,10 +49,8 @@ public static function decrypt($value)
throw new \Exception('Decryption error. Input value is not valid base64 data.');
}
// Extract the input vector from the value.
$iv = substr($value, 0, static::iv_size());
// Remove the input vector from the encrypted value.
$value = substr($value, static::iv_size());
return rtrim(mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv), "\0");
@ -87,12 +84,9 @@ private static function randomizer()
*/
private static function key()
{
if (is_null($key = Config::get('application.key')) or $key == '')
{
throw new \Exception("The encryption class can not be used without an encryption key.");
}
if ( ! is_null($key = Config::get('application.key')) and $key !== '') return $key;
return $key;
throw new \Exception("The encryption class can not be used without an encryption key.");
}
/**