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

125 lines
2.9 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Security;
class Crypter {
2011-06-08 23:45:08 -05:00
/**
* The encryption cipher.
*
* @var string
*/
public $cipher;
2011-06-08 23:45:08 -05:00
/**
* The encryption mode.
*
* @var string
*/
public $mode;
2011-08-19 20:12:39 -05:00
/**
* The encryption key.
*
* @var string
*/
public $key;
/**
* Create a new Crypter instance.
*
2011-09-20 23:48:35 -05:00
* A valid cipher and mode supported by the Mcrypt extension must be given to the constructor.
* Also, an encryption key (typically from the application configuration) must be specified.
*
* @param string $cipher
* @param string $mode
2011-08-19 20:12:39 -05:00
* @param string $key
* @return void
*/
public function __construct($cipher, $mode, $key)
{
2011-08-19 20:12:39 -05:00
$this->key = $key;
2011-09-20 23:48:35 -05:00
$this->mode = $mode;
$this->cipher = $cipher;
2011-08-19 20:12:39 -05:00
if (trim((string) $this->key) === '')
{
2011-08-22 22:21:51 -05:00
throw new \Exception('The encryption class may not be used without an encryption key.');
2011-08-19 20:12:39 -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-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
*/
public function encrypt($value)
2011-06-08 23:45:08 -05:00
{
2011-09-20 23:48:35 -05:00
// Determine the most appropriate random number generator for the operating
// 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;
}
$iv = mcrypt_create_iv($this->iv_size(), $randomizer);
2011-06-08 23:45:08 -05:00
2011-09-20 23:48:35 -05:00
return base64_encode($iv.mcrypt_encrypt($this->cipher, $this->key, $value, $this->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
* The string will be decrypted using the cipher and mode specified when the crypter was created.
*
* <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
*/
public 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-09-20 23:48:35 -05:00
// Extract the input vector and the encrypted string from the value
list($iv, $value) = array(substr($value, 0, $this->iv_size()), substr($value, $this->iv_size()));
2011-06-08 23:45:08 -05:00
2011-08-19 20:12:39 -05:00
return rtrim(mcrypt_decrypt($this->cipher, $this->key, $value, $this->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
*/
private function iv_size()
2011-06-08 23:45:08 -05:00
{
return mcrypt_get_iv_size($this->cipher, $this->mode);
2011-06-08 23:45:08 -05:00
}
}