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 |
||
| 23 | final class RSACrypt |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Optimal Asymmetric Encryption Padding (OAEP). |
||
| 27 | */ |
||
| 28 | public const ENCRYPTION_OAEP = 1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Use PKCS#1 padding. |
||
| 32 | */ |
||
| 33 | public const ENCRYPTION_PKCS1 = 2; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param RSAKey $key |
||
| 37 | * @param string $data |
||
| 38 | * @param int $mode |
||
| 39 | * @param null|string $hash |
||
| 40 | * |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public static function encrypt(RSAKey $key, string $data, int $mode, ?string $hash = null): string |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param RSAKey $key |
||
| 57 | * @param string $plaintext |
||
| 58 | * @param int $mode |
||
| 59 | * @param null|string $hash |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public static function decrypt(RSAKey $key, string $plaintext, int $mode, ?string $hash = null): string |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param RSAKey $key |
||
| 77 | * @param string $data |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public static function encryptWithRSA15(RSAKey $key, string $data): string |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param RSAKey $key |
||
| 108 | * @param string $c |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public static function decryptWithRSA15(RSAKey $key, string $c): string |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Encryption. |
||
| 141 | * |
||
| 142 | * @param RSAKey $key |
||
| 143 | * @param string $plaintext |
||
| 144 | * @param string $hash_algorithm |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public static function encryptWithRSAOAEP(RSAKey $key, string $plaintext, string $hash_algorithm): string |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Decryption. |
||
| 167 | * |
||
| 168 | * @param RSAKey $key |
||
| 169 | * @param string $ciphertext |
||
| 170 | * @param string $hash_algorithm |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public static function decryptWithRSAOAEP(RSAKey $key, string $ciphertext, string $hash_algorithm): string |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param BigInteger $x |
||
| 193 | * @param int $xLen |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | private static function convertIntegerToOctetString(BigInteger $x, int $xLen): string |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Octet-String-to-Integer primitive. |
||
| 209 | * |
||
| 210 | * @param string $x |
||
| 211 | * |
||
| 212 | * @return BigInteger |
||
| 213 | */ |
||
| 214 | private static function convertOctetStringToInteger(string $x): BigInteger |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Exponentiate with or without Chinese Remainder Theorem. |
||
| 221 | * Operation with primes 'p' and 'q' is appox. 2x faster. |
||
| 222 | * |
||
| 223 | * @param RSAKey $key |
||
| 224 | * @param BigInteger $c |
||
| 225 | * |
||
| 226 | * @return BigInteger |
||
| 227 | */ |
||
| 228 | private static function exponentiate(RSAKey $key, BigInteger $c): BigInteger |
||
| 247 | |||
| 248 | /** |
||
| 249 | * RSA EP. |
||
| 250 | * |
||
| 251 | * @param RSAKey $key |
||
| 252 | * @param BigInteger $m |
||
| 253 | * |
||
| 254 | * @return BigInteger |
||
| 255 | */ |
||
| 256 | private static function getRSAEP(RSAKey $key, BigInteger $m): BigInteger |
||
| 264 | |||
| 265 | /** |
||
| 266 | * RSA DP. |
||
| 267 | * |
||
| 268 | * @param RSAKey $key |
||
| 269 | * @param BigInteger $c |
||
| 270 | * |
||
| 271 | * @return BigInteger |
||
| 272 | */ |
||
| 273 | private static function getRSADP(RSAKey $key, BigInteger $c): BigInteger |
||
| 281 | |||
| 282 | /** |
||
| 283 | * MGF1. |
||
| 284 | * |
||
| 285 | * @param string $mgfSeed |
||
| 286 | * @param int $maskLen |
||
| 287 | * @param Hash $mgfHash |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | private static function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string |
||
| 302 | |||
| 303 | /** |
||
| 304 | * RSAES-OAEP-ENCRYPT. |
||
| 305 | * |
||
| 306 | * @param RSAKey $key |
||
| 307 | * @param string $m |
||
| 308 | * @param Hash $hash |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | private static function encryptRSAESOAEP(RSAKey $key, string $m, Hash $hash): string |
||
| 331 | |||
| 332 | /** |
||
| 333 | * RSAES-OAEP-DECRYPT. |
||
| 334 | * |
||
| 335 | * @param RSAKey $key |
||
| 336 | * @param string $c |
||
| 337 | * @param Hash $hash |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private static function getRSAESOAEP(RSAKey $key, string $c, Hash $hash): string |
||
| 365 | } |
||
| 366 |