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 |
||
14 | final class RSA |
||
15 | { |
||
16 | /** |
||
17 | * ASN1 Integer. |
||
18 | */ |
||
19 | const ASN1_INTEGER = 2; |
||
20 | |||
21 | /** |
||
22 | * ASN1 Bit String. |
||
23 | */ |
||
24 | const ASN1_BITSTRING = 3; |
||
25 | |||
26 | /** |
||
27 | * ASN1 Octet String. |
||
28 | */ |
||
29 | const ASN1_OCTETSTRING = 4; |
||
30 | |||
31 | /** |
||
32 | * ASN1 Object Identifier. |
||
33 | */ |
||
34 | const ASN1_OBJECT = 6; |
||
35 | |||
36 | /** |
||
37 | * ASN1 Sequence (with the constucted bit set). |
||
38 | */ |
||
39 | const ASN1_SEQUENCE = 48; |
||
40 | |||
41 | /** |
||
42 | * To use the pure-PHP implementation. |
||
43 | */ |
||
44 | const MODE_INTERNAL = 1; |
||
45 | |||
46 | /** |
||
47 | * To use the OpenSSL library. |
||
48 | */ |
||
49 | const MODE_OPENSSL = 2; |
||
50 | |||
51 | /** |
||
52 | * PKCS#1 formatted private key. |
||
53 | */ |
||
54 | const PRIVATE_FORMAT_PKCS1 = 0; |
||
55 | |||
56 | /** |
||
57 | * PuTTY formatted private key. |
||
58 | */ |
||
59 | const PRIVATE_FORMAT_PUTTY = 1; |
||
60 | |||
61 | /** |
||
62 | * XML formatted private key. |
||
63 | */ |
||
64 | const PRIVATE_FORMAT_XML = 2; |
||
65 | |||
66 | /** |
||
67 | * PKCS#8 formatted private key. |
||
68 | */ |
||
69 | const PRIVATE_FORMAT_PKCS8 = 8; |
||
70 | |||
71 | /** |
||
72 | * Raw public key. |
||
73 | */ |
||
74 | const PUBLIC_FORMAT_RAW = 3; |
||
75 | |||
76 | /** |
||
77 | * PKCS#1 formatted public key (raw). |
||
78 | */ |
||
79 | const PUBLIC_FORMAT_PKCS1 = 4; |
||
80 | const PUBLIC_FORMAT_PKCS1_RAW = 4; |
||
81 | |||
82 | /** |
||
83 | * XML formatted public key. |
||
84 | */ |
||
85 | const PUBLIC_FORMAT_XML = 5; |
||
86 | |||
87 | /** |
||
88 | * OpenSSH formatted public key. |
||
89 | */ |
||
90 | const PUBLIC_FORMAT_OPENSSH = 6; |
||
91 | |||
92 | /** |
||
93 | * PKCS#1 formatted public key (encapsulated). |
||
94 | */ |
||
95 | const PUBLIC_FORMAT_PKCS8 = 7; |
||
96 | |||
97 | /** |
||
98 | * Precomputed Zero. |
||
99 | * |
||
100 | * @var \Jose\Util\BigInteger |
||
101 | */ |
||
102 | private $zero; |
||
103 | |||
104 | /** |
||
105 | * Precomputed One. |
||
106 | * |
||
107 | * @var \Jose\Util\BigInteger |
||
108 | */ |
||
109 | private $one; |
||
110 | |||
111 | /** |
||
112 | * Modulus (ie. n). |
||
113 | * |
||
114 | * @var \Jose\Util\BigInteger |
||
115 | */ |
||
116 | private $modulus; |
||
117 | |||
118 | /** |
||
119 | * Modulus length. |
||
120 | * |
||
121 | * @var int |
||
122 | */ |
||
123 | private $k; |
||
124 | |||
125 | /** |
||
126 | * Exponent (ie. e or d). |
||
127 | * |
||
128 | * @var \Jose\Util\BigInteger |
||
129 | */ |
||
130 | private $exponent; |
||
131 | |||
132 | /** |
||
133 | * Primes for Chinese Remainder Theorem (ie. p and q). |
||
134 | * |
||
135 | * @var array |
||
136 | */ |
||
137 | private $primes; |
||
138 | |||
139 | /** |
||
140 | * Exponents for Chinese Remainder Theorem (ie. dP and dQ). |
||
141 | * |
||
142 | * @var array |
||
143 | */ |
||
144 | private $exponents; |
||
145 | |||
146 | /** |
||
147 | * Coefficients for Chinese Remainder Theorem (ie. qInv). |
||
148 | * |
||
149 | * @var array |
||
150 | */ |
||
151 | private $coefficients; |
||
152 | |||
153 | /** |
||
154 | * Hash name. |
||
155 | * |
||
156 | * @var string |
||
157 | */ |
||
158 | private $hashName; |
||
159 | |||
160 | /** |
||
161 | * Hash function. |
||
162 | * |
||
163 | * @var \Jose\Util\Hash |
||
164 | */ |
||
165 | private $hash; |
||
166 | |||
167 | /** |
||
168 | * Length of hash function output. |
||
169 | * |
||
170 | * @var int |
||
171 | */ |
||
172 | private $hLen; |
||
173 | |||
174 | /** |
||
175 | * Length of salt. |
||
176 | * |
||
177 | * @var int |
||
178 | */ |
||
179 | private $sLen; |
||
180 | |||
181 | /** |
||
182 | * Hash function for the Mask Generation Function. |
||
183 | * |
||
184 | * @var \Jose\Util\Hash |
||
185 | */ |
||
186 | private $mgfHash; |
||
187 | |||
188 | /** |
||
189 | * Length of MGF hash function output. |
||
190 | * |
||
191 | * @var int |
||
192 | */ |
||
193 | private $mgfHLen; |
||
194 | |||
195 | /** |
||
196 | * Public Exponent. |
||
197 | * |
||
198 | * @var mixed |
||
199 | */ |
||
200 | private $publicExponent = false; |
||
201 | |||
202 | /** |
||
203 | * Current String. |
||
204 | * |
||
205 | * @var mixed |
||
206 | */ |
||
207 | private $current; |
||
208 | |||
209 | /** |
||
210 | * OpenSSL configuration file name. |
||
211 | * |
||
212 | * @var mixed |
||
213 | */ |
||
214 | private $configFile; |
||
215 | |||
216 | /** |
||
217 | * Public key comment field. |
||
218 | * |
||
219 | * @var string |
||
220 | */ |
||
221 | private $comment = 'phpseclib-generated-key'; |
||
222 | |||
223 | /** |
||
224 | * RSA constructor. |
||
225 | */ |
||
226 | public function __construct() |
||
287 | |||
288 | /** |
||
289 | * Break a public or private key down into its constituant components. |
||
290 | * |
||
291 | * @param string $key |
||
292 | * @param int $type |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | private function _parseKey($key, $type) |
||
396 | |||
397 | /** |
||
398 | * Data Handler. |
||
399 | * |
||
400 | * Called by xml_set_character_data_handler() |
||
401 | * |
||
402 | * @param resource $parser |
||
403 | * @param string $data |
||
404 | */ |
||
405 | public function _data_handler($parser, $data) |
||
412 | |||
413 | /** |
||
414 | * Loads a public or private key. |
||
415 | * |
||
416 | * @param string $key |
||
417 | * @param bool $type optional |
||
418 | * |
||
419 | * @return bool |
||
420 | */ |
||
421 | public function loadKey($key, $type = false) |
||
455 | |||
456 | /** |
||
457 | * DER-decode the length. |
||
458 | * |
||
459 | * @param string $string |
||
460 | * |
||
461 | * @return int |
||
462 | */ |
||
463 | private function _decodeLength(&$string) |
||
474 | |||
475 | /** |
||
476 | * String Shift. |
||
477 | * |
||
478 | * @param string $string |
||
479 | * @param int $index |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | private function _string_shift(&$string, $index = 1) |
||
490 | |||
491 | /** |
||
492 | * Determines which hashing function should be used. |
||
493 | * |
||
494 | * @param string $hash |
||
495 | */ |
||
496 | public function setHash($hash) |
||
516 | |||
517 | /** |
||
518 | * Determines which hashing function should be used for the mask generation function. |
||
519 | * |
||
520 | * @param string $hash |
||
521 | */ |
||
522 | public function setMGFHash($hash) |
||
542 | |||
543 | /** |
||
544 | * Determines the salt length. |
||
545 | * |
||
546 | * @param int $sLen |
||
547 | */ |
||
548 | public function setSaltLength($sLen) |
||
552 | |||
553 | /** |
||
554 | * Integer-to-Octet-String primitive. |
||
555 | * |
||
556 | * @param \Jose\Util\BigInteger $x |
||
557 | * @param int $xLen |
||
558 | * |
||
559 | * @return string |
||
560 | */ |
||
561 | private function _i2osp($x, $xLen) |
||
572 | |||
573 | /** |
||
574 | * Octet-String-to-Integer primitive. |
||
575 | * |
||
576 | * @param string $x |
||
577 | * |
||
578 | * @return \Jose\Util\BigInteger |
||
579 | */ |
||
580 | private function _os2ip($x) |
||
584 | |||
585 | /** |
||
586 | * Exponentiate with or without Chinese Remainder Theorem. |
||
587 | * |
||
588 | * @param \Jose\Util\BigInteger $x |
||
589 | * |
||
590 | * @return \Jose\Util\BigInteger |
||
591 | */ |
||
592 | private function _exponentiate($x) |
||
659 | |||
660 | /** |
||
661 | * Performs RSA Blinding. |
||
662 | * |
||
663 | * @param \Jose\Util\BigInteger $x |
||
664 | * @param \Jose\Util\BigInteger $r |
||
665 | * @param int $i |
||
666 | * |
||
667 | * @return \Jose\Util\BigInteger |
||
668 | */ |
||
669 | private function _blind($x, $r, $i) |
||
680 | |||
681 | /** |
||
682 | * Performs blinded RSA equality testing. |
||
683 | * |
||
684 | * @param string $x |
||
685 | * @param string $y |
||
686 | * |
||
687 | * @return bool |
||
688 | */ |
||
689 | private function _equals($x, $y) |
||
702 | |||
703 | /** |
||
704 | * RSAEP. |
||
705 | * |
||
706 | * @param \Jose\Util\BigInteger $m |
||
707 | * |
||
708 | * @return \Jose\Util\BigInteger |
||
709 | */ |
||
710 | private function _rsaep($m) |
||
720 | |||
721 | /** |
||
722 | * RSADP. |
||
723 | * |
||
724 | * @param \Jose\Util\BigInteger $c |
||
725 | * |
||
726 | * @return \Jose\Util\BigInteger |
||
727 | */ |
||
728 | private function _rsadp($c) |
||
738 | |||
739 | /** |
||
740 | * RSASP1. |
||
741 | * |
||
742 | * @param \Jose\Util\BigInteger $m |
||
743 | * |
||
744 | * @return \Jose\Util\BigInteger |
||
745 | */ |
||
746 | private function _rsasp1($m) |
||
756 | |||
757 | /** |
||
758 | * RSAVP1. |
||
759 | * |
||
760 | * @param \Jose\Util\BigInteger $s |
||
761 | * |
||
762 | * @return \Jose\Util\BigInteger |
||
763 | */ |
||
764 | private function _rsavp1($s) |
||
774 | |||
775 | /** |
||
776 | * MGF1. |
||
777 | * |
||
778 | * @param string $mgfSeed |
||
779 | * @param int $maskLen |
||
780 | * |
||
781 | * @return string |
||
782 | */ |
||
783 | private function _mgf1($mgfSeed, $maskLen) |
||
796 | |||
797 | /** |
||
798 | * RSAES-OAEP-ENCRYPT. |
||
799 | * |
||
800 | * @param string $m |
||
801 | * @param string $l |
||
802 | * |
||
803 | * @return string |
||
804 | */ |
||
805 | private function _rsaes_oaep_encrypt($m, $l = '') |
||
842 | |||
843 | /** |
||
844 | * RSAES-OAEP-DECRYPT. |
||
845 | * |
||
846 | * @param string $c |
||
847 | * @param string $l |
||
848 | * |
||
849 | * @return string |
||
850 | */ |
||
851 | private function _rsaes_oaep_decrypt($c, $l = '') |
||
903 | |||
904 | /** |
||
905 | * EMSA-PSS-ENCODE. |
||
906 | * |
||
907 | * @param string $m |
||
908 | * @param int $emBits |
||
909 | * |
||
910 | * @return bool |
||
911 | */ |
||
912 | private function _emsa_pss_encode($m, $emBits) |
||
939 | |||
940 | /** |
||
941 | * EMSA-PSS-VERIFY. |
||
942 | * |
||
943 | * @param string $m |
||
944 | * @param string $em |
||
945 | * @param int $emBits |
||
946 | * |
||
947 | * @return string |
||
948 | */ |
||
949 | private function _emsa_pss_verify($m, $em, $emBits) |
||
985 | |||
986 | /** |
||
987 | * RSASSA-PSS-SIGN. |
||
988 | * |
||
989 | * @param string $m |
||
990 | * |
||
991 | * @return string |
||
992 | */ |
||
993 | private function _rsassa_pss_sign($m) |
||
1009 | |||
1010 | /** |
||
1011 | * RSASSA-PSS-VERIFY. |
||
1012 | * |
||
1013 | * @param string $m |
||
1014 | * @param string $s |
||
1015 | * |
||
1016 | * @return string |
||
1017 | */ |
||
1018 | private function _rsassa_pss_verify($m, $s) |
||
1050 | |||
1051 | /** |
||
1052 | * Encryption. |
||
1053 | * |
||
1054 | * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. |
||
1055 | * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will |
||
1056 | * be concatenated together. |
||
1057 | * |
||
1058 | * @see self::decrypt() |
||
1059 | * |
||
1060 | * @param string $plaintext |
||
1061 | * |
||
1062 | * @return string |
||
1063 | */ |
||
1064 | public function encrypt($plaintext) |
||
1079 | |||
1080 | /** |
||
1081 | * Decryption. |
||
1082 | * |
||
1083 | * @param string $ciphertext |
||
1084 | * |
||
1085 | * @return string |
||
1086 | */ |
||
1087 | public function decrypt($ciphertext) |
||
1108 | |||
1109 | /** |
||
1110 | * Create a signature. |
||
1111 | * |
||
1112 | * @param string $message |
||
1113 | * |
||
1114 | * @return string |
||
1115 | */ |
||
1116 | public function sign($message) |
||
1125 | |||
1126 | /** |
||
1127 | * Verifies a signature. |
||
1128 | * |
||
1129 | * @param string $message |
||
1130 | * @param string $signature |
||
1131 | * |
||
1132 | * @return bool |
||
1133 | */ |
||
1134 | public function verify($message, $signature) |
||
1142 | |||
1143 | /** |
||
1144 | * Extract raw BER from Base64 encoding. |
||
1145 | * |
||
1146 | * @param string $str |
||
1147 | * |
||
1148 | * @return string |
||
1149 | */ |
||
1150 | private function _extractBER($str) |
||
1161 | |||
1162 | /** |
||
1163 | * Defines the public key. |
||
1164 | * |
||
1165 | * @return bool |
||
1166 | */ |
||
1167 | private function setPublicKey() |
||
1175 | } |
||
1176 |
If you suppress an error, we recommend checking for the error condition explicitly: