Skip to content

Commit ae3f454

Browse files
committed
Refactor Crypt class to use Mcrypt Adapter
Removes code duplication between `Crypt` and `Mcrpyt` classes. `Mcrpyt` is used for all equivalent `Crypt` methods except `Crypt::encrypt` becuase `Mcrypt::encrpt` throws an exception preventing it's use.
1 parent 0d90f00 commit ae3f454

File tree

3 files changed

+41
-74
lines changed

3 files changed

+41
-74
lines changed

lib/internal/Magento/Framework/Encryption/Adapter/Mcrypt.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Mcrypt implements EncryptionAdapterInterface
2525
private $initVector;
2626

2727
/**
28+
* Encryption algorithm module handle
29+
*
2830
* @var resource
2931
*/
3032
private $handle;
@@ -119,6 +121,16 @@ public function getInitVector(): ?string
119121
return $this->initVector;
120122
}
121123

124+
/**
125+
* Get the current mcrypt handle
126+
*
127+
* @return resource
128+
*/
129+
public function getHandle()
130+
{
131+
return $this->handle;
132+
}
133+
122134
/**
123135
* Encrypt a string
124136
*

lib/internal/Magento/Framework/Encryption/Crypt.php

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class Crypt
3232
protected $_initVector;
3333

3434
/**
35-
* Encryption algorithm module handle
35+
* Mcrypt adapter
3636
*
37-
* @var resource
37+
* @var \Magento\Framework\Encryption\Adapter\Mcrypt
3838
*/
39-
protected $_handle;
39+
private $mcrypt;
4040

4141
/**
4242
* Constructor
@@ -56,64 +56,30 @@ public function __construct(
5656
$mode = MCRYPT_MODE_ECB,
5757
$initVector = false
5858
) {
59-
$this->_cipher = $cipher;
60-
$this->_mode = $mode;
61-
// @codingStandardsIgnoreStart
62-
$this->_handle = @mcrypt_module_open($cipher, '', $mode, '');
63-
// @codingStandardsIgnoreEnd
64-
try {
59+
if (true === $initVector) {
6560
// @codingStandardsIgnoreStart
66-
$maxKeySize = @mcrypt_enc_get_key_size($this->_handle);
61+
$handle = @mcrypt_module_open($cipher, '', $mode, '');
62+
$initVectorSize = @mcrypt_enc_get_iv_size($handle);
6763
// @codingStandardsIgnoreEnd
68-
if (strlen((string)$key) > $maxKeySize) {
69-
throw new \Magento\Framework\Exception\LocalizedException(
70-
new \Magento\Framework\Phrase('Key must not exceed %1 bytes.', [$maxKeySize])
71-
);
72-
}
73-
// @codingStandardsIgnoreStart
74-
$initVectorSize = @mcrypt_enc_get_iv_size($this->_handle);
75-
// @codingStandardsIgnoreEnd
76-
if (true === $initVector) {
77-
/* Generate a random vector from human-readable characters */
78-
$abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
79-
$initVector = '';
80-
for ($i = 0; $i < $initVectorSize; $i++) {
81-
$initVector .= $abc[rand(0, strlen($abc) - 1)];
82-
}
83-
} elseif (false === $initVector) {
84-
/* Set vector to zero bytes to not use it */
85-
$initVector = str_repeat("\0", $initVectorSize);
86-
} elseif (!is_string($initVector) || strlen($initVector) != $initVectorSize) {
87-
throw new \Magento\Framework\Exception\LocalizedException(
88-
new \Magento\Framework\Phrase(
89-
'Init vector must be a string of %1 bytes.',
90-
[$initVectorSize]
91-
)
92-
);
64+
65+
/* Generate a random vector from human-readable characters */
66+
$allowedCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
67+
$initVector = '';
68+
for ($i = 0; $i < $initVectorSize; $i++) {
69+
$initVector .= $allowedCharacters[random_int(0, strlen($allowedCharacters) - 1)];
9370
}
94-
$this->_initVector = $initVector;
95-
} catch (\Exception $e) {
9671
// @codingStandardsIgnoreStart
97-
@mcrypt_module_close($this->_handle);
72+
@mcrypt_generic_deinit($handle);
73+
@mcrypt_module_close($handle);
9874
// @codingStandardsIgnoreEnd
99-
throw $e;
10075
}
101-
// @codingStandardsIgnoreStart
102-
@mcrypt_generic_init($this->_handle, $key, $initVector);
103-
// @codingStandardsIgnoreEnd
104-
}
10576

106-
/**
107-
* Destructor frees allocated resources
108-
*/
109-
public function __destruct()
110-
{
111-
// @codingStandardsIgnoreStart
112-
@mcrypt_generic_deinit($this->_handle);
113-
// @codingStandardsIgnoreEnd
114-
// @codingStandardsIgnoreStart
115-
@mcrypt_module_close($this->_handle);
116-
// @codingStandardsIgnoreEnd
77+
$this->mcrypt = new \Magento\Framework\Encryption\Adapter\Mcrypt(
78+
$key,
79+
$cipher,
80+
$mode,
81+
$initVector === false ? null : $initVector
82+
);
11783
}
11884

11985
/**
@@ -123,7 +89,7 @@ public function __destruct()
12389
*/
12490
public function getCipher()
12591
{
126-
return $this->_cipher;
92+
return $this->mcrypt->getCipher();
12793
}
12894

12995
/**
@@ -133,7 +99,7 @@ public function getCipher()
13399
*/
134100
public function getMode()
135101
{
136-
return $this->_mode;
102+
return $this->mcrypt->getMode();
137103
}
138104

139105
/**
@@ -143,7 +109,7 @@ public function getMode()
143109
*/
144110
public function getInitVector()
145111
{
146-
return $this->_initVector;
112+
return $this->mcrypt->getInitVector();
147113
}
148114

149115
/**
@@ -157,9 +123,8 @@ public function encrypt($data)
157123
if (strlen($data) == 0) {
158124
return $data;
159125
}
160-
// @codingStandardsIgnoreStart
161-
return @mcrypt_generic($this->_handle, $data);
162-
// @codingStandardsIgnoreEnd
126+
// @codingStandardsIgnoreLine
127+
return @mcrypt_generic($this->mcrypt->getHandle(), $data);
163128
}
164129

165130
/**
@@ -170,17 +135,6 @@ public function encrypt($data)
170135
*/
171136
public function decrypt($data)
172137
{
173-
if (strlen($data) == 0) {
174-
return $data;
175-
}
176-
// @codingStandardsIgnoreStart
177-
$data = @mdecrypt_generic($this->_handle, $data);
178-
// @codingStandardsIgnoreEnd
179-
/*
180-
* Returned string can in fact be longer than the unencrypted string due to the padding of the data
181-
* @link http://www.php.net/manual/en/function.mdecrypt-generic.php
182-
*/
183-
$data = rtrim($data, "\0");
184-
return $data;
138+
return $this->mcrypt->decrypt($data);
185139
}
186140
}

lib/internal/Magento/Framework/Encryption/Test/Unit/CryptTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function testConstructor($cipher, $mode)
8585

8686
public function getConstructorExceptionData()
8787
{
88+
$key = substr(__CLASS__, -32, 32);
8889
$result = [];
8990
foreach (self::SUPPORTED_CIPHER_MODE_COMBINATIONS as $cipher => $modes) {
9091
/** @var array $modes */
@@ -94,8 +95,8 @@ public function getConstructorExceptionData()
9495
$tooLongInitVector = str_repeat('-', $this->_getInitVectorSize($cipher, $mode) + 1);
9596
$result['tooLongKey-' . $cipher . '-' . $mode . '-false'] = [$tooLongKey, $cipher, $mode, false];
9697
$keyPrefix = 'key-' . $cipher . '-' . $mode;
97-
$result[$keyPrefix . '-tooShortInitVector'] = [$this->_key, $cipher, $mode, $tooShortInitVector];
98-
$result[$keyPrefix . '-tooLongInitVector'] = [$this->_key, $cipher, $mode, $tooLongInitVector];
98+
$result[$keyPrefix . '-tooShortInitVector'] = [$key, $cipher, $mode, $tooShortInitVector];
99+
$result[$keyPrefix . '-tooLongInitVector'] = [$key, $cipher, $mode, $tooLongInitVector];
99100
}
100101
}
101102
return $result;

0 commit comments

Comments
 (0)