Files
spa-api/laravel/crypter.php

87 lines
2.0 KiB
PHP
Raw Normal View History

2012-01-16 13:59:24 -06:00
<?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');
2011-10-05 18:32:48 -05:00
2012-01-16 13:59:24 -06:00
if (trim(Config::get('application.key')) === '')
2011-10-05 18:32:48 -05:00
{
2012-01-16 13:59:24 -06:00
throw new \Exception('The Crypter class may not be used without an application key.');
2011-10-05 18:32:48 -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-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
{
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
*
* @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
// encryption ciphers and modes, so we will get the correct size for
// the cipher and mode being used by the class.
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,
// so we will trim all of the padding characters from the string.
$key = static::key();
return rtrim(mcrypt_decrypt(static::$cipher, $key, $value, static::$mode, $iv), "\0");
}
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()
{
2012-01-16 13:59:24 -06:00
return Config::get('application.key');
2011-10-19 22:44:02 -05:00
}
}