2011-11-11 21:27:30 -06:00
|
|
|
<?php namespace Laravel;
|
2011-10-05 18:32:48 -05:00
|
|
|
|
2011-10-15 14:04:11 -05:00
|
|
|
if (trim(Config::$items['application']['key']) === '')
|
2011-10-05 18:32:48 -05:00
|
|
|
{
|
2011-11-15 12:35:04 +00:00
|
|
|
throw new \LogicException('The encryption class may not be used without an application key.');
|
2011-10-05 18:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-15 20:11:21 -05:00
|
|
|
class Crypter {
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The encryption cipher.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
protected static $cipher = MCRYPT_RIJNDAEL_256;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The encryption mode.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2011-10-20 22:47:00 -05:00
|
|
|
protected static $mode = MCRYPT_MODE_CBC;
|
2011-08-15 20:11:21 -05:00
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2011-08-22 22:25:12 -05:00
|
|
|
* Encrypt a string using Mcrypt.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-10-21 21:49:33 -05:00
|
|
|
* The given string will be encrypted using AES-256 encryption for a high
|
|
|
|
|
* degree of security. The returned string will also be base64 encoded.
|
|
|
|
|
*
|
|
|
|
|
* Mcrypt must be installed on your machine before using this method, and
|
|
|
|
|
* an application key must be specified in the application configuration.
|
2011-09-20 23:48:35 -05:00
|
|
|
*
|
|
|
|
|
* <code>
|
|
|
|
|
* // Encrypt a string using the Mcrypt PHP extension
|
|
|
|
|
* $encrypted = Crypter::encrpt('secret');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-06-08 23:45:08 -05:00
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function encrypt($value)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-20 22:47:00 -05:00
|
|
|
$iv = mcrypt_create_iv(static::iv_size(), MCRYPT_RAND);
|
2011-10-20 22:28:39 -05:00
|
|
|
|
|
|
|
|
$value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
|
|
|
|
|
|
|
|
|
|
return base64_encode($iv.$value);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2011-08-22 22:25:12 -05:00
|
|
|
* Decrypt a string using Mcrypt.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-10-21 21:49:33 -05:00
|
|
|
* The given encrypted value must have been encrypted using Laravel and
|
|
|
|
|
* the application key specified in the application configuration file.
|
|
|
|
|
*
|
|
|
|
|
* Mcrypt must be installed on your machine before using this method.
|
|
|
|
|
*
|
2011-06-08 23:45:08 -05:00
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function decrypt($value)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-21 21:49:33 -05:00
|
|
|
if (($value = base64_decode($value)) === false)
|
|
|
|
|
{
|
2011-11-15 12:35:04 +00:00
|
|
|
throw new \InvalidArgumentException('Decryption error. Input value is not valid base64 data.');
|
2011-10-21 21:49:33 -05:00
|
|
|
}
|
2011-10-15 14:04:11 -05:00
|
|
|
|
2011-11-01 19:41:35 -05:00
|
|
|
$iv = substr($value, 0, static::iv_size());
|
2011-10-20 21:44:18 -05:00
|
|
|
|
2011-11-01 19:41:35 -05:00
|
|
|
$value = substr($value, static::iv_size());
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-11-01 19:41:35 -05:00
|
|
|
return rtrim(mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv), "\0");
|
2011-10-18 20:19:36 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Get the input vector size for the cipher and mode.
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
protected static function iv_size()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return mcrypt_get_iv_size(static::$cipher, static::$mode);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-10-19 22:44:02 -05:00
|
|
|
/**
|
|
|
|
|
* Get the encryption key from the application configuration.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function key()
|
|
|
|
|
{
|
|
|
|
|
return Config::$items['application']['key'];
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 12:35:04 +00:00
|
|
|
}
|