@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | * @param integer $Nb |
48 | 48 | */ |
49 | 49 | private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
50 | - for ($r=0; $r<4; $r++) { |
|
51 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
50 | + for ($r = 0; $r < 4; $r++) { |
|
51 | + for ($c = 0; $c < $Nb; $c++) $state[$r][$c] ^= $w[$rnd * 4 + $c][$r]; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | return $state; |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | * @param integer $Nb |
59 | 59 | */ |
60 | 60 | private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
61 | - for ($r=0; $r<4; $r++) { |
|
62 | - for ($c=0; $c<$Nb; $c++) { |
|
61 | + for ($r = 0; $r < 4; $r++) { |
|
62 | + for ($c = 0; $c < $Nb; $c++) { |
|
63 | 63 | $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
64 | 64 | } |
65 | 65 | } |
@@ -72,29 +72,29 @@ discard block |
||
72 | 72 | */ |
73 | 73 | private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
74 | 74 | $t = array(4); |
75 | - for ($r=1; $r<4; $r++) { |
|
76 | - for ($c=0; $c<4; $c++) { |
|
77 | - $t[$c] = $s[$r][($c+$r)%$Nb]; |
|
75 | + for ($r = 1; $r < 4; $r++) { |
|
76 | + for ($c = 0; $c < 4; $c++) { |
|
77 | + $t[$c] = $s[$r][($c + $r) % $Nb]; |
|
78 | 78 | } |
79 | 79 | // shift into temp copy |
80 | - for ($c=0; $c<4; $c++) { |
|
80 | + for ($c = 0; $c < 4; $c++) { |
|
81 | 81 | $s[$r][$c] = $t[$c]; |
82 | 82 | } |
83 | 83 | // and copy back |
84 | 84 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
85 | - return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
85 | + return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
89 | 89 | * @param integer $Nb |
90 | 90 | */ |
91 | 91 | private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
92 | - for ($c=0; $c<4; $c++) { |
|
93 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | - for ($i=0; $i<4; $i++) { |
|
92 | + for ($c = 0; $c < 4; $c++) { |
|
93 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | + for ($i = 0; $i < 4; $i++) { |
|
96 | 96 | $a[$i] = $s[$i][$c]; |
97 | - $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
|
97 | + $b[$i] = $s[$i][$c] & 0x80 ? $s[$i][$c] << 1 ^ 0x011b : $s[$i][$c] << 1; |
|
98 | 98 | } |
99 | 99 | // a[n] ^ b[n] is aé{03} in GF(2^8) |
100 | 100 | $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
@@ -114,33 +114,33 @@ discard block |
||
114 | 114 | * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
115 | 115 | */ |
116 | 116 | public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
117 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
118 | - $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
119 | - $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
117 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
118 | + $Nk = count($key) / 4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
119 | + $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
120 | 120 | |
121 | 121 | $w = array(); |
122 | 122 | $temp = array(); |
123 | 123 | |
124 | - for ($i=0; $i<$Nk; $i++) { |
|
125 | - $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
124 | + for ($i = 0; $i < $Nk; $i++) { |
|
125 | + $r = array($key[4 * $i], $key[4 * $i + 1], $key[4 * $i + 2], $key[4 * $i + 3]); |
|
126 | 126 | $w[$i] = $r; |
127 | 127 | } |
128 | 128 | |
129 | - for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
|
129 | + for ($i = $Nk; $i < ($Nb * ($Nr + 1)); $i++) { |
|
130 | 130 | $w[$i] = array(); |
131 | - for ($t=0; $t<4; $t++) { |
|
132 | - $temp[$t] = $w[$i-1][$t]; |
|
131 | + for ($t = 0; $t < 4; $t++) { |
|
132 | + $temp[$t] = $w[$i - 1][$t]; |
|
133 | 133 | } |
134 | 134 | if ($i % $Nk == 0) { |
135 | 135 | $temp = self::subWord(self::rotWord($temp)); |
136 | - for ($t=0; $t<4; $t++) { |
|
137 | - $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
136 | + for ($t = 0; $t < 4; $t++) { |
|
137 | + $temp[$t] ^= self::$rCon[$i / $Nk][$t]; |
|
138 | 138 | } |
139 | - } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
139 | + } elseif ($Nk > 6 && $i % $Nk == 4) { |
|
140 | 140 | $temp = self::subWord($temp); |
141 | 141 | } |
142 | - for ($t=0; $t<4; $t++) { |
|
143 | - $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
142 | + for ($t = 0; $t < 4; $t++) { |
|
143 | + $w[$i][$t] = $w[$i - $Nk][$t] ^ $temp[$t]; |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | } |
149 | 149 | |
150 | 150 | private static function subWord($w) { // apply SBox to 4-byte word w |
151 | - for ($i=0; $i<4; $i++) { |
|
151 | + for ($i = 0; $i < 4; $i++) { |
|
152 | 152 | $w[$i] = self::$sBox[$w[$i]]; |
153 | 153 | } |
154 | 154 | |
@@ -157,8 +157,8 @@ discard block |
||
157 | 157 | |
158 | 158 | private static function rotWord($w) { // rotate 4-byte word w left by one byte |
159 | 159 | $tmp = $w[0]; |
160 | - for ($i=0; $i<3; $i++) { |
|
161 | - $w[$i] = $w[$i+1]; |
|
160 | + for ($i = 0; $i < 3; $i++) { |
|
161 | + $w[$i] = $w[$i + 1]; |
|
162 | 162 | } |
163 | 163 | $w[3] = $tmp; |
164 | 164 |
@@ -20,8 +20,8 @@ discard block |
||
20 | 20 | */ |
21 | 21 | public static function encrypt($plaintext, $password, $nBits) |
22 | 22 | { |
23 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
24 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
23 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
24 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
25 | 25 | return ''; |
26 | 26 | } |
27 | 27 | // standard allows 128/192/256 bit keys |
@@ -99,58 +99,58 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public static function decrypt($ciphertext, $password, $nBits) |
101 | 101 | { |
102 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
103 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
102 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
103 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
104 | 104 | return ''; |
105 | 105 | } |
106 | 106 | // standard allows 128/192/256 bit keys |
107 | 107 | $ciphertext = base64_decode($ciphertext); |
108 | 108 | |
109 | 109 | // use AES to encrypt password (mirroring encrypt routine) |
110 | - $nBytes = $nBits/8; // no bytes in key |
|
110 | + $nBytes = $nBits / 8; // no bytes in key |
|
111 | 111 | $pwBytes = array(); |
112 | - for ($i=0; $i<$nBytes; $i++) { |
|
113 | - $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
112 | + for ($i = 0; $i < $nBytes; $i++) { |
|
113 | + $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
|
114 | 114 | } |
115 | 115 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
116 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
116 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
117 | 117 | |
118 | 118 | // recover nonce from 1st element of ciphertext |
119 | 119 | $counterBlock = array(); |
120 | 120 | $ctrTxt = substr($ciphertext, 0, 8); |
121 | - for ($i=0; $i<8; $i++) { |
|
122 | - $counterBlock[$i] = ord(substr($ctrTxt,$i,1)); |
|
121 | + for ($i = 0; $i < 8; $i++) { |
|
122 | + $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | // generate key schedule |
126 | 126 | $keySchedule = Aes::keyExpansion($key); |
127 | 127 | |
128 | 128 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
129 | - $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); |
|
129 | + $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); |
|
130 | 130 | $ct = array(); |
131 | - for ($b=0; $b<$nBlocks; $b++) { |
|
132 | - $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
131 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
132 | + $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); |
|
133 | 133 | } |
134 | - $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
134 | + $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
135 | 135 | |
136 | 136 | // plaintext will get generated block-by-block into array of block-length strings |
137 | 137 | $plaintxt = array(); |
138 | 138 | |
139 | - for ($b=0; $b<$nBlocks; $b++) { |
|
139 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
140 | 140 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
141 | - for ($c=0; $c<4; $c++) { |
|
142 | - $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
141 | + for ($c = 0; $c < 4; $c++) { |
|
142 | + $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
143 | 143 | } |
144 | - for ($c=0; $c<4; $c++) { |
|
145 | - $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
144 | + for ($c = 0; $c < 4; $c++) { |
|
145 | + $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; |
|
146 | 146 | } |
147 | 147 | |
148 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
148 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
149 | 149 | |
150 | 150 | $plaintxtByte = array(); |
151 | - for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
151 | + for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { |
|
152 | 152 | // -- xor plaintext with ciphered counter byte-by-byte -- |
153 | - $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b],$i,1)); |
|
153 | + $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); |
|
154 | 154 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
155 | 155 | |
156 | 156 | } |
@@ -321,14 +321,14 @@ |
||
321 | 321 | /** |
322 | 322 | * @param string $which |
323 | 323 | */ |
324 | - public function insertOrReplace($which, $table, $datas, $options=array()) { |
|
324 | + public function insertOrReplace($which, $table, $datas, $options = array()) { |
|
325 | 325 | $datas = unserialize(serialize($datas)); // break references within array |
326 | 326 | $keys = $values = array(); |
327 | 327 | |
328 | 328 | if (isset($datas[0]) && is_array($datas[0])) { |
329 | 329 | foreach ($datas as $datum) { |
330 | 330 | ksort($datum); |
331 | - if (! $keys) { |
|
331 | + if (!$keys) { |
|
332 | 332 | $keys = array_keys($datum); |
333 | 333 | } |
334 | 334 | $values[] = array_values($datum); |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | var $maxStrLenRead; |
71 | 71 | |
72 | 72 | function __construct() { |
73 | - $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
73 | + $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | */ |
80 | 80 | function getMetrics($file) { |
81 | 81 | $this->filename = $file; |
82 | - $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
|
82 | + $this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); |
|
83 | 83 | $this->_pos = 0; |
84 | 84 | $this->charWidths = ''; |
85 | 85 | $this->glyphPos = array(); |
@@ -1037,7 +1037,7 @@ discard block |
||
1037 | 1037 | /** |
1038 | 1038 | * @param integer $unicode_cmap_offset |
1039 | 1039 | */ |
1040 | - function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
|
1040 | + function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph) { |
|
1041 | 1041 | $this->maxUniChar = 0; |
1042 | 1042 | $this->seek($unicode_cmap_offset + 2); |
1043 | 1043 | $length = $this->read_ushort(); |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | for ($i = 0; $i < $max; ++$i) |
164 | 164 | { |
165 | 165 | $pos = $i * 2; |
166 | - $text[$pos] = chr($w[$i]); |
|
166 | + $text[$pos] = chr($w[$i]); |
|
167 | 167 | $text[$pos + 1] = chr($w[$i] >> 8); |
168 | 168 | } |
169 | 169 | |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | for ($i = 0; $i < $max; ++$i) |
238 | 238 | { |
239 | 239 | $pos = $i * 2; |
240 | - $text[$pos] = chr($w[$i]); |
|
240 | + $text[$pos] = chr($w[$i]); |
|
241 | 241 | $text[$pos + 1] = chr($w[$i] >> 8); |
242 | 242 | } |
243 | 243 |
@@ -71,7 +71,7 @@ |
||
71 | 71 | // The source of random data used to create keys and IV's |
72 | 72 | // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
73 | 73 | const RAND = "rand"; // uses mt_rand(), windows & unix |
74 | - const RAND_DEV_RAND = "/dev/random"; // unix only |
|
74 | + const RAND_DEV_RAND = "/dev/random"; // unix only |
|
75 | 75 | const RAND_DEV_URAND = "/dev/urandom"; // unix only |
76 | 76 | const RAND_WIN_COM = "wincom"; // windows only, COM extension |
77 | 77 | const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |