-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
253 lines (225 loc) · 5.38 KB
/
code.power
File metadata and controls
253 lines (225 loc) · 5.38 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* The Crypt AES FOF class
* replacement class
*
* @var FOF
* @since 3.2.0
*/
protected FOF $fof;
/**
* The Crypt AES CBC class
*
* @var Aes
* @since 3.2.0
*/
protected Aes $aes;
/**
* The Crypt AES Legacy class
*
* @var Legacy
* @since 3.2.0
*/
protected Legacy $legacy;
/**
* The Password class
*
* @var Password
* @since 3.2.0
*/
protected Password $password;
/**
* Active encryption options
*
* @var array
* @since 3.2.0
*/
protected array $options = [
'basic' => 'fof',
'medium' => 'fof',
'local' => 'aes'
];
/**
* Active passwords
*
* @var array
* @since 3.2.0
*/
protected array $passwords = [];
/**
* Constructor
*
* @param FOF $fof The FOF class
* @param Aes $aes The AES CBC class
* @param Legacy $legacy The AES Legacy class
* @param Password $password The Password class
*
* @since 3.2.0
*/
public function __construct(FOF $fof, Aes $aes, Legacy $legacy, Password $password)
{
$this->fof = $fof;
$this->aes = $aes;
$this->legacy = $legacy;
$this->password = $password;
}
/**
* Encrypt a string as needed
*
* @param string $string The string to encrypt
* @param string $method The encryption method to use
* @param string|null $password The password
*
* @return string
* @since 3.2.0
**/
public function encrypt(string $string, string $method,
?string $password = null): string
{
if (($password = $this->getPassword($method, $password)) !== null
&& ($name = $this->getClassName($method)) !== null)
{
return $this->{$name}->encrypt($string, $password);
}
return $string;
}
/**
* Decrypt a string as needed
*
* @param string $string The string to decrypt
* @param string $method The encryption method to use
* @param string|null $default The default password
*
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $method,
?string $default = null): ?string
{
if (($password = $this->getPassword($method, $default)) !== null
&& ($name = $this->getClassName($method)) !== null)
{
return $this->{$name}->decrypt($string, $password);
}
return null;
}
/**
* Check if a decryption method exist and is supported
*
* @param string $method The encryption method to find
*
* @return bool true if it exist
* @since 3.2.0
**/
public function exist(string $method): bool
{
return is_string($this->getClassName($method)) ?? false;
}
/**
* Get crypto class name to use
*
* @param string $method The encryption method to find
*
* @return string|null The crypto class name
* @since 3.2.0
**/
private function getClassName(string $method): ?string
{
if (($name = $this->getClassNameFromRegistry($method)) !== null)
{
return $name;
}
return $this->getClassNameFromOptions($method);
}
/**
* Get the crypto class name from the registry
*
* @param string $method The encryption method to use
*
* @return string|null The crypto class name, or null if not found
* @since 3.2.0
**/
private function getClassNameFromRegistry(string $method): ?string
{
$name = $this->name($method);
if (isset($this->{$name}) && $this->{$name} instanceof Cryptinterface)
{
return $name;
}
return null;
}
/**
* Get the crypto class name for the given encryption method and options
*
* @param string $method The encryption method to use
*
* @return string|null The crypto class name, or null if not found
* @since 3.2.0
**/
private function getClassNameFromOptions(string $method): ?string
{
$key = $this->getPasswordKey($method);
if (isset($this->options[$key]))
{
$name = $this->options[$key];
if (isset($this->{$name}) && $this->{$name} instanceof Cryptinterface)
{
return $name;
}
}
return null;
}
/**
* Get the password
*
* @param string $method The encryption method to find
* @param string|null $password The password
*
* @return string|null the password or null
* @since 3.2.0
**/
private function getPassword(string $method, ?string $password = null): ?string
{
if (StringHelper::check($password))
{
return $password;
}
// get password key name
$key = $this->getPasswordKey($method);
if (empty($this->passwords[$key]))
{
$this->passwords[$key] = $this->password->get($key);
}
return $this->passwords[$key];
}
/**
* Get the key
*
* @param string $method The encryption method to find
*
* @return string the password key name
* @since 3.2.0
**/
private function getPasswordKey(string $method): string
{
if (($pos = strpos($method, '.')) !== false)
{
return substr($method, 0, $pos);
}
return $method;
}
/**
* Get the class name
*
* @param string $method The encryption method to find
*
* @return string the class name
* @since 3.2.0
**/
private function name(string $method): string
{
if (($pos = strpos($method, '.')) !== false)
{
return substr($method, $pos + 1);
}
return $method;
}