Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mcrypt.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 {
19  private $cipher;
20 
24  private $mode;
25 
29  private $initVector;
30 
36  private $handle;
37 
46  public function __construct(
47  string $key,
48  string $cipher = MCRYPT_BLOWFISH,
49  string $mode = MCRYPT_MODE_ECB,
50  string $initVector = null
51  ) {
52  $this->cipher = $cipher;
53  $this->mode = $mode;
54  // @codingStandardsIgnoreLine
55  $this->handle = @mcrypt_module_open($cipher, '', $mode, '');
56  try {
57  // @codingStandardsIgnoreLine
58  $maxKeySize = @mcrypt_enc_get_key_size($this->handle);
59  if (strlen($key) > $maxKeySize) {
60  throw new \Magento\Framework\Exception\LocalizedException(
61  new \Magento\Framework\Phrase('Key must not exceed %1 bytes.', [$maxKeySize])
62  );
63  }
64  // @codingStandardsIgnoreLine
65  $initVectorSize = @mcrypt_enc_get_iv_size($this->handle);
66  if (null === $initVector) {
67  /* Set vector to zero bytes to not use it */
68  $initVector = str_repeat("\0", $initVectorSize);
69  } elseif (!is_string($initVector) || strlen($initVector) != $initVectorSize) {
70  throw new \Magento\Framework\Exception\LocalizedException(
71  new \Magento\Framework\Phrase(
72  'Init vector must be a string of %1 bytes.',
74  )
75  );
76  }
77  $this->initVector = $initVector;
78  } catch (\Exception $e) {
79  // @codingStandardsIgnoreLine
80  @mcrypt_module_close($this->handle);
81  throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase($e->getMessage()));
82  }
83  // @codingStandardsIgnoreLine
84  @mcrypt_generic_init($this->handle, $key, $initVector);
85  }
86 
90  public function __destruct()
91  {
92  // @codingStandardsIgnoreStart
93  @mcrypt_generic_deinit($this->handle);
94  @mcrypt_module_close($this->handle);
95  // @codingStandardsIgnoreEnd
96  }
97 
103  public function getCipher(): string
104  {
105  return $this->cipher;
106  }
107 
113  public function getMode(): string
114  {
115  return $this->mode;
116  }
117 
123  public function getInitVector(): ?string
124  {
125  return $this->initVector;
126  }
127 
133  public function getHandle()
134  {
135  return $this->handle;
136  }
137 
145  public function encrypt(string $data): string
146  {
147  if (strlen($data) == 0) {
148  return $data;
149  }
150  // @codingStandardsIgnoreLine
151  return @mcrypt_generic($this->getHandle(), $data);
152  }
153 
160  public function decrypt(string $data): string
161  {
162  if (strlen($data) == 0) {
163  return $data;
164  }
165  // @codingStandardsIgnoreLine
166  $data = @mdecrypt_generic($this->handle, $data);
167  /*
168  * Returned string can in fact be longer than the unencrypted string due to the padding of the data
169  * @link http://www.php.net/manual/en/function.mdecrypt-generic.php
170  */
171  $data = rtrim($data, "\0");
172  return $data;
173  }
174 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$initVectorSize
__construct(string $key, string $cipher=MCRYPT_BLOWFISH, string $mode=MCRYPT_MODE_ECB, string $initVector=null)
Definition: Mcrypt.php:46