| Conditions | 11 |
| Paths | 73 |
| Total Lines | 64 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 20 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 104 | public static function decrypt($ciphertext, $password, $nBits) |
||
| 105 | { |
||
| 106 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
||
| 107 | if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
||
| 108 | return ''; |
||
| 109 | } |
||
| 110 | // standard allows 128/192/256 bit keys |
||
| 111 | $ciphertext = base64_decode($ciphertext); |
||
| 112 | |||
| 113 | // use AES to encrypt password (mirroring encrypt routine) |
||
| 114 | $nBytes = $nBits / 8; // no bytes in key |
||
| 115 | $pwBytes = array(); |
||
| 116 | for ($i = 0; $i < $nBytes; $i++) { |
||
| 117 | $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
||
| 118 | } |
||
| 119 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
||
| 120 | $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
||
| 121 | |||
| 122 | // recover nonce from 1st element of ciphertext |
||
| 123 | $counterBlock = array(); |
||
| 124 | $ctrTxt = substr($ciphertext, 0, 8); |
||
| 125 | for ($i = 0; $i < 8; $i++) { |
||
| 126 | $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); |
||
| 127 | } |
||
| 128 | |||
| 129 | // generate key schedule |
||
| 130 | $keySchedule = Aes::keyExpansion($key); |
||
| 131 | |||
| 132 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
||
| 133 | $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); |
||
| 134 | $ct = array(); |
||
| 135 | for ($b = 0; $b < $nBlocks; $b++) { |
||
| 136 | $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); |
||
| 137 | } |
||
| 138 | $ciphertext = $ct; // ciphertext is now array of block-length strings |
||
| 139 | |||
| 140 | // plaintext will get generated block-by-block into array of block-length strings |
||
| 141 | $plaintxt = array(); |
||
| 142 | |||
| 143 | for ($b = 0; $b < $nBlocks; $b++) { |
||
| 144 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
||
| 145 | for ($c = 0; $c < 4; $c++) { |
||
| 146 | $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
||
| 147 | } |
||
| 148 | for ($c = 0; $c < 4; $c++) { |
||
| 149 | $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; |
||
| 150 | } |
||
| 151 | |||
| 152 | $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
||
| 153 | |||
| 154 | $plaintxtByte = array(); |
||
| 155 | for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { |
||
| 156 | // -- xor plaintext with ciphered counter byte-by-byte -- |
||
| 157 | $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); |
||
| 158 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
||
| 159 | |||
| 160 | } |
||
| 161 | $plaintxt[$b] = implode('', $plaintxtByte); |
||
| 162 | } |
||
| 163 | |||
| 164 | // join array of blocks into single plaintext string |
||
| 165 | $plaintext = implode('', $plaintxt); |
||
| 166 | |||
| 167 | return $plaintext; |
||
| 168 | } |
||
| 192 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.