Complex classes like CryptTool often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CryptTool, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class CryptTool { |
||
29 | const TYPE_SODIUM = 'sodium'; |
||
30 | const TYPE_SALT = 'salt'; |
||
31 | |||
32 | const FILE_NONCE = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"; |
||
33 | const FILE_THUMBNAIL_NONCE = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"; |
||
34 | /** |
||
35 | * @var CryptTool |
||
36 | */ |
||
37 | private static $instance = null; |
||
38 | |||
39 | /** |
||
40 | * Prior libsodium |
||
41 | * |
||
42 | * @return CryptTool |
||
43 | */ |
||
44 | public static function getInstance() { |
||
63 | |||
64 | /** |
||
65 | * @param string $type |
||
66 | * @return null|CryptTool null on unknown type |
||
67 | */ |
||
68 | public static function createInstance($type) { |
||
85 | |||
86 | const MESSAGE_ID_LEN = 8; |
||
87 | const BLOB_ID_LEN = 16; |
||
88 | const IMAGE_FILE_SIZE_LEN = 4; |
||
89 | const IMAGE_NONCE_LEN = 24; |
||
90 | |||
91 | const EMAIL_HMAC_KEY = "\x30\xa5\x50\x0f\xed\x97\x01\xfa\x6d\xef\xdb\x61\x08\x41\x90\x0f\xeb\xb8\xe4\x30\x88\x1f\x7a\xd8\x16\x82\x62\x64\xec\x09\xba\xd7"; |
||
92 | const PHONENO_HMAC_KEY = "\x85\xad\xf8\x22\x69\x53\xf3\xd9\x6c\xfd\x5d\x09\xbf\x29\x55\x5e\xb9\x55\xfc\xd8\xaa\x5e\xc4\xf9\xfc\xd8\x69\xe2\x58\x37\x07\x23"; |
||
93 | |||
94 | protected function __construct() {} |
||
95 | |||
96 | /** |
||
97 | * Encrypt a text message. |
||
98 | * |
||
99 | * @param string $text the text to be encrypted (max. 3500 bytes) |
||
100 | * @param string $senderPrivateKey the private key of the sending ID |
||
101 | * @param string $recipientPublicKey the public key of the receiving ID |
||
102 | * @param string $nonce the nonce to be used for the encryption (usually 24 random bytes) |
||
103 | * @return string encrypted box |
||
104 | */ |
||
105 | final public function encryptMessageText($text, $senderPrivateKey, $recipientPublicKey, $nonce) { |
||
117 | |||
118 | /** |
||
119 | * @param UploadFileResult $uploadFileResult the result of the upload |
||
120 | * @param EncryptResult $encryptResult the result of the image encryption |
||
121 | * @param string $senderPrivateKey the private key of the sending ID (as binary) |
||
122 | * @param string $recipientPublicKey the public key of the receiving ID (as binary) |
||
123 | * @param string $nonce the nonce to be used for the encryption (usually 24 random bytes) |
||
124 | * @return string |
||
125 | */ |
||
126 | final public function encryptImageMessage( |
||
144 | |||
145 | final public function encryptFileMessage(UploadFileResult $uploadFileResult, |
||
177 | |||
178 | /** |
||
179 | * make a box |
||
180 | * |
||
181 | * @param string $data |
||
182 | * @param string $nonce |
||
183 | * @param string $senderPrivateKey |
||
184 | * @param string $recipientPublicKey |
||
185 | * @return string encrypted box |
||
186 | */ |
||
187 | abstract protected function makeBox($data, $nonce, $senderPrivateKey, $recipientPublicKey); |
||
188 | |||
189 | /** |
||
190 | * make a secret box |
||
191 | * |
||
192 | * @param $data |
||
193 | * @param $nonce |
||
194 | * @param $key |
||
195 | * @return mixed |
||
196 | */ |
||
197 | abstract protected function makeSecretBox($data, $nonce, $key); |
||
198 | |||
199 | /** |
||
200 | * decrypt a box |
||
201 | * |
||
202 | * @param string $box as binary |
||
203 | * @param string $recipientPrivateKey as binary |
||
204 | * @param string $senderPublicKey as binary |
||
205 | * @param string $nonce as binary |
||
206 | * @return string |
||
207 | */ |
||
208 | abstract protected function openBox($box, $recipientPrivateKey, $senderPublicKey, $nonce); |
||
209 | |||
210 | /** |
||
211 | * decrypt a secret box |
||
212 | * |
||
213 | * @param string $box as binary |
||
214 | * @param string $nonce as binary |
||
215 | * @param string $key as binary |
||
216 | * @return string as binary |
||
217 | */ |
||
218 | abstract protected function openSecretBox($box, $nonce, $key); |
||
219 | |||
220 | /** |
||
221 | * @param string $box |
||
222 | * @param string $recipientPrivateKey |
||
223 | * @param string $senderPublicKey |
||
224 | * @param string $nonce |
||
225 | * @return ThreemaMessage the decrypted message |
||
226 | * @throws BadMessageException |
||
227 | * @throws DecryptionFailedException |
||
228 | * @throws UnsupportedMessageTypeException |
||
229 | */ |
||
230 | final public function decryptMessage($box, $recipientPrivateKey, $senderPublicKey, $nonce) { |
||
307 | |||
308 | /** |
||
309 | * Generate a new key pair. |
||
310 | * |
||
311 | * @return KeyPair the new key pair |
||
312 | */ |
||
313 | abstract public function generateKeyPair(); |
||
314 | |||
315 | /** |
||
316 | * Hashes an email address for identity lookup. |
||
317 | * |
||
318 | * @param string $email the email address |
||
319 | * @return string the email hash (hex) |
||
320 | */ |
||
321 | final public function hashEmail($email) { |
||
325 | |||
326 | /** |
||
327 | * Hashes an phone number address for identity lookup. |
||
328 | * |
||
329 | * @param string $phoneNo the phone number (in E.164 format, no leading +) |
||
330 | * @return string the phone number hash (hex) |
||
331 | */ |
||
332 | final public function hashPhoneNo($phoneNo) { |
||
336 | |||
337 | abstract protected function createRandom($size); |
||
338 | |||
339 | /** |
||
340 | * Generate a random nonce. |
||
341 | * |
||
342 | * @return string random nonce |
||
343 | */ |
||
344 | final public function randomNonce() { |
||
347 | |||
348 | /** |
||
349 | * Generate a symmetric key |
||
350 | * @return mixed |
||
351 | */ |
||
352 | final public function symmetricKey() { |
||
355 | |||
356 | /** |
||
357 | * Derive the public key |
||
358 | * |
||
359 | * @param string $privateKey as binary |
||
360 | * @return string as binary |
||
361 | */ |
||
362 | abstract public function derivePublicKey($privateKey); |
||
363 | |||
364 | /** |
||
365 | * Check if implementation supported |
||
366 | * @return bool |
||
367 | */ |
||
368 | abstract public function isSupported(); |
||
369 | |||
370 | /** |
||
371 | * Validate crypt tool |
||
372 | * |
||
373 | * @return bool |
||
374 | * @throws Exception |
||
375 | */ |
||
376 | abstract public function validate(); |
||
377 | |||
378 | /** |
||
379 | * @param $data |
||
380 | * @return EncryptResult |
||
381 | */ |
||
382 | public final function encryptFile($data) { |
||
387 | |||
388 | /** |
||
389 | * @param string $data as binary |
||
390 | * @param string $key as binary |
||
391 | * @return null|string |
||
392 | */ |
||
393 | public final function decryptFile($data, $key) { |
||
397 | |||
398 | /** |
||
399 | * @param string $data |
||
400 | * @param string $key |
||
401 | * @return EncryptResult |
||
402 | */ |
||
403 | public final function encryptFileThumbnail($data, $key) { |
||
407 | |||
408 | public final function decryptFileThumbnail($data, $key) { |
||
412 | |||
413 | /** |
||
414 | * @param string $imageData |
||
415 | * @param string $privateKey as binary |
||
416 | * @param string $publicKey as binary |
||
417 | * @return EncryptResult |
||
418 | */ |
||
419 | public final function encryptImage($imageData, $privateKey, $publicKey) { |
||
431 | |||
432 | /** |
||
433 | * @param string $data as binary |
||
434 | * @param string $publicKey as binary |
||
435 | * @param string $privateKey as binary |
||
436 | * @param string $nonce as binary |
||
437 | * @return string |
||
438 | */ |
||
439 | public final function decryptImage($data, $publicKey, $privateKey, $nonce) { |
||
445 | |||
446 | /** |
||
447 | * determine random amount of PKCS7 padding |
||
448 | * @return int |
||
449 | */ |
||
450 | private function generatePadBytes() { |
||
457 | |||
458 | function __toString() { |
||
461 | |||
462 | /** |
||
463 | * Converts a binary string to an hexdecimal string. |
||
464 | * |
||
465 | * This is the same as PHP's bin2hex() implementation, but it is resistant to |
||
466 | * timing attacks. |
||
467 | * |
||
468 | * @param string $binaryString The binary string to convert |
||
469 | * @return string |
||
470 | */ |
||
471 | public function bin2hex($binaryString) |
||
475 | |||
476 | /** |
||
477 | * Converts an hexdecimal string to a binary string. |
||
478 | * |
||
479 | * This is the same as PHP's hex2bin() implementation, but it is resistant to |
||
480 | * timing attacks. |
||
481 | * Note that the default implementation does not support $ignore currrently and will |
||
482 | * throw an error. Only when libsodium >= 0.22 is used, this is supported. |
||
483 | * |
||
484 | * @param string $hexString The hex string to convert |
||
485 | * @param string|null $ignore (optional) Characters to ignore |
||
486 | * @throws \Threema\Core\Exception |
||
487 | * @return string |
||
488 | */ |
||
489 | public function hex2bin($hexString, $ignore = null) |
||
496 | |||
497 | /** |
||
498 | * Compares two strings in a secure way. |
||
499 | * |
||
500 | * This is the same as PHP's strcmp() implementation, but it is resistant to |
||
501 | * timing attacks. |
||
502 | * |
||
503 | * @link https://paragonie.com/book/pecl-libsodium/read/03-utilities-helpers.md#compare |
||
504 | * @param string $str1 The first string |
||
505 | * @param string $str2 The second string |
||
506 | * @return int |
||
507 | */ |
||
508 | public function stringCompare($str1, $str2) |
||
532 | |||
533 | /** |
||
534 | * Name of the CryptTool |
||
535 | * @return string |
||
536 | */ |
||
537 | abstract public function getName(); |
||
538 | |||
539 | /** |
||
540 | * Description of the CryptTool |
||
541 | * @return string |
||
542 | */ |
||
543 | abstract public function getDescription(); |
||
544 | } |
||
545 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.