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 | |||
| 81 | /** |
||
| 82 | * Precomputed Zero. |
||
| 83 | * |
||
| 84 | * @var \Jose\Util\BigInteger |
||
| 85 | */ |
||
| 86 | private $zero; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Precomputed One. |
||
| 90 | * |
||
| 91 | * @var \Jose\Util\BigInteger |
||
| 92 | */ |
||
| 93 | private $one; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Modulus (ie. n). |
||
| 97 | * |
||
| 98 | * @var \Jose\Util\BigInteger |
||
| 99 | */ |
||
| 100 | private $modulus; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Modulus length. |
||
| 104 | * |
||
| 105 | * @var int |
||
| 106 | */ |
||
| 107 | private $k; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Exponent (ie. e or d). |
||
| 111 | * |
||
| 112 | * @var \Jose\Util\BigInteger |
||
| 113 | */ |
||
| 114 | private $exponent; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Primes for Chinese Remainder Theorem (ie. p and q). |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private $primes; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Exponents for Chinese Remainder Theorem (ie. dP and dQ). |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | private $exponents; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Coefficients for Chinese Remainder Theorem (ie. qInv). |
||
| 132 | * |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | private $coefficients; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Hash name. |
||
| 139 | * |
||
| 140 | * @var string |
||
| 141 | */ |
||
| 142 | private $hashName; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Hash function. |
||
| 146 | * |
||
| 147 | * @var \Jose\Util\Hash |
||
| 148 | */ |
||
| 149 | private $hash; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Length of hash function output. |
||
| 153 | * |
||
| 154 | * @var int |
||
| 155 | */ |
||
| 156 | private $hLen; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Length of salt. |
||
| 160 | * |
||
| 161 | * @var int |
||
| 162 | */ |
||
| 163 | private $sLen; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Hash function for the Mask Generation Function. |
||
| 167 | * |
||
| 168 | * @var \Jose\Util\Hash |
||
| 169 | */ |
||
| 170 | private $mgfHash; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Length of MGF hash function output. |
||
| 174 | * |
||
| 175 | * @var int |
||
| 176 | */ |
||
| 177 | private $mgfHLen; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Public Exponent. |
||
| 181 | * |
||
| 182 | * @var mixed |
||
| 183 | */ |
||
| 184 | private $publicExponent = false; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * RSA constructor. |
||
| 188 | */ |
||
| 189 | public function __construct() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Break a public or private key down into its constituant components. |
||
| 203 | * |
||
| 204 | * @param string $key |
||
| 205 | * @param int $type |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | private function _parseKey($key, $type) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Loads a public or private key. |
||
| 312 | * |
||
| 313 | * @param string $key |
||
| 314 | * @param bool $type optional |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public function loadKey($key, $type = false) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * DER-decode the length. |
||
| 352 | * |
||
| 353 | * @param string $string |
||
| 354 | * |
||
| 355 | * @return int |
||
| 356 | */ |
||
| 357 | private function _decodeLength(&$string) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * String Shift. |
||
| 371 | * |
||
| 372 | * @param string $string |
||
| 373 | * @param int $index |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | private function _string_shift(&$string, $index = 1) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Determines which hashing function should be used. |
||
| 387 | * |
||
| 388 | * @param string $hash |
||
| 389 | */ |
||
| 390 | public function setHash($hash) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Determines which hashing function should be used for the mask generation function. |
||
| 413 | * |
||
| 414 | * @param string $hash |
||
| 415 | */ |
||
| 416 | public function setMGFHash($hash) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Determines the salt length. |
||
| 439 | * |
||
| 440 | * @param int $sLen |
||
| 441 | */ |
||
| 442 | public function setSaltLength($sLen) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Integer-to-Octet-String primitive. |
||
| 449 | * |
||
| 450 | * @param \Jose\Util\BigInteger $x |
||
| 451 | * @param int $xLen |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | private function _i2osp($x, $xLen) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Octet-String-to-Integer primitive. |
||
| 468 | * |
||
| 469 | * @param string $x |
||
| 470 | * |
||
| 471 | * @return \Jose\Util\BigInteger |
||
| 472 | */ |
||
| 473 | private function _os2ip($x) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Exponentiate with or without Chinese Remainder Theorem. |
||
| 480 | * |
||
| 481 | * @param \Jose\Util\BigInteger $x |
||
| 482 | * |
||
| 483 | * @return \Jose\Util\BigInteger |
||
| 484 | */ |
||
| 485 | private function _exponentiate($x) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Performs RSA Blinding. |
||
| 531 | * |
||
| 532 | * @param \Jose\Util\BigInteger $x |
||
| 533 | * @param \Jose\Util\BigInteger $r |
||
| 534 | * @param int $i |
||
| 535 | * |
||
| 536 | * @return \Jose\Util\BigInteger |
||
| 537 | */ |
||
| 538 | private function _blind($x, $r, $i) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Performs blinded RSA equality testing. |
||
| 552 | * |
||
| 553 | * @param string $x |
||
| 554 | * @param string $y |
||
| 555 | * |
||
| 556 | * @return bool |
||
| 557 | */ |
||
| 558 | private function _equals($x, $y) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * RSAEP. |
||
| 574 | * |
||
| 575 | * @param \Jose\Util\BigInteger $m |
||
| 576 | * |
||
| 577 | * @return \Jose\Util\BigInteger |
||
| 578 | */ |
||
| 579 | private function _rsaep($m) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * RSADP. |
||
| 591 | * |
||
| 592 | * @param \Jose\Util\BigInteger $c |
||
| 593 | * |
||
| 594 | * @return \Jose\Util\BigInteger |
||
| 595 | */ |
||
| 596 | private function _rsadp($c) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * RSASP1. |
||
| 608 | * |
||
| 609 | * @param \Jose\Util\BigInteger $m |
||
| 610 | * |
||
| 611 | * @return \Jose\Util\BigInteger |
||
| 612 | */ |
||
| 613 | private function _rsasp1($m) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * RSAVP1. |
||
| 625 | * |
||
| 626 | * @param \Jose\Util\BigInteger $s |
||
| 627 | * |
||
| 628 | * @return \Jose\Util\BigInteger |
||
| 629 | */ |
||
| 630 | private function _rsavp1($s) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * MGF1. |
||
| 642 | * |
||
| 643 | * @param string $mgfSeed |
||
| 644 | * @param int $maskLen |
||
| 645 | * |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | private function _mgf1($mgfSeed, $maskLen) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * RSAES-OAEP-ENCRYPT. |
||
| 664 | * |
||
| 665 | * @param string $m |
||
| 666 | * @param string $l |
||
| 667 | * |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | private function _rsaes_oaep_encrypt($m, $l = '') |
||
| 706 | |||
| 707 | /** |
||
| 708 | * RSAES-OAEP-DECRYPT. |
||
| 709 | * |
||
| 710 | * @param string $c |
||
| 711 | * @param string $l |
||
| 712 | * |
||
| 713 | * @return string |
||
| 714 | */ |
||
| 715 | private function _rsaes_oaep_decrypt($c, $l = '') |
||
| 763 | |||
| 764 | /** |
||
| 765 | * EMSA-PSS-ENCODE. |
||
| 766 | * |
||
| 767 | * @param string $m |
||
| 768 | * @param int $emBits |
||
| 769 | * |
||
| 770 | * @return bool |
||
| 771 | */ |
||
| 772 | private function _emsa_pss_encode($m, $emBits) |
||
| 798 | |||
| 799 | /** |
||
| 800 | * EMSA-PSS-VERIFY. |
||
| 801 | * |
||
| 802 | * @param string $m |
||
| 803 | * @param string $em |
||
| 804 | * @param int $emBits |
||
| 805 | * |
||
| 806 | * @return string |
||
| 807 | */ |
||
| 808 | private function _emsa_pss_verify($m, $em, $emBits) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * RSASSA-PSS-SIGN. |
||
| 847 | * |
||
| 848 | * @param string $m |
||
| 849 | * |
||
| 850 | * @return string |
||
| 851 | */ |
||
| 852 | private function _rsassa_pss_sign($m) |
||
| 868 | |||
| 869 | /** |
||
| 870 | * RSASSA-PSS-VERIFY. |
||
| 871 | * |
||
| 872 | * @param string $m |
||
| 873 | * @param string $s |
||
| 874 | * |
||
| 875 | * @return string |
||
| 876 | */ |
||
| 877 | private function _rsassa_pss_verify($m, $s) |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Encryption. |
||
| 909 | * |
||
| 910 | * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. |
||
| 911 | * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will |
||
| 912 | * be concatenated together. |
||
| 913 | * |
||
| 914 | * @see self::decrypt() |
||
| 915 | * |
||
| 916 | * @param string $plaintext |
||
| 917 | * |
||
| 918 | * @return string |
||
| 919 | */ |
||
| 920 | public function encrypt($plaintext) |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Decryption. |
||
| 938 | * |
||
| 939 | * @param string $ciphertext |
||
| 940 | * |
||
| 941 | * @return string |
||
| 942 | */ |
||
| 943 | public function decrypt($ciphertext) |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Create a signature. |
||
| 967 | * |
||
| 968 | * @param string $message |
||
| 969 | * |
||
| 970 | * @return string |
||
| 971 | */ |
||
| 972 | public function sign($message) |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Verifies a signature. |
||
| 984 | * |
||
| 985 | * @param string $message |
||
| 986 | * @param string $signature |
||
| 987 | * |
||
| 988 | * @return bool |
||
| 989 | */ |
||
| 990 | public function verify($message, $signature) |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Extract raw BER from Base64 encoding. |
||
| 1001 | * |
||
| 1002 | * @param string $str |
||
| 1003 | * |
||
| 1004 | * @return string |
||
| 1005 | */ |
||
| 1006 | private function _extractBER($str) |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Defines the public key. |
||
| 1020 | * |
||
| 1021 | * @return bool |
||
| 1022 | */ |
||
| 1023 | private function setPublicKey() |
||
| 1031 | } |
||
| 1032 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: