| Conditions | 13 |
| Paths | 273 |
| Total Lines | 73 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 21 | public static function encrypt($plaintext, $password, $nBits) |
||
| 22 | { |
||
| 23 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
||
| 24 | if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
||
| 25 | return ''; |
||
| 26 | } |
||
| 27 | // standard allows 128/192/256 bit keys |
||
| 28 | // note PHP (5) gives us plaintext and password in UTF8 encoding! |
||
| 29 | |||
| 30 | // use AES itself to encrypt password to get cipher key (using plain password as source for |
||
| 31 | // key expansion) - gives us well encrypted key |
||
| 32 | $nBytes = $nBits / 8; // no bytes in key |
||
| 33 | $pwBytes = array(); |
||
| 34 | for ($i = 0; $i < $nBytes; $i++) { |
||
| 35 | $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
||
| 36 | } |
||
| 37 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
||
| 38 | $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
||
| 39 | |||
| 40 | // initialise counter block (NIST SP800-38A §B.2): millisecond time-stamp for nonce in |
||
| 41 | // 1st 8 bytes, block counter in 2nd 8 bytes |
||
| 42 | $counterBlock = array(); |
||
| 43 | $nonce = floor(microtime(true) * 1000); // timestamp: milliseconds since 1-Jan-1970 |
||
| 44 | $nonceSec = floor($nonce / 1000); |
||
| 45 | $nonceMs = $nonce % 1000; |
||
| 46 | // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes |
||
| 47 | for ($i = 0; $i < 4; $i++) { |
||
| 48 | $counterBlock[$i] = self::urs($nonceSec, $i * 8) & 0xff; |
||
| 49 | } |
||
| 50 | for ($i = 0; $i < 4; $i++) { |
||
| 51 | $counterBlock[$i + 4] = $nonceMs & 0xff; |
||
| 52 | } |
||
| 53 | // and convert it to a string to go on the front of the ciphertext |
||
| 54 | $ctrTxt = ''; |
||
| 55 | for ($i = 0; $i < 8; $i++) { |
||
| 56 | $ctrTxt .= chr($counterBlock[$i]); |
||
| 57 | } |
||
| 58 | |||
| 59 | // generate key schedule - an expansion of the key into distinct Key Rounds for each round |
||
| 60 | $keySchedule = Aes::keyExpansion($key); |
||
| 61 | //print_r($keySchedule); |
||
| 62 | |||
| 63 | $blockCount = ceil(strlen($plaintext) / $blockSize); |
||
| 64 | $ciphertxt = array(); // ciphertext as array of strings |
||
| 65 | |||
| 66 | for ($b = 0; $b < $blockCount; $b++) { |
||
| 67 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
||
| 68 | // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
||
| 69 | for ($c = 0; $c < 4; $c++) { |
||
| 70 | $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
||
| 71 | } |
||
| 72 | for ($c = 0; $c < 4; $c++) { |
||
| 73 | $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); |
||
| 74 | } |
||
| 75 | |||
| 76 | $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
||
| 77 | |||
| 78 | // block size is reduced on final block |
||
| 79 | $blockLength = $b < $blockCount - 1 ? $blockSize : (strlen($plaintext) - 1) % $blockSize + 1; |
||
| 80 | $cipherByte = array(); |
||
| 81 | |||
| 82 | for ($i = 0; $i < $blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
||
| 83 | $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b * $blockSize + $i, 1)); |
||
| 84 | $cipherByte[$i] = chr($cipherByte[$i]); |
||
| 85 | } |
||
| 86 | $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
||
| 87 | } |
||
| 88 | |||
| 89 | // implode is more efficient than repeated string concatenation |
||
| 90 | $ciphertext = $ctrTxt.implode('', $ciphertxt); |
||
| 91 | $ciphertext = base64_encode($ciphertext); |
||
| 92 | |||
| 93 | return $ciphertext; |
||
| 94 | } |
||
| 192 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths