/vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php

https://bitbucket.org/nick_snyder/summer · PHP · 260 lines · 94 code · 39 blank · 127 comment · 5 complexity · 8f83768956e9bef52f4d47a7feee9ea8 MD5 · raw file

  1. <?php namespace Illuminate\Encryption;
  2. class DecryptException extends \RuntimeException {}
  3. class Encrypter {
  4. /**
  5. * The encryption key.
  6. *
  7. * @var string
  8. */
  9. protected $key;
  10. /**
  11. * The algorithm used for encryption.
  12. *
  13. * @var string
  14. */
  15. protected $cipher = 'rijndael-256';
  16. /**
  17. * The mode used for encryption.
  18. *
  19. * @var string
  20. */
  21. protected $mode = 'cbc';
  22. /**
  23. * The block size of the cipher.
  24. *
  25. * @var int
  26. */
  27. protected $block = 32;
  28. /**
  29. * Create a new encrypter instance.
  30. *
  31. * @param string $key
  32. * @return void
  33. */
  34. public function __construct($key)
  35. {
  36. $this->key = $key;
  37. }
  38. /**
  39. * Encrypt the given value.
  40. *
  41. * @param string $value
  42. * @return string
  43. */
  44. public function encrypt($value)
  45. {
  46. $iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());
  47. $value = base64_encode($this->padAndMcrypt($value, $iv));
  48. // Once we have the encrypted value we will go ahead base64_encode the input
  49. // vector and create the MAC for the encrypted value so we can verify its
  50. // authenticity. Then, we'll JSON encode the data in a "payload" array.
  51. $mac = $this->hash($iv = base64_encode($iv), $value);
  52. return base64_encode(json_encode(compact('iv', 'value', 'mac')));
  53. }
  54. /**
  55. * Pad and use mcrypt on the given value and input vector.
  56. *
  57. * @param string $value
  58. * @param string $iv
  59. * @return string
  60. */
  61. protected function padAndMcrypt($value, $iv)
  62. {
  63. $value = $this->addPadding(serialize($value));
  64. return mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv);
  65. }
  66. /**
  67. * Decrypt the given value.
  68. *
  69. * @param string $payload
  70. * @return string
  71. */
  72. public function decrypt($payload)
  73. {
  74. $payload = $this->getJsonPayload($payload);
  75. // We'll go ahead and remove the PKCS7 padding from the encrypted value before
  76. // we decrypt it. Once we have the de-padded value, we will grab the vector
  77. // and decrypt the data, passing back the unserialized from of the value.
  78. $value = base64_decode($payload['value']);
  79. $iv = base64_decode($payload['iv']);
  80. return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));
  81. }
  82. /**
  83. * Run the mcrypt decryption routine for the value.
  84. *
  85. * @param string $value
  86. * @param string $iv
  87. * @return string
  88. */
  89. protected function mcryptDecrypt($value, $iv)
  90. {
  91. return mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv);
  92. }
  93. /**
  94. * Get the JSON array from the given payload.
  95. *
  96. * @param string $payload
  97. * @return array
  98. */
  99. protected function getJsonPayload($payload)
  100. {
  101. $payload = json_decode(base64_decode($payload), true);
  102. // If the payload is not valid JSON or does not have the proper keys set we will
  103. // assume it is invalid and bail out of the routine since we will not be able
  104. // to decrypt the given value. We'll also check the MAC for this encryption.
  105. if ( ! $payload or $this->invalidPayload($payload))
  106. {
  107. throw new DecryptException("Invalid data.");
  108. }
  109. if ($payload['mac'] !== $this->hash($payload['iv'], $payload['value']))
  110. {
  111. throw new DecryptException("MAC is invalid.");
  112. }
  113. return $payload;
  114. }
  115. /**
  116. * Create a MAC for the given value.
  117. *
  118. * @param string $iv
  119. * @param string $value
  120. * @return string
  121. */
  122. protected function hash($iv, $value)
  123. {
  124. return hash_hmac('sha256', $iv.$value, $this->key);
  125. }
  126. /**
  127. * Add PKCS7 padding to a given value.
  128. *
  129. * @param string $value
  130. * @return string
  131. */
  132. protected function addPadding($value)
  133. {
  134. $pad = $this->block - (strlen($value) % $this->block);
  135. return $value.str_repeat(chr($pad), $pad);
  136. }
  137. /**
  138. * Remove the padding from the given value.
  139. *
  140. * @param string $value
  141. * @return string
  142. */
  143. protected function stripPadding($value)
  144. {
  145. $pad = ord($value[($len = strlen($value)) - 1]);
  146. return $this->paddingIsValid($pad, $value) ? substr($value, 0, strlen($value) - $pad) : $value;
  147. }
  148. /**
  149. * Determine if the given padding for a value is valid.
  150. *
  151. * @param string $pad
  152. * @param string $value
  153. * @return bool
  154. */
  155. protected function paddingIsValid($pad, $value)
  156. {
  157. $beforePad = strlen($value) - $pad;
  158. return substr($value, $beforePad) == str_repeat(substr($value, -1), $pad);
  159. }
  160. /**
  161. * Verify that the encryption payload is valid.
  162. *
  163. * @param array $data
  164. * @return bool
  165. */
  166. protected function invalidPayload(array $data)
  167. {
  168. return ! isset($data['iv']) or ! isset($data['value']) or ! isset($data['mac']);
  169. }
  170. /**
  171. * Get the IV size for the cipher.
  172. *
  173. * @return int
  174. */
  175. protected function getIvSize()
  176. {
  177. return mcrypt_get_iv_size($this->cipher, $this->mode);
  178. }
  179. /**
  180. * Get the random data source available for the OS.
  181. *
  182. * @return int
  183. */
  184. protected function getRandomizer()
  185. {
  186. if (defined('MCRYPT_DEV_URANDOM')) return MCRYPT_DEV_URANDOM;
  187. if (defined('MCRYPT_DEV_RANDOM')) return MCRYPT_DEV_RANDOM;
  188. mt_srand();
  189. return MCRYPT_RAND;
  190. }
  191. /**
  192. * Set the encryption key.
  193. *
  194. * @param string $key
  195. * @return void
  196. */
  197. public function setKey($key)
  198. {
  199. $this->key = $key;
  200. }
  201. /**
  202. * Set the encryption cipher.
  203. *
  204. * @param string $cipher
  205. * @return void
  206. */
  207. public function setCipher($cipher)
  208. {
  209. $this->cipher = $cipher;
  210. }
  211. /**
  212. * Set the encryption mode.
  213. *
  214. * @param string $mode
  215. * @return void
  216. */
  217. public function setMode($mode)
  218. {
  219. $this->mode = $mode;
  220. }
  221. }