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 | * Optimal Asymmetric Encryption Padding (OAEP). |
||
| 18 | */ |
||
| 19 | const ENCRYPTION_OAEP = 1; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * PKCS#1 padding. |
||
| 23 | */ |
||
| 24 | const ENCRYPTION_PKCS1 = 2; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Probabilistic Signature Scheme for signing. |
||
| 28 | */ |
||
| 29 | const SIGNATURE_PSS = 1; |
||
| 30 | /** |
||
| 31 | * PKCS#1 scheme. |
||
| 32 | */ |
||
| 33 | const SIGNATURE_PKCS1 = 2; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * ASN1 Integer. |
||
| 37 | */ |
||
| 38 | const ASN1_INTEGER = 2; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * ASN1 Bit String. |
||
| 42 | */ |
||
| 43 | const ASN1_BITSTRING = 3; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * ASN1 Octet String. |
||
| 47 | */ |
||
| 48 | const ASN1_OCTETSTRING = 4; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * ASN1 Object Identifier. |
||
| 52 | */ |
||
| 53 | const ASN1_OBJECT = 6; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * ASN1 Sequence (with the constucted bit set). |
||
| 57 | */ |
||
| 58 | const ASN1_SEQUENCE = 48; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * To use the pure-PHP implementation. |
||
| 62 | */ |
||
| 63 | const MODE_INTERNAL = 1; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * To use the OpenSSL library. |
||
| 67 | */ |
||
| 68 | const MODE_OPENSSL = 2; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * PKCS#1 formatted private key. |
||
| 72 | */ |
||
| 73 | const PRIVATE_FORMAT_PKCS1 = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * PuTTY formatted private key. |
||
| 77 | */ |
||
| 78 | const PRIVATE_FORMAT_PUTTY = 1; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * XML formatted private key. |
||
| 82 | */ |
||
| 83 | const PRIVATE_FORMAT_XML = 2; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * PKCS#8 formatted private key. |
||
| 87 | */ |
||
| 88 | const PRIVATE_FORMAT_PKCS8 = 8; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Raw public key. |
||
| 92 | */ |
||
| 93 | const PUBLIC_FORMAT_RAW = 3; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * PKCS#1 formatted public key (raw). |
||
| 97 | */ |
||
| 98 | const PUBLIC_FORMAT_PKCS1 = 4; |
||
| 99 | const PUBLIC_FORMAT_PKCS1_RAW = 4; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * XML formatted public key. |
||
| 103 | */ |
||
| 104 | const PUBLIC_FORMAT_XML = 5; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * OpenSSH formatted public key. |
||
| 108 | */ |
||
| 109 | const PUBLIC_FORMAT_OPENSSH = 6; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * PKCS#1 formatted public key (encapsulated). |
||
| 113 | */ |
||
| 114 | const PUBLIC_FORMAT_PKCS8 = 7; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Precomputed Zero. |
||
| 118 | * |
||
| 119 | * @var \Jose\Util\BigInteger |
||
| 120 | */ |
||
| 121 | private $zero; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Precomputed One. |
||
| 125 | * |
||
| 126 | * @var \Jose\Util\BigInteger |
||
| 127 | */ |
||
| 128 | private $one; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Private Key Format. |
||
| 132 | * |
||
| 133 | * @var int |
||
| 134 | */ |
||
| 135 | private $privateKeyFormat = self::PRIVATE_FORMAT_PKCS1; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Public Key Format. |
||
| 139 | * |
||
| 140 | * @var int |
||
| 141 | */ |
||
| 142 | private $publicKeyFormat = self::PUBLIC_FORMAT_PKCS8; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Modulus (ie. n). |
||
| 146 | * |
||
| 147 | * @var \Jose\Util\BigInteger |
||
| 148 | */ |
||
| 149 | private $modulus; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Modulus length. |
||
| 153 | * |
||
| 154 | * @var int |
||
| 155 | */ |
||
| 156 | private $k; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Exponent (ie. e or d). |
||
| 160 | * |
||
| 161 | * @var \Jose\Util\BigInteger |
||
| 162 | */ |
||
| 163 | private $exponent; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Primes for Chinese Remainder Theorem (ie. p and q). |
||
| 167 | * |
||
| 168 | * @var array |
||
| 169 | */ |
||
| 170 | private $primes; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Exponents for Chinese Remainder Theorem (ie. dP and dQ). |
||
| 174 | * |
||
| 175 | * @var array |
||
| 176 | */ |
||
| 177 | private $exponents; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Coefficients for Chinese Remainder Theorem (ie. qInv). |
||
| 181 | * |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | private $coefficients; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Hash name. |
||
| 188 | * |
||
| 189 | * @var string |
||
| 190 | */ |
||
| 191 | private $hashName; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Hash function. |
||
| 195 | * |
||
| 196 | * @var \Jose\Util\Hash |
||
| 197 | */ |
||
| 198 | private $hash; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Length of hash function output. |
||
| 202 | * |
||
| 203 | * @var int |
||
| 204 | */ |
||
| 205 | private $hLen; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Length of salt. |
||
| 209 | * |
||
| 210 | * @var int |
||
| 211 | */ |
||
| 212 | private $sLen; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Hash function for the Mask Generation Function. |
||
| 216 | * |
||
| 217 | * @var \Jose\Util\Hash |
||
| 218 | */ |
||
| 219 | private $mgfHash; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Length of MGF hash function output. |
||
| 223 | * |
||
| 224 | * @var int |
||
| 225 | */ |
||
| 226 | private $mgfHLen; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Encryption mode. |
||
| 230 | * |
||
| 231 | * @var int |
||
| 232 | */ |
||
| 233 | private $encryptionMode = self::ENCRYPTION_OAEP; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Signature mode. |
||
| 237 | * |
||
| 238 | * @var int |
||
| 239 | */ |
||
| 240 | private $signatureMode = self::SIGNATURE_PSS; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Public Exponent. |
||
| 244 | * |
||
| 245 | * @var mixed |
||
| 246 | */ |
||
| 247 | private $publicExponent = false; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Password. |
||
| 251 | * |
||
| 252 | * @var string |
||
| 253 | */ |
||
| 254 | private $password = false; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Components. |
||
| 258 | * |
||
| 259 | * @var array |
||
| 260 | */ |
||
| 261 | private $components = []; |
||
|
|
|||
| 262 | |||
| 263 | /** |
||
| 264 | * Current String. |
||
| 265 | * |
||
| 266 | * @var mixed |
||
| 267 | */ |
||
| 268 | private $current; |
||
| 269 | |||
| 270 | /** |
||
| 271 | * OpenSSL configuration file name. |
||
| 272 | * |
||
| 273 | * @var mixed |
||
| 274 | */ |
||
| 275 | private $configFile; |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Public key comment field. |
||
| 279 | * |
||
| 280 | * @var string |
||
| 281 | */ |
||
| 282 | private $comment = 'phpseclib-generated-key'; |
||
| 283 | |||
| 284 | /** |
||
| 285 | * RSA constructor. |
||
| 286 | */ |
||
| 287 | public function __construct() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Break a public or private key down into its constituant components. |
||
| 351 | * |
||
| 352 | * @param string $key |
||
| 353 | * @param int $type |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | private function _parseKey($key, $type) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Data Handler. |
||
| 460 | * |
||
| 461 | * Called by xml_set_character_data_handler() |
||
| 462 | * |
||
| 463 | * @param resource $parser |
||
| 464 | * @param string $data |
||
| 465 | */ |
||
| 466 | public function _data_handler($parser, $data) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Loads a public or private key. |
||
| 476 | * |
||
| 477 | * @param string $key |
||
| 478 | * @param bool $type optional |
||
| 479 | * |
||
| 480 | * @return bool |
||
| 481 | */ |
||
| 482 | public function loadKey($key, $type = false) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * DER-decode the length. |
||
| 589 | * |
||
| 590 | * @param string $string |
||
| 591 | * |
||
| 592 | * @return int |
||
| 593 | */ |
||
| 594 | private function _decodeLength(&$string) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * String Shift. |
||
| 608 | * |
||
| 609 | * @param string $string |
||
| 610 | * @param int $index |
||
| 611 | * |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | private function _string_shift(&$string, $index = 1) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Determines which hashing function should be used. |
||
| 624 | * |
||
| 625 | * @param string $hash |
||
| 626 | */ |
||
| 627 | public function setHash($hash) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Determines which hashing function should be used for the mask generation function. |
||
| 650 | * |
||
| 651 | * @param string $hash |
||
| 652 | */ |
||
| 653 | public function setMGFHash($hash) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Determines the salt length. |
||
| 676 | * |
||
| 677 | * @param int $sLen |
||
| 678 | */ |
||
| 679 | public function setSaltLength($sLen) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Integer-to-Octet-String primitive. |
||
| 686 | * |
||
| 687 | * @param \Jose\Util\BigInteger $x |
||
| 688 | * @param int $xLen |
||
| 689 | * |
||
| 690 | * @return string |
||
| 691 | */ |
||
| 692 | private function _i2osp($x, $xLen) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Octet-String-to-Integer primitive. |
||
| 706 | * |
||
| 707 | * @param string $x |
||
| 708 | * |
||
| 709 | * @return \Jose\Util\BigInteger |
||
| 710 | */ |
||
| 711 | private function _os2ip($x) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Exponentiate with or without Chinese Remainder Theorem. |
||
| 718 | * |
||
| 719 | * @param \Jose\Util\BigInteger $x |
||
| 720 | * |
||
| 721 | * @return \Jose\Util\BigInteger |
||
| 722 | */ |
||
| 723 | private function _exponentiate($x) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Performs RSA Blinding. |
||
| 793 | * |
||
| 794 | * @param \Jose\Util\BigInteger $x |
||
| 795 | * @param \Jose\Util\BigInteger $r |
||
| 796 | * @param int $i |
||
| 797 | * |
||
| 798 | * @return \Jose\Util\BigInteger |
||
| 799 | */ |
||
| 800 | private function _blind($x, $r, $i) |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Performs blinded RSA equality testing. |
||
| 814 | * |
||
| 815 | * @param string $x |
||
| 816 | * @param string $y |
||
| 817 | * |
||
| 818 | * @return bool |
||
| 819 | */ |
||
| 820 | private function _equals($x, $y) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * RSAEP. |
||
| 836 | * |
||
| 837 | * @param \Jose\Util\BigInteger $m |
||
| 838 | * |
||
| 839 | * @return \Jose\Util\BigInteger |
||
| 840 | */ |
||
| 841 | private function _rsaep($m) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * RSADP. |
||
| 854 | * |
||
| 855 | * @param \Jose\Util\BigInteger $c |
||
| 856 | * |
||
| 857 | * @return \Jose\Util\BigInteger |
||
| 858 | */ |
||
| 859 | private function _rsadp($c) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * RSASP1. |
||
| 872 | * |
||
| 873 | * @param \Jose\Util\BigInteger $m |
||
| 874 | * |
||
| 875 | * @return \Jose\Util\BigInteger |
||
| 876 | */ |
||
| 877 | private function _rsasp1($m) |
||
| 887 | |||
| 888 | /** |
||
| 889 | * RSAVP1. |
||
| 890 | * |
||
| 891 | * @param \Jose\Util\BigInteger $s |
||
| 892 | * |
||
| 893 | * @return \Jose\Util\BigInteger |
||
| 894 | */ |
||
| 895 | private function _rsavp1($s) |
||
| 905 | |||
| 906 | /** |
||
| 907 | * MGF1. |
||
| 908 | * |
||
| 909 | * @param string $mgfSeed |
||
| 910 | * @param int $maskLen |
||
| 911 | * |
||
| 912 | * @return string |
||
| 913 | */ |
||
| 914 | private function _mgf1($mgfSeed, $maskLen) |
||
| 927 | |||
| 928 | /** |
||
| 929 | * RSAES-OAEP-ENCRYPT. |
||
| 930 | * |
||
| 931 | * @param string $m |
||
| 932 | * @param string $l |
||
| 933 | * |
||
| 934 | * @return string |
||
| 935 | */ |
||
| 936 | private function _rsaes_oaep_encrypt($m, $l = '') |
||
| 973 | |||
| 974 | /** |
||
| 975 | * RSAES-OAEP-DECRYPT. |
||
| 976 | * |
||
| 977 | * @param string $c |
||
| 978 | * @param string $l |
||
| 979 | * |
||
| 980 | * @return string |
||
| 981 | */ |
||
| 982 | private function _rsaes_oaep_decrypt($c, $l = '') |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * EMSA-PSS-ENCODE. |
||
| 1037 | * |
||
| 1038 | * @param string $m |
||
| 1039 | * @param int $emBits |
||
| 1040 | * |
||
| 1041 | * @return bool |
||
| 1042 | */ |
||
| 1043 | private function _emsa_pss_encode($m, $emBits) |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * EMSA-PSS-VERIFY. |
||
| 1073 | * |
||
| 1074 | * @param string $m |
||
| 1075 | * @param string $em |
||
| 1076 | * @param int $emBits |
||
| 1077 | * |
||
| 1078 | * @return string |
||
| 1079 | */ |
||
| 1080 | private function _emsa_pss_verify($m, $em, $emBits) |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * RSASSA-PSS-SIGN. |
||
| 1119 | * |
||
| 1120 | * @param string $m |
||
| 1121 | * |
||
| 1122 | * @return string |
||
| 1123 | */ |
||
| 1124 | private function _rsassa_pss_sign($m) |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * RSASSA-PSS-VERIFY. |
||
| 1143 | * |
||
| 1144 | * @param string $m |
||
| 1145 | * @param string $s |
||
| 1146 | * |
||
| 1147 | * @return string |
||
| 1148 | */ |
||
| 1149 | private function _rsassa_pss_verify($m, $s) |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * Set Encryption Mode. |
||
| 1184 | * |
||
| 1185 | * Valid values include self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1. |
||
| 1186 | * |
||
| 1187 | * @param int $mode |
||
| 1188 | */ |
||
| 1189 | public function setEncryptionMode($mode) |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Set Signature Mode. |
||
| 1196 | * |
||
| 1197 | * Valid values include self::SIGNATURE_PSS and self::SIGNATURE_PKCS1 |
||
| 1198 | * |
||
| 1199 | * @param int $mode |
||
| 1200 | */ |
||
| 1201 | public function setSignatureMode($mode) |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Encryption. |
||
| 1208 | * |
||
| 1209 | * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. |
||
| 1210 | * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will |
||
| 1211 | * be concatenated together. |
||
| 1212 | * |
||
| 1213 | * @see self::decrypt() |
||
| 1214 | * |
||
| 1215 | * @param string $plaintext |
||
| 1216 | * |
||
| 1217 | * @return string |
||
| 1218 | */ |
||
| 1219 | public function encrypt($plaintext) |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Decryption. |
||
| 1237 | * |
||
| 1238 | * @param string $ciphertext |
||
| 1239 | * |
||
| 1240 | * @return string |
||
| 1241 | */ |
||
| 1242 | public function decrypt($ciphertext) |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Create a signature. |
||
| 1266 | * |
||
| 1267 | * @param string $message |
||
| 1268 | * |
||
| 1269 | * @return string |
||
| 1270 | */ |
||
| 1271 | public function sign($message) |
||
| 1280 | |||
| 1281 | /** |
||
| 1282 | * Verifies a signature. |
||
| 1283 | * |
||
| 1284 | * @param string $message |
||
| 1285 | * @param string $signature |
||
| 1286 | * |
||
| 1287 | * @return bool |
||
| 1288 | */ |
||
| 1289 | public function verify($message, $signature) |
||
| 1297 | |||
| 1298 | /** |
||
| 1299 | * Extract raw BER from Base64 encoding. |
||
| 1300 | * |
||
| 1301 | * @param string $str |
||
| 1302 | * |
||
| 1303 | * @return string |
||
| 1304 | */ |
||
| 1305 | private function _extractBER($str) |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Defines the public key. |
||
| 1319 | * |
||
| 1320 | * @param string $key optional |
||
| 1321 | * |
||
| 1322 | * @return bool |
||
| 1323 | */ |
||
| 1324 | private function setPublicKey($key = false) |
||
| 1332 | } |
||
| 1333 |
This check marks private properties in classes that are never used. Those properties can be removed.