Complex classes like PHP_Crypt 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 PHP_Crypt, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class PHP_Crypt |
||
| 37 | { |
||
| 38 | // Ciphers |
||
| 39 | const CIPHER_3DES = "3DES"; |
||
| 40 | const CIPHER_3WAY = "3-Way"; |
||
| 41 | const CIPHER_AES_128 = "AES-128"; |
||
| 42 | const CIPHER_AES_192 = "AES-192"; |
||
| 43 | const CIPHER_AES_256 = "AES-256"; |
||
| 44 | const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
||
| 45 | const CIPHER_BLOWFISH = "Blowfish"; |
||
| 46 | const CIPHER_CAST_128 = "CAST-128"; |
||
| 47 | const CIPHER_CAST_256 = "CAST-256"; |
||
| 48 | const CIPHER_DES = "DES"; |
||
| 49 | const CIPHER_ENIGMA = "Enigma"; |
||
| 50 | const CIPHER_GOST = "GOST"; |
||
| 51 | const CIPHER_RC2 = "RC2"; |
||
| 52 | const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
||
| 53 | const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
||
| 54 | const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
||
| 55 | const CIPHER_SKIPJACK = "Skipjack"; |
||
| 56 | const CIPHER_SIMPLEXOR = "SimpleXOR"; |
||
| 57 | const CIPHER_VIGENERE = "Vigenere"; // historical |
||
| 58 | |||
| 59 | // Modes |
||
| 60 | const MODE_CBC = "CBC"; |
||
| 61 | const MODE_CFB = "CFB"; // 8 bit cfb mode |
||
| 62 | const MODE_CTR = "CTR"; |
||
| 63 | const MODE_ECB = "ECB"; |
||
| 64 | const MODE_NCFB = "NCFB"; // blocksize cfb mode |
||
| 65 | const MODE_NOFB = "NOFB"; // blocksize ofb mode |
||
| 66 | const MODE_OFB = "OFB"; // 8 bit ofb mode |
||
| 67 | const MODE_PCBC = "PCBC"; |
||
| 68 | const MODE_RAW = "Raw"; // raw encryption, with no mode |
||
| 69 | const MODE_STREAM = "Stream"; // used only for stream ciphers |
||
| 70 | |||
| 71 | // The source of random data used to create keys and IV's |
||
| 72 | // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
||
| 73 | const RAND = "rand"; // uses mt_rand(), windows & unix |
||
| 74 | const RAND_DEV_RAND = "/dev/random"; // unix only |
||
| 75 | const RAND_DEV_URAND = "/dev/urandom"; // unix only |
||
| 76 | const RAND_WIN_COM = "wincom"; // windows only, COM extension |
||
| 77 | const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
||
| 78 | |||
| 79 | // Padding types |
||
| 80 | const PAD_ZERO = 0; |
||
| 81 | const PAD_ANSI_X923 = 1; |
||
| 82 | const PAD_ISO_10126 = 2; |
||
| 83 | const PAD_PKCS7 = 3; |
||
| 84 | const PAD_ISO_7816_4 = 4; |
||
| 85 | |||
| 86 | |||
| 87 | /** @type object $cipher An instance of the cipher object selected */ |
||
| 88 | private $cipher = null; |
||
| 89 | |||
| 90 | /** @type object $mode An instance of the mode object selected */ |
||
| 91 | private $mode = null; |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Constructor |
||
| 96 | * |
||
| 97 | * @param string $key The key to use for the selected Cipher |
||
| 98 | * @param string $cipher The type of cipher to use |
||
| 99 | * @param string $mode The encrypt mode to use with the cipher |
||
| 100 | * @param integer $padding The padding type to use. Defaults to PAD_ZERO |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
||
| 104 | { |
||
| 105 | /* |
||
| 106 | * CIPHERS |
||
| 107 | */ |
||
| 108 | switch ($cipher) |
||
| 109 | { |
||
| 110 | case self::CIPHER_3DES: |
||
| 111 | $this->cipher = new Cipher_3DES($key); |
||
| 112 | break; |
||
| 113 | |||
| 114 | case self::CIPHER_3WAY: |
||
| 115 | $this->cipher = new Cipher_3WAY($key); |
||
| 116 | break; |
||
| 117 | |||
| 118 | case self::CIPHER_AES_128: |
||
| 119 | $this->cipher = new Cipher_AES_128($key); |
||
| 120 | break; |
||
| 121 | |||
| 122 | case self::CIPHER_AES_192: |
||
| 123 | $this->cipher = new Cipher_AES_192($key); |
||
| 124 | break; |
||
| 125 | |||
| 126 | case self::CIPHER_AES_256: |
||
| 127 | $this->cipher = new Cipher_AES_256($key); |
||
| 128 | break; |
||
| 129 | |||
| 130 | case self::CIPHER_ARC4: // an alternative to RC4 |
||
| 131 | $this->cipher = new Cipher_ARC4($key); |
||
| 132 | break; |
||
| 133 | |||
| 134 | case self::CIPHER_BLOWFISH: |
||
| 135 | $this->cipher = new Cipher_Blowfish($key); |
||
| 136 | break; |
||
| 137 | |||
| 138 | case self::CIPHER_CAST_128: |
||
| 139 | $this->cipher = new Cipher_CAST_128($key); |
||
| 140 | break; |
||
| 141 | |||
| 142 | case self::CIPHER_CAST_256: |
||
| 143 | $this->cipher = new Cipher_CAST_256($key); |
||
| 144 | break; |
||
| 145 | |||
| 146 | case self::CIPHER_DES: |
||
| 147 | $this->cipher = new Cipher_DES($key); |
||
| 148 | break; |
||
| 149 | |||
| 150 | case self::CIPHER_ENIGMA: |
||
| 151 | $this->cipher = new Cipher_Enigma($key); |
||
| 152 | break; |
||
| 153 | |||
| 154 | case self::CIPHER_GOST: |
||
| 155 | $this->cipher = new Cipher_GOST($key); |
||
| 156 | break; |
||
| 157 | |||
| 158 | case self::CIPHER_RC2: |
||
| 159 | $this->cipher = new Cipher_RC2($key); |
||
| 160 | break; |
||
| 161 | |||
| 162 | case self::CIPHER_RIJNDAEL_128: |
||
| 163 | $this->cipher = new Cipher_Rijndael_128($key); |
||
| 164 | break; |
||
| 165 | |||
| 166 | case self::CIPHER_RIJNDAEL_192: |
||
| 167 | $this->cipher = new Cipher_Rijndael_192($key); |
||
| 168 | break; |
||
| 169 | |||
| 170 | case self::CIPHER_RIJNDAEL_256: |
||
| 171 | $this->cipher = new Cipher_Rijndael_256($key); |
||
| 172 | break; |
||
| 173 | |||
| 174 | case self::CIPHER_SIMPLEXOR: |
||
| 175 | $this->cipher = new Cipher_Simple_XOR($key); |
||
| 176 | break; |
||
| 177 | |||
| 178 | case self::CIPHER_SKIPJACK: |
||
| 179 | $this->cipher = new Cipher_Skipjack($key); |
||
| 180 | break; |
||
| 181 | |||
| 182 | case self::CIPHER_VIGENERE: |
||
| 183 | $this->cipher = new Cipher_Vigenere($key); |
||
| 184 | break; |
||
| 185 | |||
| 186 | default: |
||
| 187 | trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | /* |
||
| 192 | * MODES |
||
| 193 | */ |
||
| 194 | switch ($mode) |
||
| 195 | { |
||
| 196 | case self::MODE_CBC: |
||
| 197 | $this->mode = new Mode_CBC($this->cipher); |
||
| 198 | break; |
||
| 199 | |||
| 200 | case self::MODE_CFB: |
||
| 201 | $this->mode = new Mode_CFB($this->cipher); |
||
| 202 | break; |
||
| 203 | |||
| 204 | case self::MODE_CTR: |
||
| 205 | $this->mode = new Mode_CTR($this->cipher); |
||
| 206 | break; |
||
| 207 | |||
| 208 | case self::MODE_ECB: |
||
| 209 | $this->mode = new Mode_ECB($this->cipher); |
||
| 210 | break; |
||
| 211 | |||
| 212 | case self::MODE_NCFB: |
||
| 213 | $this->mode = new Mode_NCFB($this->cipher); |
||
| 214 | break; |
||
| 215 | |||
| 216 | case self::MODE_NOFB: |
||
| 217 | $this->mode = new Mode_NOFB($this->cipher); |
||
| 218 | break; |
||
| 219 | |||
| 220 | case self::MODE_OFB: |
||
| 221 | $this->mode = new Mode_OFB($this->cipher); |
||
| 222 | break; |
||
| 223 | |||
| 224 | case self::MODE_PCBC: |
||
| 225 | $this->mode = new Mode_PCBC($this->cipher); |
||
| 226 | break; |
||
| 227 | |||
| 228 | case self::MODE_RAW: |
||
| 229 | $this->mode = new Mode_RAW($this->cipher); |
||
| 230 | break; |
||
| 231 | |||
| 232 | case self::MODE_STREAM: |
||
| 233 | $this->mode = new Mode_Stream($this->cipher); |
||
| 234 | break; |
||
| 235 | |||
| 236 | default: |
||
| 237 | trigger_error("$mode is not a valid mode", E_USER_WARNING); |
||
| 238 | } |
||
| 239 | |||
| 240 | // set the default padding |
||
| 241 | $this->padding($padding); |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Destructor |
||
| 247 | * |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | public function __destruct() |
||
| 251 | { |
||
| 252 | |||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Encrypt a plain text message using the Mode and Cipher selected. |
||
| 258 | * Some stream modes require this function to be called in a loop |
||
| 259 | * which requires the use of $result parameter to retrieve |
||
| 260 | * the decrypted data. |
||
| 261 | * |
||
| 262 | * @param string $text The plain text string |
||
| 263 | * @return string The encrypted string |
||
| 264 | */ |
||
| 265 | public function encrypt($text) |
||
| 266 | { |
||
| 267 | // check that an iv is set, if required by the mode |
||
| 268 | $this->mode->checkIV(); |
||
| 269 | |||
| 270 | // the encryption is done inside the mode |
||
| 271 | $this->mode->encrypt($text); |
||
| 272 | return $text; |
||
| 273 | } |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Decrypt an encrypted message using the Mode and Cipher selected. |
||
| 278 | * Some stream modes require this function to be called in a loop |
||
| 279 | * which requires the use of $result parameter to retrieve |
||
| 280 | * the decrypted data. |
||
| 281 | * |
||
| 282 | * @param string $text The encrypted string |
||
| 283 | * @return string The decrypted string |
||
| 284 | */ |
||
| 285 | public function decrypt($text) |
||
| 286 | { |
||
| 287 | // check that an iv is set, if required by the mode |
||
| 288 | $this->mode->checkIV(); |
||
| 289 | |||
| 290 | // the decryption is done inside the mode |
||
| 291 | $this->mode->decrypt($text); |
||
| 292 | return $text; |
||
| 293 | } |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Return the cipher object being used |
||
| 298 | * |
||
| 299 | * @return object The Cipher object |
||
| 300 | */ |
||
| 301 | public function cipher() |
||
| 302 | { |
||
| 303 | return $this->cipher; |
||
| 304 | } |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Return the mode object being used |
||
| 309 | * |
||
| 310 | * @return object The Mode object |
||
| 311 | */ |
||
| 312 | public function mode() |
||
| 313 | { |
||
| 314 | return $this->mode; |
||
| 315 | } |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns the name of the cipher being used |
||
| 320 | * |
||
| 321 | * @return string The name of the cipher currently in use, |
||
| 322 | * it will be one of the predefined phpCrypt cipher constants |
||
| 323 | */ |
||
| 324 | public function cipherName() |
||
| 325 | { |
||
| 326 | return $this->cipher->name(); |
||
| 327 | } |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Return the name of the mode being used |
||
| 332 | * |
||
| 333 | * @return string The name of the mode in use, it will |
||
| 334 | * be one of the predefined phpCrypt mode constants |
||
| 335 | */ |
||
| 336 | public function modeName() |
||
| 337 | { |
||
| 338 | return $this->mode->name(); |
||
| 339 | } |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns Ciphers required block size in bytes |
||
| 344 | * |
||
| 345 | * @return integer The cipher data block size, in bytes |
||
| 346 | */ |
||
| 347 | public function cipherBlockSize() |
||
| 348 | { |
||
| 349 | return $this->cipher->blockSize(); |
||
| 350 | } |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns the cipher's required key size, in bytes |
||
| 355 | * |
||
| 356 | * @return integer The cipher's key size requirement, in bytes |
||
| 357 | */ |
||
| 358 | public function cipherKeySize() |
||
| 359 | { |
||
| 360 | return $this->cipher->keySize(); |
||
| 361 | } |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Sets and/or returns the key to be used. Normally you set |
||
| 366 | * the key in the phpCrypt constructor. This can be usefully |
||
| 367 | * if you need to change the key on the fly and don't want |
||
| 368 | * to create a new instance of phpCrypt. |
||
| 369 | * |
||
| 370 | * If the $key parameter is not given, this function will simply |
||
| 371 | * return the key currently in use. |
||
| 372 | * |
||
| 373 | * @param string $key Optional, The key to set |
||
| 374 | * @return string The key being used |
||
| 375 | */ |
||
| 376 | public function cipherKey($key = "") |
||
| 377 | { |
||
| 378 | return $this->cipher->key($key); |
||
| 379 | } |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * A helper function which will create a random key. Calls |
||
| 384 | * Core::randBytes(). By default it will use PHP_Crypt::RAND for |
||
| 385 | * the random source of bytes, and return a PHP_Crypt::RAND_DEFAULT_SZ |
||
| 386 | * byte string. There are 4 ways to create a random byte string by |
||
| 387 | * setting the $src parameter: |
||
| 388 | * PHP_Crypt::RAND - Default, uses mt_rand() |
||
| 389 | * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
||
| 390 | * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
||
| 391 | * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
||
| 392 | * |
||
| 393 | * @param string $src Optional, The source to use to create random bytes |
||
| 394 | * @param integer $len Optional, The number of random bytes to return |
||
| 395 | * @return string A random string of bytes |
||
| 396 | */ |
||
| 397 | public static function createKey($src = self::RAND, $len = self::RAND_DEFAULT_SZ) |
||
| 398 | { |
||
| 399 | return Core::randBytes($src, $len); |
||
| 400 | } |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Sets the IV to use. Note that you do not need to call |
||
| 405 | * this function if creating an IV using createIV(). This |
||
| 406 | * function is used when an IV has already been created |
||
| 407 | * outside of phpCrypt and needs to be set. Alternatively |
||
| 408 | * you can just pass the $iv parameter to the encrypt() |
||
| 409 | * or decrypt() functions |
||
| 410 | * |
||
| 411 | * When the $iv parameter is not given, the function will |
||
| 412 | * return the current IV being used. See createIV() if you |
||
| 413 | * need to create an IV. |
||
| 414 | * |
||
| 415 | * @param string $iv Optional, The IV to use during Encryption/Decryption |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | public function IV($iv = "") |
||
| 419 | { |
||
| 420 | return $this->mode->IV($iv); |
||
| 421 | } |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * Creates an IV for the the Cipher selected, if one is required. |
||
| 426 | * If you already have an IV to use, this function does not need |
||
| 427 | * to be called, instead set it with setIV(). If you create an |
||
| 428 | * IV with createIV(), you do not need to set it with setIV(), |
||
| 429 | * as it is automatically set in this function |
||
| 430 | * |
||
| 431 | * $src values are: |
||
| 432 | * PHP_Crypt::RAND - Default, uses mt_rand() |
||
| 433 | * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
||
| 434 | * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
||
| 435 | * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
||
| 436 | * |
||
| 437 | * @param string $src Optional, how the IV is generated |
||
| 438 | * @return string The IV that was created, and set for the mode |
||
| 439 | */ |
||
| 440 | public function createIV($src = self::RAND) |
||
| 441 | { |
||
| 442 | return $this->mode->createIV($src); |
||
| 443 | } |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * Sets the type of padding to be used within the specified Mode |
||
| 448 | * |
||
| 449 | * @param string $type One of the predefined padding types |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | public function padding($type = "") |
||
| 453 | { |
||
| 454 | return $this->mode->padding($type); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | ?> |
||
| 458 |