Complex classes like RSA 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 RSA, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class RSA |
||
18 | { |
||
19 | /** |
||
20 | * Precomputed Zero. |
||
21 | * |
||
22 | * @var \Jose\Util\BigInteger |
||
23 | */ |
||
24 | private $zero; |
||
25 | |||
26 | /** |
||
27 | * Precomputed One. |
||
28 | * |
||
29 | * @var \Jose\Util\BigInteger |
||
30 | */ |
||
31 | private $one; |
||
32 | |||
33 | /** |
||
34 | * Modulus (ie. n). |
||
35 | * |
||
36 | * @var \Jose\Util\BigInteger |
||
37 | */ |
||
38 | private $modulus; |
||
39 | |||
40 | /** |
||
41 | * Modulus length. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | private $k; |
||
46 | |||
47 | /** |
||
48 | * Exponent (ie. e or d). |
||
49 | * |
||
50 | * @var \Jose\Util\BigInteger |
||
51 | */ |
||
52 | private $exponent; |
||
53 | |||
54 | /** |
||
55 | * Primes for Chinese Remainder Theorem (ie. p and q). |
||
56 | * |
||
57 | * @var \Jose\Util\BigInteger[] |
||
58 | */ |
||
59 | private $primes; |
||
60 | |||
61 | /** |
||
62 | * Exponents for Chinese Remainder Theorem (ie. dP and dQ). |
||
63 | * |
||
64 | * @var \Jose\Util\BigInteger[] |
||
65 | */ |
||
66 | private $exponents; |
||
67 | |||
68 | /** |
||
69 | * Coefficients for Chinese Remainder Theorem (ie. qInv). |
||
70 | * |
||
71 | * @var \Jose\Util\BigInteger[] |
||
72 | */ |
||
73 | private $coefficients; |
||
74 | |||
75 | /** |
||
76 | * Hash function. |
||
77 | * |
||
78 | * @var \Jose\Util\Hash |
||
79 | */ |
||
80 | private $hash; |
||
81 | |||
82 | /** |
||
83 | * Hash function for the Mask Generation Function. |
||
84 | * |
||
85 | * @var \Jose\Util\Hash |
||
86 | */ |
||
87 | private $mgfHash; |
||
88 | |||
89 | /** |
||
90 | * Public Exponent. |
||
91 | * |
||
92 | * @var mixed |
||
93 | */ |
||
94 | private $publicExponent = false; |
||
95 | |||
96 | /** |
||
97 | * RSA constructor. |
||
98 | */ |
||
99 | public function __construct() |
||
107 | |||
108 | /** |
||
109 | * Loads a public or private key. |
||
110 | * |
||
111 | * @param \Jose\Object\JWKInterface $key |
||
112 | */ |
||
113 | public function loadKey(JWKInterface $key) |
||
144 | |||
145 | /** |
||
146 | * Determines which hashing function should be used. |
||
147 | * |
||
148 | * @param string $hash |
||
149 | */ |
||
150 | public function setHash($hash) |
||
154 | |||
155 | /** |
||
156 | * Determines which hashing function should be used for the mask generation function. |
||
157 | * |
||
158 | * @param string $hash |
||
159 | */ |
||
160 | public function setMGFHash($hash) |
||
164 | |||
165 | /** |
||
166 | * Integer-to-Octet-String primitive. |
||
167 | * |
||
168 | * @param \Jose\Util\BigInteger $x |
||
169 | * @param int $xLen |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | private function convertIntegerToOctetString($x, $xLen) |
||
183 | |||
184 | /** |
||
185 | * Octet-String-to-Integer primitive. |
||
186 | * |
||
187 | * @param string $x |
||
188 | * |
||
189 | * @return \Jose\Util\BigInteger |
||
190 | */ |
||
191 | private function convertOctetStringToInteger($x) |
||
195 | |||
196 | /** |
||
197 | * Exponentiate with or without Chinese Remainder Theorem. |
||
198 | * |
||
199 | * @param \Jose\Util\BigInteger $x |
||
200 | * |
||
201 | * @return \Jose\Util\BigInteger |
||
202 | */ |
||
203 | private function _exponentiate($x) |
||
246 | |||
247 | /** |
||
248 | * Performs RSA Blinding. |
||
249 | * |
||
250 | * @param \Jose\Util\BigInteger $x |
||
251 | * @param \Jose\Util\BigInteger $r |
||
252 | * @param int $i |
||
253 | * |
||
254 | * @return \Jose\Util\BigInteger |
||
255 | */ |
||
256 | private function _blind($x, $r, $i) |
||
267 | |||
268 | /** |
||
269 | * Performs blinded RSA equality testing. |
||
270 | * |
||
271 | * @param string $x |
||
272 | * @param string $y |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | private function _equals($x, $y) |
||
289 | |||
290 | /** |
||
291 | * RSAEP. |
||
292 | * |
||
293 | * @param \Jose\Util\BigInteger $m |
||
294 | * |
||
295 | * @return \Jose\Util\BigInteger|false |
||
296 | */ |
||
297 | private function _rsaep($m) |
||
306 | |||
307 | /** |
||
308 | * RSADP. |
||
309 | * |
||
310 | * @param \Jose\Util\BigInteger $c |
||
311 | * |
||
312 | * @return \Jose\Util\BigInteger|false |
||
313 | */ |
||
314 | private function _rsadp($c) |
||
323 | |||
324 | /** |
||
325 | * RSASP1. |
||
326 | * |
||
327 | * @param \Jose\Util\BigInteger $m |
||
328 | * |
||
329 | * @return \Jose\Util\BigInteger|false |
||
330 | */ |
||
331 | private function _rsasp1($m) |
||
340 | |||
341 | /** |
||
342 | * RSAVP1. |
||
343 | * |
||
344 | * @param \Jose\Util\BigInteger $s |
||
345 | * |
||
346 | * @return \Jose\Util\BigInteger|false |
||
347 | */ |
||
348 | private function _rsavp1($s) |
||
357 | |||
358 | /** |
||
359 | * MGF1. |
||
360 | * |
||
361 | * @param string $mgfSeed |
||
362 | * @param int $maskLen |
||
363 | * |
||
364 | * @return string |
||
365 | */ |
||
366 | private function _mgf1($mgfSeed, $maskLen) |
||
379 | |||
380 | /** |
||
381 | * RSAES-OAEP-ENCRYPT. |
||
382 | * |
||
383 | * @param string $m |
||
384 | * @param string $l |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | private function _rsaes_oaep_encrypt($m, $l = '') |
||
424 | |||
425 | /** |
||
426 | * RSAES-OAEP-DECRYPT. |
||
427 | * |
||
428 | * @param string $c |
||
429 | * @param string $l |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | private function _rsaes_oaep_decrypt($c, $l = '') |
||
480 | |||
481 | /** |
||
482 | * EMSA-PSS-ENCODE. |
||
483 | * |
||
484 | * @param string $m |
||
485 | * @param int $emBits |
||
486 | * |
||
487 | * @return bool |
||
488 | */ |
||
489 | private function _emsa_pss_encode($m, $emBits) |
||
515 | |||
516 | /** |
||
517 | * EMSA-PSS-VERIFY. |
||
518 | * |
||
519 | * @param string $m |
||
520 | * @param string $em |
||
521 | * @param int $emBits |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | private function _emsa_pss_verify($m, $em, $emBits) |
||
561 | |||
562 | /** |
||
563 | * RSASSA-PSS-SIGN. |
||
564 | * |
||
565 | * @param string $m |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | private function _rsassa_pss_sign($m) |
||
585 | |||
586 | /** |
||
587 | * RSASSA-PSS-VERIFY. |
||
588 | * |
||
589 | * @param string $m |
||
590 | * @param string $s |
||
591 | * |
||
592 | * @return string |
||
593 | */ |
||
594 | private function _rsassa_pss_verify($m, $s) |
||
623 | |||
624 | /** |
||
625 | * Encryption. |
||
626 | * |
||
627 | * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. |
||
628 | * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will |
||
629 | * be concatenated together. |
||
630 | * |
||
631 | * @see self::decrypt() |
||
632 | * |
||
633 | * @param string $plaintext |
||
634 | * |
||
635 | * @return string |
||
636 | */ |
||
637 | public function encrypt($plaintext) |
||
652 | |||
653 | /** |
||
654 | * Decryption. |
||
655 | * |
||
656 | * @param string $ciphertext |
||
657 | * |
||
658 | * @return string |
||
659 | */ |
||
660 | public function decrypt($ciphertext) |
||
681 | |||
682 | /** |
||
683 | * Create a signature. |
||
684 | * |
||
685 | * @param string $message |
||
686 | * |
||
687 | * @return string |
||
688 | */ |
||
689 | public function sign($message) |
||
698 | |||
699 | /** |
||
700 | * Verifies a signature. |
||
701 | * |
||
702 | * @param string $message |
||
703 | * @param string $signature |
||
704 | * |
||
705 | * @return bool |
||
706 | */ |
||
707 | public function verify($message, $signature) |
||
715 | } |
||
716 |