2012-01-28 14:55:08 -06:00
|
|
|
<?php namespace Laravel; defined('DS') or die('No direct script access.');
|
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
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static $cipher = MCRYPT_RIJNDAEL_256;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The encryption mode.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static $mode = MCRYPT_MODE_CBC;
|
2011-08-15 20:11:21 -05:00
|
|
|
|
2012-02-16 14:31:11 -06:00
|
|
|
/**
|
|
|
|
|
* The block size of the cipher.
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
public static $block = 32;
|
|
|
|
|
|
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
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* The string will be encrypted using the AES-256 scheme and will be base64 encoded.
|
2011-09-20 23:48:35 -05:00
|
|
|
*
|
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
|
|
|
{
|
2012-02-12 19:45:04 -06:00
|
|
|
$iv = mcrypt_create_iv(static::iv_size(), static::randomizer());
|
2011-10-20 22:28:39 -05:00
|
|
|
|
2012-02-16 14:31:11 -06:00
|
|
|
$value = static::pad($value);
|
|
|
|
|
|
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
|
|
|
*
|
|
|
|
|
* @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
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
$value = base64_decode($value);
|
2011-10-15 14:04:11 -05:00
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
// To decrypt the value, we first need to extract the input vector and
|
|
|
|
|
// the encrypted value. The input vector size varies across different
|
2012-02-08 21:31:27 -06:00
|
|
|
// encryption ciphers and modes, so we'll get the correct size.
|
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
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
// Once we have the input vector and the value, we can give them both
|
|
|
|
|
// to Mcrypt for decryption. The value is sometimes padded with \0,
|
2012-02-08 21:31:27 -06:00
|
|
|
// so we will trim all of the padding characters.
|
2012-01-16 13:59:24 -06:00
|
|
|
$key = static::key();
|
|
|
|
|
|
2012-02-16 14:31:11 -06:00
|
|
|
$value = mcrypt_decrypt(static::$cipher, $key, $value, static::$mode, $iv);
|
|
|
|
|
|
|
|
|
|
return static::unpad($value);
|
2011-10-18 20:19:36 -05:00
|
|
|
}
|
|
|
|
|
|
2012-02-12 19:45:04 -06:00
|
|
|
/**
|
|
|
|
|
* Get the most secure random number generator for the system.
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
protected static function randomizer()
|
|
|
|
|
{
|
|
|
|
|
// There are various sources from which we can get random numbers
|
|
|
|
|
// but some are more random than others. We'll choose the most
|
|
|
|
|
// random source we can for this server environment.
|
|
|
|
|
if (defined('MCRYPT_DEV_URANDOM'))
|
|
|
|
|
{
|
|
|
|
|
return MCRYPT_DEV_URANDOM;
|
|
|
|
|
}
|
|
|
|
|
elseif (defined('MCRYPT_DEV_RANDOM'))
|
|
|
|
|
{
|
|
|
|
|
return MCRYPT_DEV_RANDOM;
|
|
|
|
|
}
|
|
|
|
|
// When using the default random number generator, we'll seed
|
|
|
|
|
// the generator on each call to ensure the results are as
|
|
|
|
|
// random as we can possibly get them.
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mt_srand();
|
|
|
|
|
|
|
|
|
|
return MCRYPT_RAND;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2012-02-16 14:31:11 -06:00
|
|
|
/**
|
|
|
|
|
* Add PKCS7 compatible padding on the given value.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function pad($value)
|
|
|
|
|
{
|
|
|
|
|
$pad = static::$block - (Str::length($value) % static::$block);
|
|
|
|
|
|
|
|
|
|
return $value .= str_repeat(chr($pad), $pad);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the PKCS7 compatible padding from the given value.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function unpad($value)
|
|
|
|
|
{
|
|
|
|
|
$pad = ord($value[($length = Str::length($value)) - 1]);
|
|
|
|
|
|
2012-02-16 14:53:22 -06:00
|
|
|
if ($pad and $pad < static::$block)
|
|
|
|
|
{
|
|
|
|
|
// If the correct padding is present on the string, we will remove
|
|
|
|
|
// it and return the value. Otherwise, we'll throw an exception
|
|
|
|
|
// as the padding appears to have been changed.
|
|
|
|
|
if (preg_match('/'.chr($pad).'{'.$pad.'}$/', $value))
|
|
|
|
|
{
|
|
|
|
|
return substr($value, 0, $length - $pad);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-19 22:56:59 -06:00
|
|
|
// If the padding characters do not match the expected padding
|
|
|
|
|
// for the value we'll bomb out with an exception since the
|
|
|
|
|
// encrypted value seems to have been changed.
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("Decryption error. Padding is invalid.");
|
|
|
|
|
}
|
2012-02-16 14:53:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
2012-02-16 14:31:11 -06:00
|
|
|
}
|
|
|
|
|
|
2011-10-19 22:44:02 -05:00
|
|
|
/**
|
|
|
|
|
* Get the encryption key from the application configuration.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function key()
|
|
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return Config::get('application.key');
|
2011-10-19 22:44:02 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-23 08:17:19 -06:00
|
|
|
}
|