-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Crypt.php
183 lines (171 loc) · 5.28 KB
/
Crypt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Encryption;
/**
* Class encapsulates cryptographic algorithm
*
* @api
*/
class Crypt
{
/**
* @var string
*/
protected $_cipher;
/**
* @var string
*/
protected $_mode;
/**
* @var string
*/
protected $_initVector;
/**
* Encryption algorithm module handle
*
* @var resource
*/
protected $_handle;
/**
* Constructor
*
* @param string $key Secret encryption key.
* It's unsafe to store encryption key in memory, so no getter for key exists.
* @param string $cipher Cipher algorithm (one of the MCRYPT_ciphername constants)
* @param string $mode Mode of cipher algorithm (MCRYPT_MODE_modeabbr constants)
* @param string|bool $initVector Initial vector to fill algorithm blocks.
* TRUE generates a random initial vector.
* FALSE fills initial vector with zero bytes to not use it.
* @throws \Exception
*/
public function __construct(
$key,
$cipher = MCRYPT_BLOWFISH,
$mode = MCRYPT_MODE_ECB,
$initVector = false
) {
$this->_cipher = $cipher;
$this->_mode = $mode;
// @codingStandardsIgnoreStart
$this->_handle = @mcrypt_module_open($cipher, '', $mode, '');
// @codingStandardsIgnoreEnd
try {
// @codingStandardsIgnoreStart
$maxKeySize = @mcrypt_enc_get_key_size($this->_handle);
// @codingStandardsIgnoreEnd
if (strlen($key) > $maxKeySize) {
throw new \Magento\Framework\Exception\LocalizedException(
new \Magento\Framework\Phrase('Key must not exceed %1 bytes.', [$maxKeySize])
);
}
// @codingStandardsIgnoreStart
$initVectorSize = @mcrypt_enc_get_iv_size($this->_handle);
// @codingStandardsIgnoreEnd
if (true === $initVector) {
/* Generate a random vector from human-readable characters */
$abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$initVector = '';
for ($i = 0; $i < $initVectorSize; $i++) {
$initVector .= $abc[rand(0, strlen($abc) - 1)];
}
} elseif (false === $initVector) {
/* Set vector to zero bytes to not use it */
$initVector = str_repeat("\0", $initVectorSize);
} elseif (!is_string($initVector) || strlen($initVector) != $initVectorSize) {
throw new \Magento\Framework\Exception\LocalizedException(
new \Magento\Framework\Phrase(
'Init vector must be a string of %1 bytes.',
[$initVectorSize]
)
);
}
$this->_initVector = $initVector;
} catch (\Exception $e) {
// @codingStandardsIgnoreStart
@mcrypt_module_close($this->_handle);
// @codingStandardsIgnoreEnd
throw $e;
}
// @codingStandardsIgnoreStart
@mcrypt_generic_init($this->_handle, $key, $initVector);
// @codingStandardsIgnoreEnd
}
/**
* Destructor frees allocated resources
*/
public function __destruct()
{
// @codingStandardsIgnoreStart
@mcrypt_generic_deinit($this->_handle);
// @codingStandardsIgnoreEnd
// @codingStandardsIgnoreStart
@mcrypt_module_close($this->_handle);
// @codingStandardsIgnoreEnd
}
/**
* Retrieve a name of currently used cryptographic algorithm
*
* @return string
*/
public function getCipher()
{
return $this->_cipher;
}
/**
* Mode in which cryptographic algorithm is running
*
* @return string
*/
public function getMode()
{
return $this->_mode;
}
/**
* Retrieve an actual value of initial vector that has been used to initialize a cipher
*
* @return string
*/
public function getInitVector()
{
return $this->_initVector;
}
/**
* Encrypt a data
*
* @param string $data String to encrypt
* @return string
*/
public function encrypt($data)
{
if (strlen($data) == 0) {
return $data;
}
// @codingStandardsIgnoreStart
return @mcrypt_generic($this->_handle, $data);
// @codingStandardsIgnoreEnd
}
/**
* Decrypt a data
*
* @param string $data String to decrypt
* @return string
*/
public function decrypt($data)
{
if (strlen($data) == 0) {
return $data;
}
// @codingStandardsIgnoreStart
$data = @mdecrypt_generic($this->_handle, $data);
// @codingStandardsIgnoreEnd
/*
* Returned string can in fact be longer than the unencrypted string due to the padding of the data
* @link http://www.php.net/manual/en/function.mdecrypt-generic.php
*/
$data = rtrim($data, "\0");
return $data;
}
}