Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes
Zend_Filter_Encrypt_Mcrypt Class Reference
Inheritance diagram for Zend_Filter_Encrypt_Mcrypt:
Zend_Filter_Encrypt_Interface

Public Member Functions

 __construct ($options)
 
 getEncryption ()
 
 setEncryption ($options)
 
 getVector ()
 
 setVector ($vector=null)
 
 getCompression ()
 
 setCompression ($compression)
 
 encrypt ($value)
 
 decrypt ($value)
 
 toString ()
 

Protected Member Functions

 _openCipher ()
 
 _closeCipher ($cipher)
 
 _initCipher ($cipher)
 
 _srand ()
 

Protected Attributes

 $_encryption
 
 $_compression
 

Static Protected Attributes

static $_srandCalled = false
 

Detailed Description

Definition at line 38 of file Mcrypt.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options)

Class constructor

Parameters
string | array | Zend_Config$optionsCryption Options

Definition at line 74 of file Mcrypt.php.

75  {
76  if (!extension_loaded('mcrypt')) {
77  #require_once 'Zend/Filter/Exception.php';
78  throw new Zend_Filter_Exception('This filter needs the mcrypt extension');
79  }
80 
81  if ($options instanceof Zend_Config) {
82  $options = $options->toArray();
83  } elseif (is_string($options)) {
84  $options = array('key' => $options);
85  } elseif (!is_array($options)) {
86  #require_once 'Zend/Filter/Exception.php';
87  throw new Zend_Filter_Exception('Invalid options argument provided to filter');
88  }
89 
90  if (array_key_exists('compression', $options)) {
91  $this->setCompression($options['compression']);
92  unset($options['compress']);
93  }
94 
95  $this->setEncryption($options);
96  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setCompression($compression)
Definition: Mcrypt.php:214

Member Function Documentation

◆ _closeCipher()

_closeCipher (   $cipher)
protected

Close a cipher

Parameters
resource$cipherCipher to close
Returns
Zend_Filter_Encrypt_Mcrypt

Definition at line 314 of file Mcrypt.php.

315  {
316  mcrypt_module_close($cipher);
317 
318  return $this;
319  }

◆ _initCipher()

_initCipher (   $cipher)
protected

Initialises the cipher with the set key

Parameters
resource$cipher
Exceptions

Definition at line 328 of file Mcrypt.php.

329  {
330  $key = $this->_encryption['key'];
331 
332  $keysizes = mcrypt_enc_get_supported_key_sizes($cipher);
333  if (empty($keysizes) || ($this->_encryption['salt'] == true)) {
334  $this->_srand();
335  $keysize = mcrypt_enc_get_key_size($cipher);
336  $key = substr(md5($key), 0, $keysize);
337  } else if (!in_array(strlen($key), $keysizes)) {
338  #require_once 'Zend/Filter/Exception.php';
339  throw new Zend_Filter_Exception('The given key has a wrong size for the set algorithm');
340  }
341 
342  $result = mcrypt_generic_init($cipher, $key, $this->_encryption['vector']);
343  if ($result < 0) {
344  #require_once 'Zend/Filter/Exception.php';
345  throw new Zend_Filter_Exception('Mcrypt could not be initialize with the given setting');
346  }
347 
348  return $this;
349  }

◆ _openCipher()

_openCipher ( )
protected

Open a cipher

Exceptions
Zend_Filter_ExceptionWhen the cipher can not be opened
Returns
resource Returns the opened cipher

Definition at line 292 of file Mcrypt.php.

293  {
294  $cipher = mcrypt_module_open(
295  $this->_encryption['algorithm'],
296  $this->_encryption['algorithm_directory'],
297  $this->_encryption['mode'],
298  $this->_encryption['mode_directory']);
299 
300  if ($cipher === false) {
301  #require_once 'Zend/Filter/Exception.php';
302  throw new Zend_Filter_Exception('Mcrypt can not be opened with your settings');
303  }
304 
305  return $cipher;
306  }

◆ _srand()

_srand ( )
protected

_srand() interception

See also
ZF-8742

Definition at line 356 of file Mcrypt.php.

357  {
358  if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
359  return;
360  }
361  if (!self::$_srandCalled) {
362  srand(Zend_Crypt_Math::randInteger(0, PHP_INT_MAX));
363  self::$_srandCalled = true;
364  }
365  }
static randInteger($min, $max, $strong=false)
Definition: Math.php:117

◆ decrypt()

decrypt (   $value)

Defined by Zend_Filter_Interface

Decrypts $value with the defined settings

Parameters
string$valueContent to decrypt
Returns
string The decrypted content

Implements Zend_Filter_Encrypt_Interface.

Definition at line 258 of file Mcrypt.php.

259  {
260  $cipher = $this->_openCipher();
261  $this->_initCipher($cipher);
262  $decrypted = mdecrypt_generic($cipher, $value);
263  mcrypt_generic_deinit($cipher);
264  $this->_closeCipher($cipher);
265 
266  // decompress after decryption
267  if (!empty($this->_compression)) {
268  #require_once 'Zend/Filter/Decompress.php';
269  $decompress = new Zend_Filter_Decompress($this->_compression);
270  $decrypted = $decompress->filter($decrypted);
271  }
272 
273  return $decrypted;
274  }
$value
Definition: gender.phtml:16

◆ encrypt()

encrypt (   $value)

Defined by Zend_Filter_Interface

Encrypts $value with the defined settings

Parameters
string$valueThe content to encrypt
Returns
string The encrypted content

Implements Zend_Filter_Encrypt_Interface.

Definition at line 232 of file Mcrypt.php.

233  {
234  // compress prior to encryption
235  if (!empty($this->_compression)) {
236  #require_once 'Zend/Filter/Compress.php';
237  $compress = new Zend_Filter_Compress($this->_compression);
238  $value = $compress->filter($value);
239  }
240 
241  $cipher = $this->_openCipher();
242  $this->_initCipher($cipher);
243  $encrypted = mcrypt_generic($cipher, $value);
244  mcrypt_generic_deinit($cipher);
245  $this->_closeCipher($cipher);
246 
247  return $encrypted;
248  }
$value
Definition: gender.phtml:16

◆ getCompression()

getCompression ( )

Returns the compression

Returns
array

Definition at line 203 of file Mcrypt.php.

204  {
205  return $this->_compression;
206  }

◆ getEncryption()

getEncryption ( )

Returns the set encryption options

Returns
array

Definition at line 103 of file Mcrypt.php.

104  {
105  return $this->_encryption;
106  }

◆ getVector()

getVector ( )

Returns the set vector

Returns
string

Definition at line 158 of file Mcrypt.php.

159  {
160  return $this->_encryption['vector'];
161  }

◆ setCompression()

setCompression (   $compression)

Sets a internal compression for values to encrypt

Parameters
string | array$compression
Returns
Zend_Filter_Encrypt_Mcrypt

Definition at line 214 of file Mcrypt.php.

215  {
216  if (is_string($this->_compression)) {
217  $compression = array('adapter' => $compression);
218  }
219 
220  $this->_compression = $compression;
221  return $this;
222  }

◆ setEncryption()

setEncryption (   $options)

Sets new encryption options

Parameters
string | array$optionsEncryption options
Returns
Zend_Filter_File_Encryption

Definition at line 114 of file Mcrypt.php.

115  {
116  if (is_string($options)) {
117  $options = array('key' => $options);
118  }
119 
120  if (!is_array($options)) {
121  #require_once 'Zend/Filter/Exception.php';
122  throw new Zend_Filter_Exception('Invalid options argument provided to filter');
123  }
124 
125  $options = $options + $this->getEncryption();
126  $algorithms = mcrypt_list_algorithms($options['algorithm_directory']);
127  if (!in_array($options['algorithm'], $algorithms)) {
128  #require_once 'Zend/Filter/Exception.php';
129  throw new Zend_Filter_Exception("The algorithm '{$options['algorithm']}' is not supported");
130  }
131 
132  $modes = mcrypt_list_modes($options['mode_directory']);
133  if (!in_array($options['mode'], $modes)) {
134  #require_once 'Zend/Filter/Exception.php';
135  throw new Zend_Filter_Exception("The mode '{$options['mode']}' is not supported");
136  }
137 
138  if (!mcrypt_module_self_test($options['algorithm'], $options['algorithm_directory'])) {
139  #require_once 'Zend/Filter/Exception.php';
140  throw new Zend_Filter_Exception('The given algorithm can not be used due an internal mcrypt problem');
141  }
142 
143  if (!isset($options['vector'])) {
144  $options['vector'] = null;
145  }
146 
147  $this->_encryption = $options;
148  $this->setVector($options['vector']);
149 
150  return $this;
151  }
setVector($vector=null)
Definition: Mcrypt.php:169

◆ setVector()

setVector (   $vector = null)

Sets the initialization vector

Parameters
string$vector(Optional) Vector to set
Returns
Zend_Filter_Encrypt_Mcrypt

Definition at line 169 of file Mcrypt.php.

170  {
171  $cipher = $this->_openCipher();
172  $size = mcrypt_enc_get_iv_size($cipher);
173  if (empty($vector)) {
174  $this->_srand();
175  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && version_compare(PHP_VERSION, '5.3.0', '<')) {
176  $method = MCRYPT_RAND;
177  } else {
178  if (file_exists('/dev/urandom') || (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')) {
179  $method = MCRYPT_DEV_URANDOM;
180  } elseif (file_exists('/dev/random')) {
181  $method = MCRYPT_DEV_RANDOM;
182  } else {
183  $method = MCRYPT_RAND;
184  }
185  }
186  $vector = mcrypt_create_iv($size, $method);
187  } else if (strlen($vector) != $size) {
188  #require_once 'Zend/Filter/Exception.php';
189  throw new Zend_Filter_Exception('The given vector has a wrong size for the set algorithm');
190  }
191 
192  $this->_encryption['vector'] = $vector;
193  $this->_closeCipher($cipher);
194 
195  return $this;
196  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$method
Definition: info.phtml:13

◆ toString()

toString ( )

Returns the adapter name

Returns
string

Definition at line 281 of file Mcrypt.php.

282  {
283  return 'Mcrypt';
284  }

Field Documentation

◆ $_compression

$_compression
protected

Definition at line 65 of file Mcrypt.php.

◆ $_encryption

$_encryption
protected
Initial value:
= array(
'key' => 'ZendFramework',
'algorithm' => 'blowfish',
'algorithm_directory' => '',
'mode' => 'cbc',
'mode_directory' => '',
'vector' => null,
'salt' => false
)

Definitions for encryption array( 'key' => encryption key string 'algorithm' => algorithm to use 'algorithm_directory' => directory where to find the algorithm 'mode' => encryption mode to use 'modedirectory' => directory where to find the mode )

Definition at line 50 of file Mcrypt.php.

◆ $_srandCalled

$_srandCalled = false
staticprotected

Definition at line 67 of file Mcrypt.php.


The documentation for this class was generated from the following file: