Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
8 | View Code Duplication | class AesCtr extends Aes |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * Encrypt a text using AES encryption in Counter mode of operation |
||
12 | * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
||
13 | * |
||
14 | * Unicode multi-byte character safe |
||
15 | * |
||
16 | * @param plaintext source text to be encrypted |
||
17 | * @param password the password to use to generate a key |
||
18 | * @param nBits number of bits to be used in the key (128, 192, or 256) |
||
19 | * @return string text |
||
20 | */ |
||
21 | public static function encrypt($plaintext, $password, $nBits) |
||
95 | |||
96 | /** |
||
97 | * Decrypt a text encrypted by AES in counter mode of operation |
||
98 | * |
||
99 | * @param ciphertext source text to be decrypted |
||
100 | * @param password the password to use to generate a key |
||
101 | * @param nBits number of bits to be used in the key (128, 192, or 256) |
||
102 | * @return string text |
||
103 | */ |
||
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 | } |
||
169 | |||
170 | /* |
||
171 | * Unsigned right shift function, since PHP has neither >>> operator nor unsigned ints |
||
172 | * |
||
173 | * @param a number to be shifted (32-bit integer) |
||
174 | * @param b number of bits to shift a to the right (0..31) |
||
175 | * @return a right-shifted and zero-filled by b bits |
||
176 | */ |
||
177 | private static function urs($a, $b) |
||
189 | |||
190 | } |
||
191 | /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ |
||
192 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.