PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/nmhieucoder/laravel_tintuc
PHP | 240 lines | 99 code | 31 blank | 110 comment | 10 complexity | 295acc98f6848b2877502120e2afc7b8 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Encryption;
  3. use Illuminate\Contracts\Encryption\DecryptException;
  4. use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
  5. use Illuminate\Contracts\Encryption\EncryptException;
  6. use Illuminate\Contracts\Encryption\StringEncrypter;
  7. use RuntimeException;
  8. class Encrypter implements EncrypterContract, StringEncrypter
  9. {
  10. /**
  11. * The encryption key.
  12. *
  13. * @var string
  14. */
  15. protected $key;
  16. /**
  17. * The algorithm used for encryption.
  18. *
  19. * @var string
  20. */
  21. protected $cipher;
  22. /**
  23. * Create a new encrypter instance.
  24. *
  25. * @param string $key
  26. * @param string $cipher
  27. * @return void
  28. *
  29. * @throws \RuntimeException
  30. */
  31. public function __construct($key, $cipher = 'AES-128-CBC')
  32. {
  33. $key = (string) $key;
  34. if (static::supported($key, $cipher)) {
  35. $this->key = $key;
  36. $this->cipher = $cipher;
  37. } else {
  38. throw new RuntimeException('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.');
  39. }
  40. }
  41. /**
  42. * Determine if the given key and cipher combination is valid.
  43. *
  44. * @param string $key
  45. * @param string $cipher
  46. * @return bool
  47. */
  48. public static function supported($key, $cipher)
  49. {
  50. $length = mb_strlen($key, '8bit');
  51. return ($cipher === 'AES-128-CBC' && $length === 16) ||
  52. ($cipher === 'AES-256-CBC' && $length === 32);
  53. }
  54. /**
  55. * Create a new encryption key for the given cipher.
  56. *
  57. * @param string $cipher
  58. * @return string
  59. */
  60. public static function generateKey($cipher)
  61. {
  62. return random_bytes($cipher === 'AES-128-CBC' ? 16 : 32);
  63. }
  64. /**
  65. * Encrypt the given value.
  66. *
  67. * @param mixed $value
  68. * @param bool $serialize
  69. * @return string
  70. *
  71. * @throws \Illuminate\Contracts\Encryption\EncryptException
  72. */
  73. public function encrypt($value, $serialize = true)
  74. {
  75. $iv = random_bytes(openssl_cipher_iv_length($this->cipher));
  76. // First we will encrypt the value using OpenSSL. After this is encrypted we
  77. // will proceed to calculating a MAC for the encrypted value so that this
  78. // value can be verified later as not having been changed by the users.
  79. $value = \openssl_encrypt(
  80. $serialize ? serialize($value) : $value,
  81. $this->cipher, $this->key, 0, $iv
  82. );
  83. if ($value === false) {
  84. throw new EncryptException('Could not encrypt the data.');
  85. }
  86. // Once we get the encrypted value we'll go ahead and base64_encode the input
  87. // vector and create the MAC for the encrypted value so we can then verify
  88. // its authenticity. Then, we'll JSON the data into the "payload" array.
  89. $mac = $this->hash($iv = base64_encode($iv), $value);
  90. $json = json_encode(compact('iv', 'value', 'mac'), JSON_UNESCAPED_SLASHES);
  91. if (json_last_error() !== JSON_ERROR_NONE) {
  92. throw new EncryptException('Could not encrypt the data.');
  93. }
  94. return base64_encode($json);
  95. }
  96. /**
  97. * Encrypt a string without serialization.
  98. *
  99. * @param string $value
  100. * @return string
  101. *
  102. * @throws \Illuminate\Contracts\Encryption\EncryptException
  103. */
  104. public function encryptString($value)
  105. {
  106. return $this->encrypt($value, false);
  107. }
  108. /**
  109. * Decrypt the given value.
  110. *
  111. * @param string $payload
  112. * @param bool $unserialize
  113. * @return mixed
  114. *
  115. * @throws \Illuminate\Contracts\Encryption\DecryptException
  116. */
  117. public function decrypt($payload, $unserialize = true)
  118. {
  119. $payload = $this->getJsonPayload($payload);
  120. $iv = base64_decode($payload['iv']);
  121. // Here we will decrypt the value. If we are able to successfully decrypt it
  122. // we will then unserialize it and return it out to the caller. If we are
  123. // unable to decrypt this value we will throw out an exception message.
  124. $decrypted = \openssl_decrypt(
  125. $payload['value'], $this->cipher, $this->key, 0, $iv
  126. );
  127. if ($decrypted === false) {
  128. throw new DecryptException('Could not decrypt the data.');
  129. }
  130. return $unserialize ? unserialize($decrypted) : $decrypted;
  131. }
  132. /**
  133. * Decrypt the given string without unserialization.
  134. *
  135. * @param string $payload
  136. * @return string
  137. *
  138. * @throws \Illuminate\Contracts\Encryption\DecryptException
  139. */
  140. public function decryptString($payload)
  141. {
  142. return $this->decrypt($payload, false);
  143. }
  144. /**
  145. * Create a MAC for the given value.
  146. *
  147. * @param string $iv
  148. * @param mixed $value
  149. * @return string
  150. */
  151. protected function hash($iv, $value)
  152. {
  153. return hash_hmac('sha256', $iv.$value, $this->key);
  154. }
  155. /**
  156. * Get the JSON array from the given payload.
  157. *
  158. * @param string $payload
  159. * @return array
  160. *
  161. * @throws \Illuminate\Contracts\Encryption\DecryptException
  162. */
  163. protected function getJsonPayload($payload)
  164. {
  165. $payload = json_decode(base64_decode($payload), true);
  166. // If the payload is not valid JSON or does not have the proper keys set we will
  167. // assume it is invalid and bail out of the routine since we will not be able
  168. // to decrypt the given value. We'll also check the MAC for this encryption.
  169. if (! $this->validPayload($payload)) {
  170. throw new DecryptException('The payload is invalid.');
  171. }
  172. if (! $this->validMac($payload)) {
  173. throw new DecryptException('The MAC is invalid.');
  174. }
  175. return $payload;
  176. }
  177. /**
  178. * Verify that the encryption payload is valid.
  179. *
  180. * @param mixed $payload
  181. * @return bool
  182. */
  183. protected function validPayload($payload)
  184. {
  185. return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac']) &&
  186. strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length($this->cipher);
  187. }
  188. /**
  189. * Determine if the MAC for the given payload is valid.
  190. *
  191. * @param array $payload
  192. * @return bool
  193. */
  194. protected function validMac(array $payload)
  195. {
  196. return hash_equals(
  197. $this->hash($payload['iv'], $payload['value']), $payload['mac']
  198. );
  199. }
  200. /**
  201. * Get the encryption key.
  202. *
  203. * @return string
  204. */
  205. public function getKey()
  206. {
  207. return $this->key;
  208. }
  209. }