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 |
||
| 20 | final class RSA |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Probabilistic Signature Scheme. |
||
| 24 | */ |
||
| 25 | public const SIGNATURE_PSS = 1; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Use the PKCS#1. |
||
| 29 | */ |
||
| 30 | public const SIGNATURE_PKCS1 = 2; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param BigInteger $x |
||
| 34 | * @param int $xLen |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | private static function convertIntegerToOctetString(BigInteger $x, int $xLen): string |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Exponentiate with or without Chinese Remainder Theorem. |
||
| 50 | * Operation with primes 'p' and 'q' is appox. 2x faster. |
||
| 51 | * |
||
| 52 | * @param RSAKey $key |
||
| 53 | * @param BigInteger $c |
||
| 54 | * |
||
| 55 | * @return BigInteger |
||
| 56 | */ |
||
| 57 | private static function exponentiate(RSAKey $key, BigInteger $c): BigInteger |
||
| 58 | { |
||
| 59 | if ($c->compare(BigInteger::createFromDecimal(0)) < 0 || $c->compare($key->getModulus()) > 0) { |
||
| 60 | throw new \RuntimeException(); |
||
| 61 | } |
||
| 62 | if ($key->isPublic() || empty($key->getPrimes()) || empty($key->getExponents()) || null === $key->getCoefficient()) { |
||
| 63 | return $c->modPow($key->getExponent(), $key->getModulus()); |
||
| 64 | } |
||
| 65 | |||
| 66 | $p = $key->getPrimes()[0]; |
||
| 67 | $q = $key->getPrimes()[1]; |
||
| 68 | $dP = $key->getExponents()[0]; |
||
| 69 | $dQ = $key->getExponents()[1]; |
||
| 70 | $qInv = $key->getCoefficient(); |
||
| 71 | |||
| 72 | $m1 = $c->modPow($dP, $p); |
||
| 73 | $m2 = $c->modPow($dQ, $q); |
||
| 74 | $h = $qInv->multiply($m1->subtract($m2)->add($p))->mod($p); |
||
| 75 | $m = $m2->add($h->multiply($q)); |
||
| 76 | |||
| 77 | return $m; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * MGF1. |
||
| 82 | * |
||
| 83 | * @param string $mgfSeed |
||
| 84 | * @param int $maskLen |
||
| 85 | * @param Hash $mgfHash |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | private static function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string |
||
| 100 | |||
| 101 | /** |
||
| 102 | * EMSA-PSS-ENCODE. |
||
| 103 | * |
||
| 104 | * @param string $message |
||
| 105 | * @param int $modulusLength |
||
| 106 | * @param Hash $hash |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | private static function encodeEMSAPSS(string $message, int $modulusLength, Hash $hash): string |
||
| 130 | |||
| 131 | /** |
||
| 132 | * EMSA-PSS-VERIFY. |
||
| 133 | * |
||
| 134 | * @param string $m |
||
| 135 | * @param string $em |
||
| 136 | * @param int $emBits |
||
| 137 | * @param Hash $hash |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | private static function verifyEMSAPSS(string $m, string $em, int $emBits, Hash $hash): bool |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $m |
||
| 177 | * @param int $emBits |
||
| 178 | * @param Hash $hash |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | private static function encodeEMSA15(string $m, int $emBits, Hash $hash): string |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param RSAKey $key |
||
| 211 | * @param string $message |
||
| 212 | * @param string $hash |
||
| 213 | * @param int $mode |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public static function sign(RSAKey $key, string $message, string $hash, int $mode): string |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Create a signature. |
||
| 231 | * |
||
| 232 | * @param RSAKey $key |
||
| 233 | * @param string $message |
||
| 234 | * @param string $hash |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public static function signWithPSS(RSAKey $key, string $message, string $hash): string |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Create a signature. |
||
| 252 | * |
||
| 253 | * @param RSAKey $key |
||
| 254 | * @param string $message |
||
| 255 | * @param string $hash |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public static function signWithPKCS15(RSAKey $key, string $message, string $hash): string |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param RSAKey $key |
||
| 273 | * @param string $message |
||
| 274 | * @param string $signature |
||
| 275 | * @param string $hash |
||
| 276 | * @param int $mode |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public static function verify(RSAKey $key, string $message, string $signature, string $hash, int $mode): bool |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Verifies a signature. |
||
| 294 | * |
||
| 295 | * @param RSAKey $key |
||
| 296 | * @param string $message |
||
| 297 | * @param string $signature |
||
| 298 | * @param string $hash |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public static function verifyWithPSS(RSAKey $key, string $message, string $signature, string $hash): bool |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Verifies a signature. |
||
| 320 | * |
||
| 321 | * @param RSAKey $key |
||
| 322 | * @param string $message |
||
| 323 | * @param string $signature |
||
| 324 | * @param string $hash |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public static function verifyWithPKCS15(RSAKey $key, string $message, string $signature, string $hash): bool |
||
| 342 | } |
||
| 343 |