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 | * ASN1 Integer. |
||
| 21 | */ |
||
| 22 | const ASN1_INTEGER = 2; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * ASN1 Bit String. |
||
| 26 | */ |
||
| 27 | const ASN1_BITSTRING = 3; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * ASN1 Octet String. |
||
| 31 | */ |
||
| 32 | const ASN1_OCTETSTRING = 4; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * ASN1 Object Identifier. |
||
| 36 | */ |
||
| 37 | const ASN1_OBJECT = 6; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * ASN1 Sequence (with the constucted bit set). |
||
| 41 | */ |
||
| 42 | const ASN1_SEQUENCE = 48; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * To use the pure-PHP implementation. |
||
| 46 | */ |
||
| 47 | const MODE_INTERNAL = 1; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * To use the OpenSSL library. |
||
| 51 | */ |
||
| 52 | const MODE_OPENSSL = 2; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * PKCS#1 formatted private key. |
||
| 56 | */ |
||
| 57 | const PRIVATE_FORMAT_PKCS1 = 0; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * PuTTY formatted private key. |
||
| 61 | */ |
||
| 62 | const PRIVATE_FORMAT_PUTTY = 1; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * XML formatted private key. |
||
| 66 | */ |
||
| 67 | const PRIVATE_FORMAT_XML = 2; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * PKCS#8 formatted private key. |
||
| 71 | */ |
||
| 72 | const PRIVATE_FORMAT_PKCS8 = 8; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Raw public key. |
||
| 76 | */ |
||
| 77 | const PUBLIC_FORMAT_RAW = 3; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * PKCS#1 formatted public key (raw). |
||
| 81 | */ |
||
| 82 | const PUBLIC_FORMAT_PKCS1 = 4; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Precomputed Zero. |
||
| 86 | * |
||
| 87 | * @var \Jose\Util\BigInteger |
||
| 88 | */ |
||
| 89 | private $zero; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Precomputed One. |
||
| 93 | * |
||
| 94 | * @var \Jose\Util\BigInteger |
||
| 95 | */ |
||
| 96 | private $one; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Modulus (ie. n). |
||
| 100 | * |
||
| 101 | * @var \Jose\Util\BigInteger |
||
| 102 | */ |
||
| 103 | private $modulus; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Modulus length. |
||
| 107 | * |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | private $k; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Exponent (ie. e or d). |
||
| 114 | * |
||
| 115 | * @var \Jose\Util\BigInteger |
||
| 116 | */ |
||
| 117 | private $exponent; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Primes for Chinese Remainder Theorem (ie. p and q). |
||
| 121 | * |
||
| 122 | * @var \Jose\Util\BigInteger[] |
||
| 123 | */ |
||
| 124 | private $primes; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Exponents for Chinese Remainder Theorem (ie. dP and dQ). |
||
| 128 | * |
||
| 129 | * @var \Jose\Util\BigInteger[] |
||
| 130 | */ |
||
| 131 | private $exponents; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Coefficients for Chinese Remainder Theorem (ie. qInv). |
||
| 135 | * |
||
| 136 | * @var \Jose\Util\BigInteger[] |
||
| 137 | */ |
||
| 138 | private $coefficients; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Hash function. |
||
| 142 | * |
||
| 143 | * @var \Jose\Util\Hash |
||
| 144 | */ |
||
| 145 | private $hash; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Length of salt. |
||
| 149 | * |
||
| 150 | * @var int |
||
| 151 | */ |
||
| 152 | private $sLen; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Hash function for the Mask Generation Function. |
||
| 156 | * |
||
| 157 | * @var \Jose\Util\Hash |
||
| 158 | */ |
||
| 159 | private $mgfHash; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Public Exponent. |
||
| 163 | * |
||
| 164 | * @var mixed |
||
| 165 | */ |
||
| 166 | private $publicExponent = false; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * RSA constructor. |
||
| 170 | */ |
||
| 171 | public function __construct() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Loads a public or private key. |
||
| 182 | * |
||
| 183 | * @param \Jose\Object\JWKInterface $key |
||
| 184 | */ |
||
| 185 | public function loadKey(JWKInterface $key) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Determines which hashing function should be used. |
||
| 219 | * |
||
| 220 | * @param string $hash |
||
| 221 | */ |
||
| 222 | public function setHash($hash) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Determines which hashing function should be used for the mask generation function. |
||
| 229 | * |
||
| 230 | * @param string $hash |
||
| 231 | */ |
||
| 232 | public function setMGFHash($hash) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Determines the salt length. |
||
| 239 | * |
||
| 240 | * @param int $sLen |
||
| 241 | */ |
||
| 242 | public function setSaltLength($sLen) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Integer-to-Octet-String primitive. |
||
| 249 | * |
||
| 250 | * @param \Jose\Util\BigInteger $x |
||
| 251 | * @param int $xLen |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | private function _i2osp($x, $xLen) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Octet-String-to-Integer primitive. |
||
| 268 | * |
||
| 269 | * @param string $x |
||
| 270 | * |
||
| 271 | * @return \Jose\Util\BigInteger |
||
| 272 | */ |
||
| 273 | private function _os2ip($x) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Exponentiate with or without Chinese Remainder Theorem. |
||
| 280 | * |
||
| 281 | * @param \Jose\Util\BigInteger $x |
||
| 282 | * |
||
| 283 | * @return \Jose\Util\BigInteger |
||
| 284 | */ |
||
| 285 | private function _exponentiate($x) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Performs RSA Blinding. |
||
| 331 | * |
||
| 332 | * @param \Jose\Util\BigInteger $x |
||
| 333 | * @param \Jose\Util\BigInteger $r |
||
| 334 | * @param int $i |
||
| 335 | * |
||
| 336 | * @return \Jose\Util\BigInteger |
||
| 337 | */ |
||
| 338 | private function _blind($x, $r, $i) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Performs blinded RSA equality testing. |
||
| 352 | * |
||
| 353 | * @param string $x |
||
| 354 | * @param string $y |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | private function _equals($x, $y) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * RSAEP. |
||
| 374 | * |
||
| 375 | * @param \Jose\Util\BigInteger $m |
||
| 376 | * |
||
| 377 | * @return \Jose\Util\BigInteger |
||
| 378 | */ |
||
| 379 | private function _rsaep($m) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * RSADP. |
||
| 391 | * |
||
| 392 | * @param \Jose\Util\BigInteger $c |
||
| 393 | * |
||
| 394 | * @return \Jose\Util\BigInteger |
||
| 395 | */ |
||
| 396 | private function _rsadp($c) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * RSASP1. |
||
| 408 | * |
||
| 409 | * @param \Jose\Util\BigInteger $m |
||
| 410 | * |
||
| 411 | * @return \Jose\Util\BigInteger |
||
| 412 | */ |
||
| 413 | private function _rsasp1($m) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * RSAVP1. |
||
| 425 | * |
||
| 426 | * @param \Jose\Util\BigInteger $s |
||
| 427 | * |
||
| 428 | * @return \Jose\Util\BigInteger |
||
| 429 | */ |
||
| 430 | private function _rsavp1($s) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * MGF1. |
||
| 442 | * |
||
| 443 | * @param string $mgfSeed |
||
| 444 | * @param int $maskLen |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | private function _mgf1($mgfSeed, $maskLen) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * RSAES-OAEP-ENCRYPT. |
||
| 464 | * |
||
| 465 | * @param string $m |
||
| 466 | * @param string $l |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | private function _rsaes_oaep_encrypt($m, $l = '') |
||
| 506 | |||
| 507 | /** |
||
| 508 | * RSAES-OAEP-DECRYPT. |
||
| 509 | * |
||
| 510 | * @param string $c |
||
| 511 | * @param string $l |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | private function _rsaes_oaep_decrypt($c, $l = '') |
||
| 563 | |||
| 564 | /** |
||
| 565 | * EMSA-PSS-ENCODE. |
||
| 566 | * |
||
| 567 | * @param string $m |
||
| 568 | * @param int $emBits |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | private function _emsa_pss_encode($m, $emBits) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * EMSA-PSS-VERIFY. |
||
| 601 | * |
||
| 602 | * @param string $m |
||
| 603 | * @param string $em |
||
| 604 | * @param int $emBits |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | private function _emsa_pss_verify($m, $em, $emBits) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * RSASSA-PSS-SIGN. |
||
| 647 | * |
||
| 648 | * @param string $m |
||
| 649 | * |
||
| 650 | * @return string |
||
| 651 | */ |
||
| 652 | private function _rsassa_pss_sign($m) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * RSASSA-PSS-VERIFY. |
||
| 671 | * |
||
| 672 | * @param string $m |
||
| 673 | * @param string $s |
||
| 674 | * |
||
| 675 | * @return string |
||
| 676 | */ |
||
| 677 | private function _rsassa_pss_verify($m, $s) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Encryption. |
||
| 709 | * |
||
| 710 | * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. |
||
| 711 | * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will |
||
| 712 | * be concatenated together. |
||
| 713 | * |
||
| 714 | * @see self::decrypt() |
||
| 715 | * |
||
| 716 | * @param string $plaintext |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | public function encrypt($plaintext) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Decryption. |
||
| 738 | * |
||
| 739 | * @param string $ciphertext |
||
| 740 | * |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | public function decrypt($ciphertext) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Create a signature. |
||
| 767 | * |
||
| 768 | * @param string $message |
||
| 769 | * |
||
| 770 | * @return string |
||
| 771 | */ |
||
| 772 | public function sign($message) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Verifies a signature. |
||
| 784 | * |
||
| 785 | * @param string $message |
||
| 786 | * @param string $signature |
||
| 787 | * |
||
| 788 | * @return bool |
||
| 789 | */ |
||
| 790 | public function verify($message, $signature) |
||
| 798 | } |
||
| 799 |