@@ -32,11 +32,11 @@ class Crypt
32
32
protected $ _initVector ;
33
33
34
34
/**
35
- * Encryption algorithm module handle
35
+ * Mcrypt adapter
36
36
*
37
- * @var resource
37
+ * @var \Magento\Framework\Encryption\Adapter\Mcrypt
38
38
*/
39
- protected $ _handle ;
39
+ private $ mcrypt ;
40
40
41
41
/**
42
42
* Constructor
@@ -56,64 +56,30 @@ public function __construct(
56
56
$ mode = MCRYPT_MODE_ECB ,
57
57
$ initVector = false
58
58
) {
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 ) {
65
60
// @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 );
67
63
// @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 )];
93
70
}
94
- $ this ->_initVector = $ initVector ;
95
- } catch (\Exception $ e ) {
96
71
// @codingStandardsIgnoreStart
97
- @mcrypt_module_close ($ this ->_handle );
72
+ @mcrypt_generic_deinit ($ handle );
73
+ @mcrypt_module_close ($ handle );
98
74
// @codingStandardsIgnoreEnd
99
- throw $ e ;
100
75
}
101
- // @codingStandardsIgnoreStart
102
- @mcrypt_generic_init ($ this ->_handle , $ key , $ initVector );
103
- // @codingStandardsIgnoreEnd
104
- }
105
76
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
+ );
117
83
}
118
84
119
85
/**
@@ -123,7 +89,7 @@ public function __destruct()
123
89
*/
124
90
public function getCipher ()
125
91
{
126
- return $ this ->_cipher ;
92
+ return $ this ->mcrypt -> getCipher () ;
127
93
}
128
94
129
95
/**
@@ -133,7 +99,7 @@ public function getCipher()
133
99
*/
134
100
public function getMode ()
135
101
{
136
- return $ this ->_mode ;
102
+ return $ this ->mcrypt -> getMode () ;
137
103
}
138
104
139
105
/**
@@ -143,7 +109,7 @@ public function getMode()
143
109
*/
144
110
public function getInitVector ()
145
111
{
146
- return $ this ->_initVector ;
112
+ return $ this ->mcrypt -> getInitVector () ;
147
113
}
148
114
149
115
/**
@@ -157,9 +123,8 @@ public function encrypt($data)
157
123
if (strlen ($ data ) == 0 ) {
158
124
return $ data ;
159
125
}
160
- // @codingStandardsIgnoreStart
161
- return @mcrypt_generic ($ this ->_handle , $ data );
162
- // @codingStandardsIgnoreEnd
126
+ // @codingStandardsIgnoreLine
127
+ return @mcrypt_generic ($ this ->mcrypt ->getHandle (), $ data );
163
128
}
164
129
165
130
/**
@@ -170,17 +135,6 @@ public function encrypt($data)
170
135
*/
171
136
public function decrypt ($ data )
172
137
{
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 );
185
139
}
186
140
}
0 commit comments