Files
spa-api/laravel/security/crypter.php

121 lines
2.6 KiB
PHP
Raw Normal View History

2011-10-10 21:34:15 -05:00
<?php namespace Laravel\Security; use Laravel\Config;
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-10-10 21:34:15 -05:00
throw new \Exception('The encryption 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
*/
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-05 18:32:48 -05:00
protected static $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
*
2011-10-15 14:04:11 -05:00
* The string will be encrypted using the cipher and mode specified when the
* crypter instance was created, and the final result will be base64 encoded.
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:28:39 -05:00
$iv = mcrypt_create_iv(static::iv_size(), static::randomizer());
$value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
return base64_encode($iv.$value);
}
/**
* Get the random number generator appropriate for the server.
*
* There are a variety of sources to get a random number; however, not all
* of them will be available on every server. We will attempt to use the
* most secure random number generator available.
*
* @return int
*/
protected static function randomizer()
{
2011-08-14 21:58:11 -05:00
if (defined('MCRYPT_DEV_URANDOM'))
{
2011-10-20 22:28:39 -05:00
return MCRYPT_DEV_URANDOM;
2011-08-14 21:58:11 -05:00
}
elseif (defined('MCRYPT_DEV_RANDOM'))
{
2011-10-20 22:28:39 -05:00
return MCRYPT_DEV_RANDOM;
2011-09-20 23:48:35 -05:00
}
2011-10-20 22:28:39 -05:00
return MCRYPT_RAND;
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
{
2011-10-19 22:44:02 -05:00
list($iv, $value) = static::parse(base64_decode($value, true));
2011-10-15 14:04:11 -05:00
2011-10-20 21:44:18 -05:00
$value = mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
return rtrim($value, "\0");
2011-06-08 23:45:08 -05:00
}
/**
* Parse an encrypted value into the input vector and the actual value.
*
* @param string $value
* @return array
*/
protected static function parse($value)
{
2011-10-20 21:44:18 -05:00
if ($value === false)
2011-10-19 22:44:02 -05:00
{
throw new \Exception('Decryption error. Input value is not valid base64 data.');
}
return array(substr($value, 0, static::iv_size()), substr($value, static::iv_size()));
}
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-06-08 23:45:08 -05:00
}