Mcrypt.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Encryption\Adapter;
  8. /**
  9. * Mcrypt adapter for decrypting values using legacy ciphers
  10. */
  11. class Mcrypt implements EncryptionAdapterInterface
  12. {
  13. /**
  14. * @var string
  15. */
  16. private $cipher;
  17. /**
  18. * @var string
  19. */
  20. private $mode;
  21. /**
  22. * @var string
  23. */
  24. private $initVector;
  25. /**
  26. * Encryption algorithm module handle
  27. *
  28. * @var resource
  29. */
  30. private $handle;
  31. /**
  32. * Mcrypt constructor.
  33. * @param string $key
  34. * @param string $cipher
  35. * @param string $mode
  36. * @param string $initVector
  37. * @throws \Exception
  38. */
  39. public function __construct(
  40. string $key,
  41. string $cipher = MCRYPT_BLOWFISH,
  42. string $mode = MCRYPT_MODE_ECB,
  43. string $initVector = null
  44. ) {
  45. $this->cipher = $cipher;
  46. $this->mode = $mode;
  47. // @codingStandardsIgnoreLine
  48. $this->handle = @mcrypt_module_open($cipher, '', $mode, '');
  49. try {
  50. // @codingStandardsIgnoreLine
  51. $maxKeySize = @mcrypt_enc_get_key_size($this->handle);
  52. if (strlen($key) > $maxKeySize) {
  53. throw new \Magento\Framework\Exception\LocalizedException(
  54. new \Magento\Framework\Phrase('Key must not exceed %1 bytes.', [$maxKeySize])
  55. );
  56. }
  57. // @codingStandardsIgnoreLine
  58. $initVectorSize = @mcrypt_enc_get_iv_size($this->handle);
  59. if (null === $initVector) {
  60. /* Set vector to zero bytes to not use it */
  61. $initVector = str_repeat("\0", $initVectorSize);
  62. } elseif (!is_string($initVector) || strlen($initVector) != $initVectorSize) {
  63. throw new \Magento\Framework\Exception\LocalizedException(
  64. new \Magento\Framework\Phrase(
  65. 'Init vector must be a string of %1 bytes.',
  66. [$initVectorSize]
  67. )
  68. );
  69. }
  70. $this->initVector = $initVector;
  71. } catch (\Exception $e) {
  72. // @codingStandardsIgnoreLine
  73. @mcrypt_module_close($this->handle);
  74. throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase($e->getMessage()));
  75. }
  76. // @codingStandardsIgnoreLine
  77. @mcrypt_generic_init($this->handle, $key, $initVector);
  78. }
  79. /**
  80. * Destructor frees allocated resources
  81. */
  82. public function __destruct()
  83. {
  84. // @codingStandardsIgnoreStart
  85. @mcrypt_generic_deinit($this->handle);
  86. @mcrypt_module_close($this->handle);
  87. // @codingStandardsIgnoreEnd
  88. }
  89. /**
  90. * Retrieve a name of currently used cryptographic algorithm
  91. *
  92. * @return string
  93. */
  94. public function getCipher(): string
  95. {
  96. return $this->cipher;
  97. }
  98. /**
  99. * Mode in which cryptographic algorithm is running
  100. *
  101. * @return string
  102. */
  103. public function getMode(): string
  104. {
  105. return $this->mode;
  106. }
  107. /**
  108. * Retrieve an actual value of initial vector that has been used to initialize a cipher
  109. *
  110. * @return string
  111. */
  112. public function getInitVector(): ?string
  113. {
  114. return $this->initVector;
  115. }
  116. /**
  117. * Get the current mcrypt handle
  118. *
  119. * @return resource
  120. */
  121. public function getHandle()
  122. {
  123. return $this->handle;
  124. }
  125. /**
  126. * Encrypt a string
  127. *
  128. * @param string $data String to encrypt
  129. * @return string
  130. * @throws \Exception
  131. */
  132. public function encrypt(string $data): string
  133. {
  134. if (strlen($data) == 0) {
  135. return $data;
  136. }
  137. // @codingStandardsIgnoreLine
  138. return @mcrypt_generic($this->getHandle(), $data);
  139. }
  140. /**
  141. * Decrypt a string
  142. *
  143. * @param string $data
  144. * @return string
  145. */
  146. public function decrypt(string $data): string
  147. {
  148. if (strlen($data) == 0) {
  149. return $data;
  150. }
  151. // @codingStandardsIgnoreLine
  152. $data = @mdecrypt_generic($this->handle, $data);
  153. /*
  154. * Returned string can in fact be longer than the unencrypted string due to the padding of the data
  155. * @link http://www.php.net/manual/en/function.mdecrypt-generic.php
  156. */
  157. $data = rtrim($data, "\0");
  158. return $data;
  159. }
  160. }