Complex classes like RSACrypt 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 RSACrypt, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | final class RSACrypt |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Optimal Asymmetric Encryption Padding (OAEP). |
||
| 24 | */ |
||
| 25 | public const ENCRYPTION_OAEP = 1; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Use PKCS#1 padding. |
||
| 29 | */ |
||
| 30 | public const ENCRYPTION_PKCS1 = 2; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param RSAKey $key |
||
| 34 | * @param string $data |
||
| 35 | * @param int $mode |
||
| 36 | * @param null|string $hash |
||
| 37 | * |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public static function encrypt(RSAKey $key, string $data, int $mode, ?string $hash = null): string |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param RSAKey $key |
||
| 54 | * @param string $plaintext |
||
| 55 | * @param int $mode |
||
| 56 | * @param null|string $hash |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public static function decrypt(RSAKey $key, string $plaintext, int $mode, ?string $hash = null): string |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param RSAKey $key |
||
| 74 | * @param string $data |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public static function encryptWithRSA15(RSAKey $key, string $data): string |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param RSAKey $key |
||
| 105 | * @param string $c |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public static function decryptWithRSA15(RSAKey $key, string $c): string |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Encryption. |
||
| 138 | * |
||
| 139 | * @param RSAKey $key |
||
| 140 | * @param string $plaintext |
||
| 141 | * @param string $hash_algorithm |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public static function encryptWithRSAOAEP(RSAKey $key, string $plaintext, string $hash_algorithm): string |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Decryption. |
||
| 164 | * |
||
| 165 | * @param RSAKey $key |
||
| 166 | * @param string $ciphertext |
||
| 167 | * @param string $hash_algorithm |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public static function decryptWithRSAOAEP(RSAKey $key, string $ciphertext, string $hash_algorithm): string |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param BigInteger $x |
||
| 190 | * @param int $xLen |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | private static function convertIntegerToOctetString(BigInteger $x, int $xLen): string |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Octet-String-to-Integer primitive. |
||
| 206 | * |
||
| 207 | * @param string $x |
||
| 208 | * |
||
| 209 | * @return BigInteger |
||
| 210 | */ |
||
| 211 | private static function convertOctetStringToInteger(string $x): BigInteger |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Exponentiate with or without Chinese Remainder Theorem. |
||
| 218 | * Operation with primes 'p' and 'q' is appox. 2x faster. |
||
| 219 | * |
||
| 220 | * @param RSAKey $key |
||
| 221 | * @param BigInteger $c |
||
| 222 | * |
||
| 223 | * @return BigInteger |
||
| 224 | */ |
||
| 225 | private static function exponentiate(RSAKey $key, BigInteger $c): BigInteger |
||
| 226 | { |
||
| 227 | if ($key->isPublic() || empty($key->getPrimes()) || empty($key->getExponents()) || null === $key->getCoefficient()) { |
||
| 228 | return $c->modPow($key->getExponent(), $key->getModulus()); |
||
| 229 | } |
||
| 230 | |||
| 231 | $p = $key->getPrimes()[0]; |
||
| 232 | $q = $key->getPrimes()[1]; |
||
| 233 | $dP = $key->getExponents()[0]; |
||
| 234 | $dQ = $key->getExponents()[1]; |
||
| 235 | $qInv = $key->getCoefficient(); |
||
| 236 | |||
| 237 | $m1 = $c->modPow($dP, $p); |
||
| 238 | $m2 = $c->modPow($dQ, $q); |
||
| 239 | $h = $qInv->multiply($m1->subtract($m2)->add($p))->mod($p); |
||
| 240 | $m = $m2->add($h->multiply($q)); |
||
| 241 | |||
| 242 | return $m; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * RSA EP. |
||
| 247 | * |
||
| 248 | * @param RSAKey $key |
||
| 249 | * @param BigInteger $m |
||
| 250 | * |
||
| 251 | * @return BigInteger |
||
| 252 | */ |
||
| 253 | private static function getRSAEP(RSAKey $key, BigInteger $m): BigInteger |
||
| 261 | |||
| 262 | /** |
||
| 263 | * RSA DP. |
||
| 264 | * |
||
| 265 | * @param RSAKey $key |
||
| 266 | * @param BigInteger $c |
||
| 267 | * |
||
| 268 | * @return BigInteger |
||
| 269 | */ |
||
| 270 | private static function getRSADP(RSAKey $key, BigInteger $c): BigInteger |
||
| 278 | |||
| 279 | /** |
||
| 280 | * MGF1. |
||
| 281 | * |
||
| 282 | * @param string $mgfSeed |
||
| 283 | * @param int $maskLen |
||
| 284 | * @param Hash $mgfHash |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | private static function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string |
||
| 299 | |||
| 300 | /** |
||
| 301 | * RSAES-OAEP-ENCRYPT. |
||
| 302 | * |
||
| 303 | * @param RSAKey $key |
||
| 304 | * @param string $m |
||
| 305 | * @param Hash $hash |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | private static function encryptRSAESOAEP(RSAKey $key, string $m, Hash $hash): string |
||
| 328 | |||
| 329 | /** |
||
| 330 | * RSAES-OAEP-DECRYPT. |
||
| 331 | * |
||
| 332 | * @param RSAKey $key |
||
| 333 | * @param string $c |
||
| 334 | * @param Hash $hash |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | private static function getRSAESOAEP(RSAKey $key, string $c, Hash $hash): string |
||
| 362 | } |
||
| 363 |