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 | * Public Exponent. |
||
| 77 | * |
||
| 78 | * @var mixed |
||
| 79 | */ |
||
| 80 | private $publicExponent = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * RSA constructor. |
||
| 84 | */ |
||
| 85 | public function __construct() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Loads a public or private key. |
||
| 93 | * |
||
| 94 | * @param \Jose\Object\JWKInterface $key |
||
| 95 | */ |
||
| 96 | public function loadKey(JWKInterface $key) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param \Jose\Object\JWKInterface $jwk |
||
| 130 | * @param string $key |
||
| 131 | * |
||
| 132 | * @return \Jose\Util\BigInteger |
||
| 133 | */ |
||
| 134 | public function getKeyComponent(JWKInterface $jwk, $key) |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Integer-to-Octet-String primitive. |
||
| 142 | * |
||
| 143 | * @param \Jose\Util\BigInteger $x |
||
| 144 | * @param int $xLen |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | private function convertIntegerToOctetString($x, $xLen) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Octet-String-to-Integer primitive. |
||
| 160 | * |
||
| 161 | * @param string $x |
||
| 162 | * |
||
| 163 | * @return \Jose\Util\BigInteger |
||
| 164 | */ |
||
| 165 | private function convertOctetStringToInteger($x) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Exponentiate with or without Chinese Remainder Theorem. |
||
| 172 | * |
||
| 173 | * @param \Jose\Util\BigInteger $x |
||
| 174 | * |
||
| 175 | * @return \Jose\Util\BigInteger |
||
| 176 | */ |
||
| 177 | private function _exponentiate($x) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Performs RSA Blinding. |
||
| 223 | * |
||
| 224 | * @param \Jose\Util\BigInteger $x |
||
| 225 | * @param \Jose\Util\BigInteger $r |
||
| 226 | * @param int $i |
||
| 227 | * |
||
| 228 | * @return \Jose\Util\BigInteger |
||
| 229 | */ |
||
| 230 | private function _blind($x, $r, $i) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Performs blinded RSA equality testing. |
||
| 244 | * |
||
| 245 | * @param string $x |
||
| 246 | * @param string $y |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | private function _equals($x, $y) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * RSAEP. |
||
| 266 | * |
||
| 267 | * @param \Jose\Util\BigInteger $m |
||
| 268 | * |
||
| 269 | * @return \Jose\Util\BigInteger|false |
||
| 270 | */ |
||
| 271 | private function _rsaep($m) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * RSADP. |
||
| 282 | * |
||
| 283 | * @param \Jose\Util\BigInteger $c |
||
| 284 | * |
||
| 285 | * @return \Jose\Util\BigInteger|false |
||
| 286 | */ |
||
| 287 | private function _rsadp($c) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * RSASP1. |
||
| 298 | * |
||
| 299 | * @param \Jose\Util\BigInteger $m |
||
| 300 | * |
||
| 301 | * @return \Jose\Util\BigInteger|false |
||
| 302 | */ |
||
| 303 | private function _rsasp1($m) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * RSAVP1. |
||
| 314 | * |
||
| 315 | * @param \Jose\Util\BigInteger $s |
||
| 316 | * |
||
| 317 | * @return \Jose\Util\BigInteger|false |
||
| 318 | */ |
||
| 319 | private function _rsavp1($s) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * MGF1. |
||
| 330 | * |
||
| 331 | * @param string $mgfSeed |
||
| 332 | * @param int $maskLen |
||
| 333 | * @param \Jose\Util\Hash $mgfHash |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | private function _mgf1($mgfSeed, $maskLen, Hash $mgfHash) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * RSAES-OAEP-ENCRYPT. |
||
| 353 | * |
||
| 354 | * @param string $m |
||
| 355 | * @param \Jose\Util\Hash $hash |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | private function _rsaes_oaep_encrypt($m, Hash $hash) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * RSAES-OAEP-DECRYPT. |
||
| 397 | * |
||
| 398 | * @param string $c |
||
| 399 | * @param \Jose\Util\Hash $hash |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | private function _rsaes_oaep_decrypt($c, Hash $hash) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * EMSA-PSS-ENCODE. |
||
| 449 | * |
||
| 450 | * @param string $m |
||
| 451 | * @param int $emBits |
||
| 452 | * @param \Jose\Util\Hash $hash |
||
| 453 | * |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | private function _emsa_pss_encode($m, $emBits, Hash $hash) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * EMSA-PSS-VERIFY. |
||
| 484 | * |
||
| 485 | * @param string $m |
||
| 486 | * @param string $em |
||
| 487 | * @param int $emBits |
||
| 488 | * @param \Jose\Util\Hash $hash |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | private function _emsa_pss_verify($m, $em, $emBits, Hash $hash) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * RSASSA-PSS-SIGN. |
||
| 531 | * |
||
| 532 | * @param string $m |
||
| 533 | * @param \Jose\Util\Hash $hash |
||
| 534 | * |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | private function _rsassa_pss_sign($m, Hash $hash) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * RSASSA-PSS-VERIFY. |
||
| 556 | * |
||
| 557 | * @param string $m |
||
| 558 | * @param string $s |
||
| 559 | * @param \Jose\Util\Hash $hash |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | private function _rsassa_pss_verify($m, $s, Hash $hash) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Encryption. |
||
| 592 | * |
||
| 593 | * Both self::ENCRYPTION_OAEP and self::ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. |
||
| 594 | * If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will |
||
| 595 | * be concatenated together. |
||
| 596 | * |
||
| 597 | * @see self::decrypt() |
||
| 598 | * |
||
| 599 | * @param string $plaintext |
||
| 600 | * @param string $hash_algorithm |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | public function encrypt($plaintext, $hash_algorithm) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Decryption. |
||
| 623 | * |
||
| 624 | * @param string $ciphertext |
||
| 625 | * @param string $hash_algorithm |
||
| 626 | * |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | public function decrypt($ciphertext, $hash_algorithm) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Create a signature. |
||
| 655 | * |
||
| 656 | * @param string $message |
||
| 657 | * @param string $hash |
||
| 658 | * |
||
| 659 | * @return string |
||
| 660 | */ |
||
| 661 | public function sign($message, $hash) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Verifies a signature. |
||
| 672 | * |
||
| 673 | * @param string $message |
||
| 674 | * @param string $signature |
||
| 675 | * @param string $hash |
||
| 676 | * |
||
| 677 | * @return bool |
||
| 678 | */ |
||
| 679 | public function verify($message, $signature, $hash) |
||
| 687 | } |
||
| 688 |