crypter.php 3.76 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel; defined('DS') or die('No direct script access.');
2

3
class Crypter {
4 5 6 7 8 9

	/**
	 * The encryption cipher.
	 *
	 * @var string
	 */
10
	public static $cipher = MCRYPT_RIJNDAEL_256;
11 12 13 14 15 16

	/**
	 * The encryption mode.
	 *
	 * @var string
	 */
17
	public static $mode = MCRYPT_MODE_CBC;
18 19

	/**
20 21 22 23 24 25 26
	 * The block size of the cipher.
	 *
	 * @var int
	 */
	public static $block = 32;

	/**
Taylor Otwell committed
27
	 * Encrypt a string using Mcrypt.
28
	 *
29
	 * The string will be encrypted using the AES-256 scheme and will be base64 encoded.
30
	 *
31 32 33
	 * @param  string  $value
	 * @return string
	 */
34
	public static function encrypt($value)
35
	{
36
		$iv = mcrypt_create_iv(static::iv_size(), static::randomizer());
37

38 39
		$value = static::pad($value);

40 41 42 43 44 45
		$value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);

		return base64_encode($iv.$value);
	}

	/**
Taylor Otwell committed
46
	 * Decrypt a string using Mcrypt.
47 48 49 50
	 *
	 * @param  string  $value
	 * @return string
	 */
51
	public static function decrypt($value)
52
	{
53
		$value = base64_decode($value);
54

55 56
		// To decrypt the value, we first need to extract the input vector and
		// the encrypted value. The input vector size varies across different
Taylor Otwell committed
57
		// encryption ciphers and modes, so we'll get the correct size.
Taylor Otwell committed
58
		$iv = substr($value, 0, static::iv_size());
59

Taylor Otwell committed
60
		$value = substr($value, static::iv_size());
61

62 63
		// 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,
Taylor Otwell committed
64
		// so we will trim all of the padding characters.
65 66
		$key = static::key();

67 68 69
		$value = mcrypt_decrypt(static::$cipher, $key, $value, static::$mode, $iv);

		return static::unpad($value);
70 71 72
	}

	/**
73 74 75 76
	 * Get the most secure random number generator for the system.
	 *
	 * @return int
	 */
77
	public static function randomizer()
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	{
		// There are various sources from which we can get random numbers
		// but some are more random than others. We'll choose the most
		// random source we can for this server environment.
		if (defined('MCRYPT_DEV_URANDOM'))
		{
			return MCRYPT_DEV_URANDOM;
		}
		elseif (defined('MCRYPT_DEV_RANDOM'))
		{
			return MCRYPT_DEV_RANDOM;
		}
		// When using the default random number generator, we'll seed
		// the generator on each call to ensure the results are as
		// random as we can possibly get them.
		else
		{
			mt_srand();

			return MCRYPT_RAND;
		}
	}

	/**
102 103 104 105
	 * Get the input vector size for the cipher and mode.
	 *
	 * @return int
	 */
106
	protected static function iv_size()
107
	{
108
		return mcrypt_get_iv_size(static::$cipher, static::$mode);
109 110
	}

111
	/**
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	 * Add PKCS7 compatible padding on the given value.
	 *
	 * @param  string  $value
	 * @return string
	 */
	protected static function pad($value)
	{
		$pad = static::$block - (Str::length($value) % static::$block);

		return $value .= str_repeat(chr($pad), $pad);
	}

	/**
	 * Remove the PKCS7 compatible padding from the given value.
	 *
	 * @param  string  $value
	 * @return string
	 */
	protected static function unpad($value)
	{
		$pad = ord($value[($length = Str::length($value)) - 1]);

134 135 136 137 138 139 140 141 142 143
		if ($pad and $pad < static::$block)
		{
			// If the correct padding is present on the string, we will remove
			// it and return the value. Otherwise, we'll throw an exception
			// as the padding appears to have been changed.
			if (preg_match('/'.chr($pad).'{'.$pad.'}$/', $value))
			{
				return substr($value, 0, $length - $pad);
			}

Taylor Otwell committed
144 145 146 147 148 149 150
			// If the padding characters do not match the expected padding
			// for the value we'll bomb out with an exception since the
			// encrypted value seems to have been changed.
			else
			{
				throw new \Exception("Decryption error. Padding is invalid.");
			}
151 152 153
		}

		return $value;
154 155 156
	}

	/**
157 158 159 160 161 162
	 * Get the encryption key from the application configuration.
	 *
	 * @return string
	 */
	protected static function key()
	{
163
		return Config::get('application.key');
164 165
	}

166
}