Crypt.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  8. /**
  9. * Class encapsulates cryptographic algorithm
  10. *
  11. * @api
  12. * @deprecated 102.0.0
  13. * @since 100.0.2
  14. */
  15. class Crypt
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $_cipher;
  21. /**
  22. * @var string
  23. */
  24. protected $_mode;
  25. /**
  26. * @var string
  27. */
  28. protected $_initVector;
  29. /**
  30. * Mcrypt adapter
  31. *
  32. * @var \Magento\Framework\Encryption\Adapter\Mcrypt
  33. */
  34. private $mcrypt;
  35. /**
  36. * Constructor
  37. *
  38. * @param string $key Secret encryption key.
  39. * It's unsafe to store encryption key in memory, so no getter for key exists.
  40. * @param string $cipher Cipher algorithm (one of the MCRYPT_ciphername constants)
  41. * @param string $mode Mode of cipher algorithm (MCRYPT_MODE_modeabbr constants)
  42. * @param string|bool $initVector Initial vector to fill algorithm blocks.
  43. * TRUE generates a random initial vector.
  44. * FALSE fills initial vector with zero bytes to not use it.
  45. * @throws \Exception
  46. */
  47. public function __construct(
  48. $key,
  49. $cipher = MCRYPT_BLOWFISH,
  50. $mode = MCRYPT_MODE_ECB,
  51. $initVector = false
  52. ) {
  53. if (true === $initVector) {
  54. // @codingStandardsIgnoreStart
  55. $handle = @mcrypt_module_open($cipher, '', $mode, '');
  56. $initVectorSize = @mcrypt_enc_get_iv_size($handle);
  57. // @codingStandardsIgnoreEnd
  58. /* Generate a random vector from human-readable characters */
  59. $allowedCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  60. $initVector = '';
  61. for ($i = 0; $i < $initVectorSize; $i++) {
  62. $initVector .= $allowedCharacters[rand(0, strlen($allowedCharacters) - 1)];
  63. }
  64. // @codingStandardsIgnoreStart
  65. @mcrypt_generic_deinit($handle);
  66. @mcrypt_module_close($handle);
  67. // @codingStandardsIgnoreEnd
  68. }
  69. $this->mcrypt = new \Magento\Framework\Encryption\Adapter\Mcrypt(
  70. $key,
  71. $cipher,
  72. $mode,
  73. $initVector === false ? null : $initVector
  74. );
  75. }
  76. /**
  77. * Retrieve a name of currently used cryptographic algorithm
  78. *
  79. * @return string
  80. */
  81. public function getCipher()
  82. {
  83. return $this->mcrypt->getCipher();
  84. }
  85. /**
  86. * Mode in which cryptographic algorithm is running
  87. *
  88. * @return string
  89. */
  90. public function getMode()
  91. {
  92. return $this->mcrypt->getMode();
  93. }
  94. /**
  95. * Retrieve an actual value of initial vector that has been used to initialize a cipher
  96. *
  97. * @return string
  98. */
  99. public function getInitVector()
  100. {
  101. return $this->mcrypt->getInitVector();
  102. }
  103. /**
  104. * Encrypt a data
  105. *
  106. * @param string $data String to encrypt
  107. * @return string
  108. */
  109. public function encrypt($data)
  110. {
  111. if (strlen($data) == 0) {
  112. return $data;
  113. }
  114. // @codingStandardsIgnoreLine
  115. return @mcrypt_generic($this->mcrypt->getHandle(), $data);
  116. }
  117. /**
  118. * Decrypt a data
  119. *
  120. * @param string $data String to decrypt
  121. * @return string
  122. */
  123. public function decrypt($data)
  124. {
  125. return $this->mcrypt->decrypt($data);
  126. }
  127. }