123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php namespace Laravel\Security;
- class Crypter {
-
- public $cipher;
-
- public $mode;
-
- public $key;
-
- public function __construct($cipher, $mode, $key)
- {
- $this->cipher = $cipher;
- $this->mode = $mode;
- $this->key = $key;
- if (trim((string) $this->key) === '')
- {
- throw new \Exception('The encryption class may not be used without an encryption key.');
- }
- }
-
- public function encrypt($value)
- {
- $iv = mcrypt_create_iv($this->iv_size(), $this->randomizer());
- return base64_encode($iv.mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv));
- }
-
- protected function randomizer()
- {
- if (defined('MCRYPT_DEV_URANDOM'))
- {
- return MCRYPT_DEV_URANDOM;
- }
- elseif (defined('MCRYPT_DEV_RANDOM'))
- {
- return MCRYPT_DEV_RANDOM;
- }
- return MCRYPT_RAND;
- }
-
- public function decrypt($value)
- {
- if ( ! is_string($value = base64_decode($value, true)))
- {
- throw new \Exception('Decryption error. Input value is not valid base64 data.');
- }
- list($iv, $value) = array(substr($value, 0, $this->iv_size()), substr($value, $this->iv_size()));
- return rtrim(mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv), "\0");
- }
-
- private function iv_size()
- {
- return mcrypt_get_iv_size($this->cipher, $this->mode);
- }
- }
|