Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Encryption 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 Encryption, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Encryption implements IEncryptionModule { |
||
45 | |||
46 | const ID = 'OC_DEFAULT_MODULE'; |
||
47 | const DISPLAY_NAME = 'Default encryption module'; |
||
48 | |||
49 | /** |
||
50 | * @var Crypt |
||
51 | */ |
||
52 | private $crypt; |
||
53 | |||
54 | /** @var string */ |
||
55 | private $cipher; |
||
56 | |||
57 | /** @var string */ |
||
58 | private $path; |
||
59 | |||
60 | /** @var string */ |
||
61 | private $user; |
||
62 | |||
63 | /** @var array */ |
||
64 | private $owner; |
||
65 | |||
66 | /** @var string */ |
||
67 | private $fileKey; |
||
68 | |||
69 | /** @var string */ |
||
70 | private $writeCache; |
||
71 | |||
72 | /** @var KeyManager */ |
||
73 | private $keyManager; |
||
74 | |||
75 | /** @var array */ |
||
76 | private $accessList; |
||
77 | |||
78 | /** @var boolean */ |
||
79 | private $isWriteOperation; |
||
80 | |||
81 | /** @var Util */ |
||
82 | private $util; |
||
83 | |||
84 | /** @var Session */ |
||
85 | private $session; |
||
86 | |||
87 | /** @var ILogger */ |
||
88 | private $logger; |
||
89 | |||
90 | /** @var IL10N */ |
||
91 | private $l; |
||
92 | |||
93 | /** @var EncryptAll */ |
||
94 | private $encryptAll; |
||
95 | |||
96 | /** @var bool */ |
||
97 | private $useMasterPassword; |
||
98 | |||
99 | /** @var DecryptAll */ |
||
100 | private $decryptAll; |
||
101 | |||
102 | /** @var int unencrypted block size if block contains signature */ |
||
103 | private $unencryptedBlockSizeSigned = 6072; |
||
104 | |||
105 | /** @var int unencrypted block size */ |
||
106 | private $unencryptedBlockSize = 6126; |
||
107 | |||
108 | /** @var int Current version of the file */ |
||
109 | private $version = 0; |
||
110 | |||
111 | /** @var array remember encryption signature version */ |
||
112 | private static $rememberVersion = []; |
||
113 | |||
114 | |||
115 | /** |
||
116 | * |
||
117 | * @param Crypt $crypt |
||
118 | * @param KeyManager $keyManager |
||
119 | * @param Util $util |
||
120 | * @param Session $session |
||
121 | * @param EncryptAll $encryptAll |
||
122 | * @param DecryptAll $decryptAll |
||
123 | * @param ILogger $logger |
||
124 | * @param IL10N $il10n |
||
125 | */ |
||
126 | View Code Duplication | public function __construct(Crypt $crypt, |
|
127 | KeyManager $keyManager, |
||
128 | Util $util, |
||
129 | Session $session, |
||
130 | EncryptAll $encryptAll, |
||
131 | DecryptAll $decryptAll, |
||
132 | ILogger $logger, |
||
133 | IL10N $il10n) { |
||
134 | $this->crypt = $crypt; |
||
135 | $this->keyManager = $keyManager; |
||
136 | $this->util = $util; |
||
137 | $this->session = $session; |
||
138 | $this->encryptAll = $encryptAll; |
||
139 | $this->decryptAll = $decryptAll; |
||
140 | $this->logger = $logger; |
||
141 | $this->l = $il10n; |
||
142 | $this->owner = []; |
||
143 | $this->useMasterPassword = $util->isMasterKeyEnabled(); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return string defining the technical unique id |
||
148 | */ |
||
149 | public function getId() { |
||
150 | return self::ID; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * In comparison to getKey() this function returns a human readable (maybe translated) name |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getDisplayName() { |
||
159 | return self::DISPLAY_NAME; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * start receiving chunks from a file. This is the place where you can |
||
164 | * perform some initial step before starting encrypting/decrypting the |
||
165 | * chunks |
||
166 | * |
||
167 | * @param string $path to the file |
||
168 | * @param string $user who read/write the file |
||
169 | * @param string $mode php stream open mode |
||
170 | * @param array $header contains the header data read from the file |
||
171 | * @param array $accessList who has access to the file contains the key 'users' and 'public' |
||
172 | * |
||
173 | * @return array $header contain data as key-value pairs which should be |
||
174 | * written to the header, in case of a write operation |
||
175 | * or if no additional data is needed return a empty array |
||
176 | */ |
||
177 | public function begin($path, $user, $mode, array $header, array $accessList) { |
||
178 | $this->path = $this->getPathToRealFile($path); |
||
179 | $this->accessList = $accessList; |
||
180 | $this->user = $user; |
||
181 | $this->isWriteOperation = false; |
||
182 | $this->writeCache = ''; |
||
183 | |||
184 | if($this->session->isReady() === false) { |
||
185 | // if the master key is enabled we can initialize encryption |
||
186 | // with a empty password and user name |
||
187 | if ($this->util->isMasterKeyEnabled()) { |
||
188 | $this->keyManager->init('', ''); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | if ($this->session->decryptAllModeActivated()) { |
||
193 | $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path); |
||
194 | $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid()); |
||
195 | $this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey, |
||
196 | $shareKey, |
||
197 | $this->session->getDecryptAllKey()); |
||
198 | } else { |
||
199 | $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user); |
||
200 | } |
||
201 | |||
202 | // always use the version from the original file, also part files |
||
203 | // need to have a correct version number if they get moved over to the |
||
204 | // final location |
||
205 | $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
||
206 | |||
207 | if ( |
||
208 | $mode === 'w' |
||
209 | || $mode === 'w+' |
||
210 | || $mode === 'wb' |
||
211 | || $mode === 'wb+' |
||
212 | ) { |
||
213 | $this->isWriteOperation = true; |
||
214 | if (empty($this->fileKey)) { |
||
215 | $this->fileKey = $this->crypt->generateFileKey(); |
||
216 | } |
||
217 | } else { |
||
218 | // if we read a part file we need to increase the version by 1 |
||
219 | // because the version number was also increased by writing |
||
220 | // the part file |
||
221 | if(Scanner::isPartialFile($path)) { |
||
222 | $this->version = $this->version + 1; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | if ($this->isWriteOperation) { |
||
227 | $this->cipher = $this->crypt->getCipher(); |
||
228 | } elseif (isset($header['cipher'])) { |
||
229 | $this->cipher = $header['cipher']; |
||
230 | } else { |
||
231 | // if we read a file without a header we fall-back to the legacy cipher |
||
232 | // which was used in <=oC6 |
||
233 | $this->cipher = $this->crypt->getLegacyCipher(); |
||
234 | } |
||
235 | |||
236 | return array('cipher' => $this->cipher, 'signed' => 'true'); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * last chunk received. This is the place where you can perform some final |
||
241 | * operation and return some remaining data if something is left in your |
||
242 | * buffer. |
||
243 | * |
||
244 | * @param string $path to the file |
||
245 | * @param int $position |
||
246 | * @return string remained data which should be written to the file in case |
||
247 | * of a write operation |
||
248 | * @throws PublicKeyMissingException |
||
249 | * @throws \Exception |
||
250 | * @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException |
||
251 | */ |
||
252 | public function end($path, $position = 0) { |
||
293 | |||
294 | |||
295 | |||
296 | /** |
||
297 | * encrypt data |
||
298 | * |
||
299 | * @param string $data you want to encrypt |
||
300 | * @param int $position |
||
301 | * @return string encrypted data |
||
302 | */ |
||
303 | public function encrypt($data, $position = 0) { |
||
360 | |||
361 | /** |
||
362 | * decrypt data |
||
363 | * |
||
364 | * @param string $data you want to decrypt |
||
365 | * @param int $position |
||
366 | * @return string decrypted data |
||
367 | * @throws DecryptionFailedException |
||
368 | */ |
||
369 | public function decrypt($data, $position = 0) { |
||
380 | |||
381 | /** |
||
382 | * update encrypted file, e.g. give additional users access to the file |
||
383 | * |
||
384 | * @param string $path path to the file which should be updated |
||
385 | * @param string $uid of the user who performs the operation |
||
386 | * @param array $accessList who has access to the file contains the key 'users' and 'public' |
||
387 | * @return boolean |
||
388 | */ |
||
389 | public function update($path, $uid, array $accessList) { |
||
433 | |||
434 | /** |
||
435 | * should the file be encrypted or not |
||
436 | * |
||
437 | * @param string $path |
||
438 | * @return boolean |
||
439 | */ |
||
440 | public function shouldEncrypt($path) { |
||
464 | |||
465 | /** |
||
466 | * get size of the unencrypted payload per block. |
||
467 | * Nextcloud read/write files with a block size of 8192 byte |
||
468 | * |
||
469 | * @param bool $signed |
||
470 | * @return int |
||
471 | */ |
||
472 | public function getUnencryptedBlockSize($signed = false) { |
||
479 | |||
480 | /** |
||
481 | * check if the encryption module is able to read the file, |
||
482 | * e.g. if all encryption keys exists |
||
483 | * |
||
484 | * @param string $path |
||
485 | * @param string $uid user for whom we want to check if he can read the file |
||
486 | * @return bool |
||
487 | * @throws DecryptionFailedException |
||
488 | */ |
||
489 | public function isReadable($path, $uid) { |
||
509 | |||
510 | /** |
||
511 | * Initial encryption of all files |
||
512 | * |
||
513 | * @param InputInterface $input |
||
514 | * @param OutputInterface $output write some status information to the terminal during encryption |
||
515 | */ |
||
516 | public function encryptAll(InputInterface $input, OutputInterface $output) { |
||
519 | |||
520 | /** |
||
521 | * prepare module to perform decrypt all operation |
||
522 | * |
||
523 | * @param InputInterface $input |
||
524 | * @param OutputInterface $output |
||
525 | * @param string $user |
||
526 | * @return bool |
||
527 | */ |
||
528 | public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
||
531 | |||
532 | |||
533 | /** |
||
534 | * @param string $path |
||
535 | * @return string |
||
536 | */ |
||
537 | protected function getPathToRealFile($path) { |
||
548 | |||
549 | /** |
||
550 | * remove .part file extension and the ocTransferId from the file to get the |
||
551 | * original file name |
||
552 | * |
||
553 | * @param string $path |
||
554 | * @return string |
||
555 | */ |
||
556 | protected function stripPartFileExtension($path) { |
||
564 | |||
565 | /** |
||
566 | * get owner of a file |
||
567 | * |
||
568 | * @param string $path |
||
569 | * @return string |
||
570 | */ |
||
571 | protected function getOwner($path) { |
||
577 | |||
578 | /** |
||
579 | * Check if the module is ready to be used by that specific user. |
||
580 | * In case a module is not ready - because e.g. key pairs have not been generated |
||
581 | * upon login this method can return false before any operation starts and might |
||
582 | * cause issues during operations. |
||
583 | * |
||
584 | * @param string $user |
||
585 | * @return boolean |
||
586 | * @since 9.1.0 |
||
587 | */ |
||
588 | public function isReadyForUser($user) { |
||
591 | |||
592 | /** |
||
593 | * We only need a detailed access list if the master key is not enabled |
||
594 | * |
||
595 | * @return bool |
||
596 | */ |
||
597 | public function needDetailedAccessList() { |
||
600 | } |
||
601 |