Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Crypt.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
18 class Crypt
19 {
23  protected $_cipher;
24 
28  protected $_mode;
29 
33  protected $_initVector;
34 
40  private $mcrypt;
41 
54  public function __construct(
55  $key,
56  $cipher = MCRYPT_BLOWFISH,
57  $mode = MCRYPT_MODE_ECB,
58  $initVector = false
59  ) {
60  if (true === $initVector) {
61  // @codingStandardsIgnoreStart
62  $handle = @mcrypt_module_open($cipher, '', $mode, '');
63  $initVectorSize = @mcrypt_enc_get_iv_size($handle);
64  // @codingStandardsIgnoreEnd
65 
66  /* Generate a random vector from human-readable characters */
67  $allowedCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
68  $initVector = '';
69  for ($i = 0; $i < $initVectorSize; $i++) {
70  $initVector .= $allowedCharacters[rand(0, strlen($allowedCharacters) - 1)];
71  }
72  // @codingStandardsIgnoreStart
73  @mcrypt_generic_deinit($handle);
74  @mcrypt_module_close($handle);
75  // @codingStandardsIgnoreEnd
76  }
77 
78  $this->mcrypt = new \Magento\Framework\Encryption\Adapter\Mcrypt(
79  $key,
80  $cipher,
81  $mode,
82  $initVector === false ? null : $initVector
83  );
84  }
85 
91  public function getCipher()
92  {
93  return $this->mcrypt->getCipher();
94  }
95 
101  public function getMode()
102  {
103  return $this->mcrypt->getMode();
104  }
105 
111  public function getInitVector()
112  {
113  return $this->mcrypt->getInitVector();
114  }
115 
122  public function encrypt($data)
123  {
124  if (strlen($data) == 0) {
125  return $data;
126  }
127  // @codingStandardsIgnoreLine
128  return @mcrypt_generic($this->mcrypt->getHandle(), $data);
129  }
130 
137  public function decrypt($data)
138  {
139  return $this->mcrypt->decrypt($data);
140  }
141 }
$initVector
__construct( $key, $cipher=MCRYPT_BLOWFISH, $mode=MCRYPT_MODE_ECB, $initVector=false)
Definition: Crypt.php:54
$initVectorSize
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$handle
$i
Definition: gallery.phtml:31