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

103 lines
2.5 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Security;
2011-10-05 18:32:48 -05:00
use Laravel\Config;
if (trim(Config::get('application.key')) === '')
{
throw new \Exception('The encryption class may not be used without an encryption key.');
}
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-09-20 23:48:35 -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.
*
* <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-05 18:32:48 -05:00
// Determine the most appropriate random number generator for the
// OS and system and environment the application is running on.
2011-08-14 21:58:11 -05:00
if (defined('MCRYPT_DEV_URANDOM'))
{
2011-09-20 23:48:35 -05:00
$randomizer = MCRYPT_DEV_URANDOM;
2011-08-14 21:58:11 -05:00
}
elseif (defined('MCRYPT_DEV_RANDOM'))
{
2011-09-20 23:48:35 -05:00
$randomizer = MCRYPT_DEV_RANDOM;
2011-08-14 21:58:11 -05:00
}
2011-09-20 23:48:35 -05:00
else
{
$randomizer = MCRYPT_RAND;
}
2011-10-05 18:32:48 -05:00
$iv = mcrypt_create_iv(static::iv_size(), $randomizer);
2011-06-08 23:45:08 -05:00
2011-10-05 18:32:48 -05:00
return base64_encode($iv.mcrypt_encrypt(static::$cipher, Config::get('application.key'), $value, static::$mode, $iv));
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-09-20 23:48:35 -05:00
* <code>
* // Decrypt a string using the Mcrypt PHP extension
* $decrypted = Crypter::decrypt($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 decrypt($value)
2011-06-08 23:45:08 -05:00
{
2011-09-20 23:48:35 -05:00
// Since all encrypted strings generated by this class are base64 encoded, we will
// first attempt to base64 decode the string. If we can't do it, we'll bail out.
2011-08-08 13:14:50 -05:00
if ( ! is_string($value = base64_decode($value, true)))
2011-06-08 23:45:08 -05:00
{
throw new \Exception('Decryption error. Input value is not valid base64 data.');
}
2011-10-05 18:32:48 -05:00
// Extract the input vector and the encrypted string from the value.
// These will be used by Mcrypt to properly decrypt the value.
$iv = substr($value, 0, static::iv_size());
$value = substr($value, static::iv_size());
2011-06-08 23:45:08 -05:00
2011-10-05 18:32:48 -05:00
return rtrim(mcrypt_decrypt(static::$cipher, Config::get('application.key'), $value, static::$mode, $iv), "\0");
2011-06-08 23:45:08 -05:00
}
/**
* Get the input vector size for the cipher and mode.
*
2011-06-16 20:33:15 -05:00
* Different ciphers and modes use varying lengths of input vectors.
*
2011-06-08 23:45:08 -05:00
* @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
}
}