@@ -173,6 +173,7 @@ discard block |
||
173 | 173 | * |
174 | 174 | * @param string $text The string to pad |
175 | 175 | * @param integer The number of bytes to pad |
176 | + * @param integer $bytes |
|
176 | 177 | * @return boolean Returns true |
177 | 178 | */ |
178 | 179 | private static function ansiX923Pad(&$text, $bytes) |
@@ -217,6 +218,7 @@ discard block |
||
217 | 218 | * |
218 | 219 | * @param string $text The string to pad |
219 | 220 | * @param integer The number of bytes to pad |
221 | + * @param integer $bytes |
|
220 | 222 | * @return boolean Returns true |
221 | 223 | */ |
222 | 224 | private static function iso10126Pad(&$text, $bytes) |
@@ -261,6 +263,7 @@ discard block |
||
261 | 263 | * |
262 | 264 | * @param string $text The string to pad |
263 | 265 | * @param integer The number of bytes to pad |
266 | + * @param integer $bytes |
|
264 | 267 | * @return boolean Returns true |
265 | 268 | */ |
266 | 269 | private static function pkcs7Pad(&$text, $bytes) |
@@ -296,6 +299,7 @@ discard block |
||
296 | 299 | * |
297 | 300 | * @param string $text The string to pad |
298 | 301 | * @param integer The number of bytes to pad |
302 | + * @param integer $bytes |
|
299 | 303 | * @return boolean Returns true |
300 | 304 | */ |
301 | 305 | private static function iso7816Pad(&$text, $bytes) |
@@ -39,290 +39,290 @@ |
||
39 | 39 | */ |
40 | 40 | class Padding |
41 | 41 | { |
42 | - /** |
|
43 | - * Constructor |
|
44 | - * |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - public function __construct() { } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Destructor |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function __destruct() { } |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * Returns a string padded using the specified padding scheme |
|
60 | - * |
|
61 | - * @param string $text The string to pad |
|
62 | - * @param integer $bytes The number of bytes to pad |
|
63 | - * @param integer $type One of the predefined padding types |
|
64 | - * @return boolean True on success, false on error |
|
65 | - */ |
|
66 | - public static function pad(&$text, $bytes, $type = PHP_Crypt::ZERO) |
|
67 | - { |
|
68 | - // if the size of padding is not greater than 1 |
|
69 | - // just return true, no padding will be done |
|
70 | - if(!($bytes > 0)) |
|
71 | - return true; |
|
72 | - |
|
73 | - switch($type) |
|
74 | - { |
|
75 | - case PHP_Crypt::PAD_ZERO: |
|
76 | - return self::zeroPad($text, $bytes); |
|
77 | - break; |
|
78 | - case PHP_Crypt::PAD_ANSI_X923: |
|
79 | - return self::ansiX923Pad($text, $bytes); |
|
80 | - break; |
|
81 | - case PHP_Crypt::PAD_ISO_10126: |
|
82 | - return self::iso10126Pad($text, $bytes); |
|
83 | - break; |
|
84 | - case PHP_Crypt::PAD_PKCS7: |
|
85 | - return self::pkcs7Pad($text, $bytes); |
|
86 | - break; |
|
87 | - case PHP_Crypt::PAD_ISO_7816_4: |
|
88 | - return self::iso7816Pad($text, $bytes); |
|
89 | - break; |
|
90 | - default: |
|
91 | - trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
92 | - return self::zeroPad($text, $bytes); |
|
93 | - } |
|
94 | - |
|
95 | - return true; |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Strips padding from a string |
|
101 | - * |
|
102 | - * @param string $text The string strip padding from |
|
103 | - * @param integer $type One of the predefined padding types |
|
104 | - * @return boolean True on success, false on error |
|
105 | - */ |
|
106 | - public static function strip(&$text, $type = PHP_Crypt::ZERO) |
|
107 | - { |
|
108 | - switch($type) |
|
109 | - { |
|
110 | - case PHP_Crypt::PAD_ZERO: |
|
111 | - return self::zeroStrip($text); |
|
112 | - break; |
|
113 | - case PHP_Crypt::PAD_ANSI_X923: |
|
114 | - return self::ansiX923Strip($text); |
|
115 | - break; |
|
116 | - case PHP_Crypt::PAD_ISO_10126: |
|
117 | - return self::iso10126Strip($text); |
|
118 | - break; |
|
119 | - case PHP_Crypt::PAD_PKCS7: |
|
120 | - return self::pkcs7Strip($text); |
|
121 | - break; |
|
122 | - case PHP_Crypt::PAD_ISO_7816_4: |
|
123 | - return self::iso7816Strip($text); |
|
124 | - break; |
|
125 | - default: |
|
126 | - trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
127 | - return self::zeroStrip($text); |
|
128 | - } |
|
129 | - |
|
130 | - return true; |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Pads a string with null bytes |
|
136 | - * |
|
137 | - * @param string $text The string to be padded |
|
138 | - * @param integer $bytes The number of bytes to pad |
|
139 | - * @return boolean Returns true |
|
140 | - */ |
|
141 | - private static function zeroPad(&$text, $bytes) |
|
142 | - { |
|
143 | - $len = $bytes + strlen($text); |
|
144 | - $text = str_pad($text, $len, chr(0), STR_PAD_RIGHT); |
|
145 | - return true; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * Strips null padding off a string |
|
151 | - * NOTE: This is generally a bad idea as there is no way |
|
152 | - * to distinguish a null byte that is not padding from |
|
153 | - * a null byte that is padding. Stripping null padding should |
|
154 | - * be handled by the developer at the application level. |
|
155 | - * |
|
156 | - * @param string $text The string with null padding to strip |
|
157 | - * @return boolean Returns true |
|
158 | - */ |
|
159 | - private static function zeroStrip(&$text) |
|
160 | - { |
|
161 | - // with NULL byte padding, we should not strip off the |
|
162 | - // null bytes, instead leave this to the developer at the |
|
163 | - // application level |
|
164 | - //$text = preg_replace('/\0+$/', '', $text); |
|
165 | - return true; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * Pads a string using ANSI X.923 |
|
171 | - * Adds null padding to the string, except for the last byte |
|
172 | - * which indicates the number of bytes padded |
|
173 | - * |
|
174 | - * @param string $text The string to pad |
|
175 | - * @param integer The number of bytes to pad |
|
176 | - * @return boolean Returns true |
|
177 | - */ |
|
178 | - private static function ansiX923Pad(&$text, $bytes) |
|
179 | - { |
|
180 | - $len = $bytes + strlen($text); |
|
181 | - $text = str_pad($text, ($len-1), "\0", STR_PAD_RIGHT); |
|
182 | - $text .= chr($bytes); |
|
183 | - return true; |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * Strips ANSI X.923 padding from a string |
|
189 | - * |
|
190 | - * @param string $text The string to strip padding from |
|
191 | - * @return boolean Returns true |
|
192 | - */ |
|
193 | - private static function ansiX923Strip(&$text) |
|
194 | - { |
|
195 | - $pos = strlen($text) - 1; |
|
196 | - $c = ord($text[$pos]); |
|
197 | - |
|
198 | - if($c == 0) |
|
199 | - return true; |
|
200 | - else if($c == 1) |
|
201 | - $text = substr($text, 0, -1); |
|
202 | - else |
|
203 | - { |
|
204 | - // the total null bytes are 1 less than the value of the final byte |
|
205 | - $nc = $c - 1; |
|
206 | - $text = preg_replace('/\0{'.$nc.'}'.preg_quote(chr($c)).'$/', "", $text); |
|
207 | - } |
|
208 | - |
|
209 | - return true; |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * Pads a string using ISO 10126 |
|
215 | - * Adds random bytes to the end of the string, except for the last |
|
216 | - * byte which indicates the number of padded bytes |
|
217 | - * |
|
218 | - * @param string $text The string to pad |
|
219 | - * @param integer The number of bytes to pad |
|
220 | - * @return boolean Returns true |
|
221 | - */ |
|
222 | - private static function iso10126Pad(&$text, $bytes) |
|
223 | - { |
|
224 | - // create the random pad bytes, we do one less than |
|
225 | - // needed because the last byte is reserved for the |
|
226 | - // number of padded bytes |
|
227 | - for($i = 0; $i < ($bytes - 1); ++$i) |
|
228 | - $text .= chr(mt_rand(0, 255)); |
|
229 | - |
|
230 | - // add the byte to indicate the padding length |
|
231 | - $text .= chr($bytes); |
|
232 | - return true; |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - /** |
|
237 | - * Strips ISO 10126 padding from a string |
|
238 | - * |
|
239 | - * @param string $text The string to strip padding from |
|
240 | - * @return boolean Returns true |
|
241 | - */ |
|
242 | - private static function iso10126Strip(&$text) |
|
243 | - { |
|
244 | - $pos = strlen($text) - 1; |
|
245 | - $c = ord($text[$pos]) * -1; |
|
246 | - |
|
247 | - // if we got a null byte at the end of the string, |
|
248 | - // just return |
|
249 | - if($c == 0) |
|
250 | - return true; |
|
251 | - |
|
252 | - $text = substr($text, 0, $c); |
|
253 | - return true; |
|
254 | - } |
|
255 | - |
|
256 | - |
|
257 | - /** |
|
258 | - * Pads a string using PKCS7 |
|
259 | - * Adds padding using the bytes with the value of the |
|
260 | - * number of bytes need for padding |
|
261 | - * |
|
262 | - * @param string $text The string to pad |
|
263 | - * @param integer The number of bytes to pad |
|
264 | - * @return boolean Returns true |
|
265 | - */ |
|
266 | - private static function pkcs7Pad(&$text, $bytes) |
|
267 | - { |
|
268 | - $len = $bytes + strlen($text); |
|
269 | - $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
270 | - return true; |
|
271 | - } |
|
272 | - |
|
273 | - |
|
274 | - /** |
|
275 | - * Strips PKCS7 padding from a string |
|
276 | - * |
|
277 | - * @param string $text The string to strip padding from |
|
278 | - * @return boolean Returns true |
|
279 | - */ |
|
280 | - private static function pkcs7Strip(&$text) |
|
281 | - { |
|
282 | - $pos = strlen($text) - 1; |
|
283 | - $c = ord($text[$pos]); |
|
284 | - |
|
285 | - if($c == 0) |
|
286 | - return true; |
|
287 | - |
|
288 | - $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
|
289 | - return true; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * Pads a string using ISO/IEC 7816-4 |
|
295 | - * Adds byte 0x80 followed by null bytes to pad a string |
|
296 | - * |
|
297 | - * @param string $text The string to pad |
|
298 | - * @param integer The number of bytes to pad |
|
299 | - * @return boolean Returns true |
|
300 | - */ |
|
301 | - private static function iso7816Pad(&$text, $bytes) |
|
302 | - { |
|
303 | - $text .= chr(0x80); |
|
304 | - $len = $bytes + strlen($text); |
|
305 | - |
|
306 | - // if we are only padding one byte, then 0x80 is all we need |
|
307 | - // else we follow up with null bytes |
|
308 | - if($bytes > 1) |
|
309 | - $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
310 | - |
|
311 | - return true; |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * Strips ISO/IEC 7816-4 padding from a string |
|
317 | - * |
|
318 | - * @param string $text The string to strip padding from |
|
319 | - * @return boolean Returns true |
|
320 | - */ |
|
321 | - private static function iso7816Strip(&$text) |
|
322 | - { |
|
323 | - $c = chr(0x80); |
|
324 | - $text = preg_replace('/'.preg_quote($c).'\0*$/', '', $text); |
|
325 | - return true; |
|
326 | - } |
|
42 | + /** |
|
43 | + * Constructor |
|
44 | + * |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + public function __construct() { } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Destructor |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function __destruct() { } |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * Returns a string padded using the specified padding scheme |
|
60 | + * |
|
61 | + * @param string $text The string to pad |
|
62 | + * @param integer $bytes The number of bytes to pad |
|
63 | + * @param integer $type One of the predefined padding types |
|
64 | + * @return boolean True on success, false on error |
|
65 | + */ |
|
66 | + public static function pad(&$text, $bytes, $type = PHP_Crypt::ZERO) |
|
67 | + { |
|
68 | + // if the size of padding is not greater than 1 |
|
69 | + // just return true, no padding will be done |
|
70 | + if(!($bytes > 0)) |
|
71 | + return true; |
|
72 | + |
|
73 | + switch($type) |
|
74 | + { |
|
75 | + case PHP_Crypt::PAD_ZERO: |
|
76 | + return self::zeroPad($text, $bytes); |
|
77 | + break; |
|
78 | + case PHP_Crypt::PAD_ANSI_X923: |
|
79 | + return self::ansiX923Pad($text, $bytes); |
|
80 | + break; |
|
81 | + case PHP_Crypt::PAD_ISO_10126: |
|
82 | + return self::iso10126Pad($text, $bytes); |
|
83 | + break; |
|
84 | + case PHP_Crypt::PAD_PKCS7: |
|
85 | + return self::pkcs7Pad($text, $bytes); |
|
86 | + break; |
|
87 | + case PHP_Crypt::PAD_ISO_7816_4: |
|
88 | + return self::iso7816Pad($text, $bytes); |
|
89 | + break; |
|
90 | + default: |
|
91 | + trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
92 | + return self::zeroPad($text, $bytes); |
|
93 | + } |
|
94 | + |
|
95 | + return true; |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Strips padding from a string |
|
101 | + * |
|
102 | + * @param string $text The string strip padding from |
|
103 | + * @param integer $type One of the predefined padding types |
|
104 | + * @return boolean True on success, false on error |
|
105 | + */ |
|
106 | + public static function strip(&$text, $type = PHP_Crypt::ZERO) |
|
107 | + { |
|
108 | + switch($type) |
|
109 | + { |
|
110 | + case PHP_Crypt::PAD_ZERO: |
|
111 | + return self::zeroStrip($text); |
|
112 | + break; |
|
113 | + case PHP_Crypt::PAD_ANSI_X923: |
|
114 | + return self::ansiX923Strip($text); |
|
115 | + break; |
|
116 | + case PHP_Crypt::PAD_ISO_10126: |
|
117 | + return self::iso10126Strip($text); |
|
118 | + break; |
|
119 | + case PHP_Crypt::PAD_PKCS7: |
|
120 | + return self::pkcs7Strip($text); |
|
121 | + break; |
|
122 | + case PHP_Crypt::PAD_ISO_7816_4: |
|
123 | + return self::iso7816Strip($text); |
|
124 | + break; |
|
125 | + default: |
|
126 | + trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
127 | + return self::zeroStrip($text); |
|
128 | + } |
|
129 | + |
|
130 | + return true; |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Pads a string with null bytes |
|
136 | + * |
|
137 | + * @param string $text The string to be padded |
|
138 | + * @param integer $bytes The number of bytes to pad |
|
139 | + * @return boolean Returns true |
|
140 | + */ |
|
141 | + private static function zeroPad(&$text, $bytes) |
|
142 | + { |
|
143 | + $len = $bytes + strlen($text); |
|
144 | + $text = str_pad($text, $len, chr(0), STR_PAD_RIGHT); |
|
145 | + return true; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * Strips null padding off a string |
|
151 | + * NOTE: This is generally a bad idea as there is no way |
|
152 | + * to distinguish a null byte that is not padding from |
|
153 | + * a null byte that is padding. Stripping null padding should |
|
154 | + * be handled by the developer at the application level. |
|
155 | + * |
|
156 | + * @param string $text The string with null padding to strip |
|
157 | + * @return boolean Returns true |
|
158 | + */ |
|
159 | + private static function zeroStrip(&$text) |
|
160 | + { |
|
161 | + // with NULL byte padding, we should not strip off the |
|
162 | + // null bytes, instead leave this to the developer at the |
|
163 | + // application level |
|
164 | + //$text = preg_replace('/\0+$/', '', $text); |
|
165 | + return true; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * Pads a string using ANSI X.923 |
|
171 | + * Adds null padding to the string, except for the last byte |
|
172 | + * which indicates the number of bytes padded |
|
173 | + * |
|
174 | + * @param string $text The string to pad |
|
175 | + * @param integer The number of bytes to pad |
|
176 | + * @return boolean Returns true |
|
177 | + */ |
|
178 | + private static function ansiX923Pad(&$text, $bytes) |
|
179 | + { |
|
180 | + $len = $bytes + strlen($text); |
|
181 | + $text = str_pad($text, ($len-1), "\0", STR_PAD_RIGHT); |
|
182 | + $text .= chr($bytes); |
|
183 | + return true; |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * Strips ANSI X.923 padding from a string |
|
189 | + * |
|
190 | + * @param string $text The string to strip padding from |
|
191 | + * @return boolean Returns true |
|
192 | + */ |
|
193 | + private static function ansiX923Strip(&$text) |
|
194 | + { |
|
195 | + $pos = strlen($text) - 1; |
|
196 | + $c = ord($text[$pos]); |
|
197 | + |
|
198 | + if($c == 0) |
|
199 | + return true; |
|
200 | + else if($c == 1) |
|
201 | + $text = substr($text, 0, -1); |
|
202 | + else |
|
203 | + { |
|
204 | + // the total null bytes are 1 less than the value of the final byte |
|
205 | + $nc = $c - 1; |
|
206 | + $text = preg_replace('/\0{'.$nc.'}'.preg_quote(chr($c)).'$/', "", $text); |
|
207 | + } |
|
208 | + |
|
209 | + return true; |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * Pads a string using ISO 10126 |
|
215 | + * Adds random bytes to the end of the string, except for the last |
|
216 | + * byte which indicates the number of padded bytes |
|
217 | + * |
|
218 | + * @param string $text The string to pad |
|
219 | + * @param integer The number of bytes to pad |
|
220 | + * @return boolean Returns true |
|
221 | + */ |
|
222 | + private static function iso10126Pad(&$text, $bytes) |
|
223 | + { |
|
224 | + // create the random pad bytes, we do one less than |
|
225 | + // needed because the last byte is reserved for the |
|
226 | + // number of padded bytes |
|
227 | + for($i = 0; $i < ($bytes - 1); ++$i) |
|
228 | + $text .= chr(mt_rand(0, 255)); |
|
229 | + |
|
230 | + // add the byte to indicate the padding length |
|
231 | + $text .= chr($bytes); |
|
232 | + return true; |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + /** |
|
237 | + * Strips ISO 10126 padding from a string |
|
238 | + * |
|
239 | + * @param string $text The string to strip padding from |
|
240 | + * @return boolean Returns true |
|
241 | + */ |
|
242 | + private static function iso10126Strip(&$text) |
|
243 | + { |
|
244 | + $pos = strlen($text) - 1; |
|
245 | + $c = ord($text[$pos]) * -1; |
|
246 | + |
|
247 | + // if we got a null byte at the end of the string, |
|
248 | + // just return |
|
249 | + if($c == 0) |
|
250 | + return true; |
|
251 | + |
|
252 | + $text = substr($text, 0, $c); |
|
253 | + return true; |
|
254 | + } |
|
255 | + |
|
256 | + |
|
257 | + /** |
|
258 | + * Pads a string using PKCS7 |
|
259 | + * Adds padding using the bytes with the value of the |
|
260 | + * number of bytes need for padding |
|
261 | + * |
|
262 | + * @param string $text The string to pad |
|
263 | + * @param integer The number of bytes to pad |
|
264 | + * @return boolean Returns true |
|
265 | + */ |
|
266 | + private static function pkcs7Pad(&$text, $bytes) |
|
267 | + { |
|
268 | + $len = $bytes + strlen($text); |
|
269 | + $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
270 | + return true; |
|
271 | + } |
|
272 | + |
|
273 | + |
|
274 | + /** |
|
275 | + * Strips PKCS7 padding from a string |
|
276 | + * |
|
277 | + * @param string $text The string to strip padding from |
|
278 | + * @return boolean Returns true |
|
279 | + */ |
|
280 | + private static function pkcs7Strip(&$text) |
|
281 | + { |
|
282 | + $pos = strlen($text) - 1; |
|
283 | + $c = ord($text[$pos]); |
|
284 | + |
|
285 | + if($c == 0) |
|
286 | + return true; |
|
287 | + |
|
288 | + $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
|
289 | + return true; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * Pads a string using ISO/IEC 7816-4 |
|
295 | + * Adds byte 0x80 followed by null bytes to pad a string |
|
296 | + * |
|
297 | + * @param string $text The string to pad |
|
298 | + * @param integer The number of bytes to pad |
|
299 | + * @return boolean Returns true |
|
300 | + */ |
|
301 | + private static function iso7816Pad(&$text, $bytes) |
|
302 | + { |
|
303 | + $text .= chr(0x80); |
|
304 | + $len = $bytes + strlen($text); |
|
305 | + |
|
306 | + // if we are only padding one byte, then 0x80 is all we need |
|
307 | + // else we follow up with null bytes |
|
308 | + if($bytes > 1) |
|
309 | + $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
310 | + |
|
311 | + return true; |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * Strips ISO/IEC 7816-4 padding from a string |
|
317 | + * |
|
318 | + * @param string $text The string to strip padding from |
|
319 | + * @return boolean Returns true |
|
320 | + */ |
|
321 | + private static function iso7816Strip(&$text) |
|
322 | + { |
|
323 | + $c = chr(0x80); |
|
324 | + $text = preg_replace('/'.preg_quote($c).'\0*$/', '', $text); |
|
325 | + return true; |
|
326 | + } |
|
327 | 327 | } |
328 | 328 | ?> |
@@ -67,10 +67,10 @@ discard block |
||
67 | 67 | { |
68 | 68 | // if the size of padding is not greater than 1 |
69 | 69 | // just return true, no padding will be done |
70 | - if(!($bytes > 0)) |
|
70 | + if (!($bytes > 0)) |
|
71 | 71 | return true; |
72 | 72 | |
73 | - switch($type) |
|
73 | + switch ($type) |
|
74 | 74 | { |
75 | 75 | case PHP_Crypt::PAD_ZERO: |
76 | 76 | return self::zeroPad($text, $bytes); |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | public static function strip(&$text, $type = PHP_Crypt::ZERO) |
107 | 107 | { |
108 | - switch($type) |
|
108 | + switch ($type) |
|
109 | 109 | { |
110 | 110 | case PHP_Crypt::PAD_ZERO: |
111 | 111 | return self::zeroStrip($text); |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | private static function ansiX923Pad(&$text, $bytes) |
179 | 179 | { |
180 | 180 | $len = $bytes + strlen($text); |
181 | - $text = str_pad($text, ($len-1), "\0", STR_PAD_RIGHT); |
|
181 | + $text = str_pad($text, ($len - 1), "\0", STR_PAD_RIGHT); |
|
182 | 182 | $text .= chr($bytes); |
183 | 183 | return true; |
184 | 184 | } |
@@ -195,9 +195,9 @@ discard block |
||
195 | 195 | $pos = strlen($text) - 1; |
196 | 196 | $c = ord($text[$pos]); |
197 | 197 | |
198 | - if($c == 0) |
|
198 | + if ($c == 0) |
|
199 | 199 | return true; |
200 | - else if($c == 1) |
|
200 | + else if ($c == 1) |
|
201 | 201 | $text = substr($text, 0, -1); |
202 | 202 | else |
203 | 203 | { |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | // create the random pad bytes, we do one less than |
225 | 225 | // needed because the last byte is reserved for the |
226 | 226 | // number of padded bytes |
227 | - for($i = 0; $i < ($bytes - 1); ++$i) |
|
227 | + for ($i = 0; $i < ($bytes - 1); ++$i) |
|
228 | 228 | $text .= chr(mt_rand(0, 255)); |
229 | 229 | |
230 | 230 | // add the byte to indicate the padding length |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | |
247 | 247 | // if we got a null byte at the end of the string, |
248 | 248 | // just return |
249 | - if($c == 0) |
|
249 | + if ($c == 0) |
|
250 | 250 | return true; |
251 | 251 | |
252 | 252 | $text = substr($text, 0, $c); |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | private static function pkcs7Pad(&$text, $bytes) |
267 | 267 | { |
268 | 268 | $len = $bytes + strlen($text); |
269 | - $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
269 | + $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
270 | 270 | return true; |
271 | 271 | } |
272 | 272 | |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | $pos = strlen($text) - 1; |
283 | 283 | $c = ord($text[$pos]); |
284 | 284 | |
285 | - if($c == 0) |
|
285 | + if ($c == 0) |
|
286 | 286 | return true; |
287 | 287 | |
288 | 288 | $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
@@ -305,8 +305,8 @@ discard block |
||
305 | 305 | |
306 | 306 | // if we are only padding one byte, then 0x80 is all we need |
307 | 307 | // else we follow up with null bytes |
308 | - if($bytes > 1) |
|
309 | - $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
308 | + if ($bytes > 1) |
|
309 | + $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
310 | 310 | |
311 | 311 | return true; |
312 | 312 | } |
@@ -67,8 +67,9 @@ discard block |
||
67 | 67 | { |
68 | 68 | // if the size of padding is not greater than 1 |
69 | 69 | // just return true, no padding will be done |
70 | - if(!($bytes > 0)) |
|
71 | - return true; |
|
70 | + if(!($bytes > 0)) { |
|
71 | + return true; |
|
72 | + } |
|
72 | 73 | |
73 | 74 | switch($type) |
74 | 75 | { |
@@ -195,11 +196,11 @@ discard block |
||
195 | 196 | $pos = strlen($text) - 1; |
196 | 197 | $c = ord($text[$pos]); |
197 | 198 | |
198 | - if($c == 0) |
|
199 | - return true; |
|
200 | - else if($c == 1) |
|
201 | - $text = substr($text, 0, -1); |
|
202 | - else |
|
199 | + if($c == 0) { |
|
200 | + return true; |
|
201 | + } else if($c == 1) { |
|
202 | + $text = substr($text, 0, -1); |
|
203 | + } else |
|
203 | 204 | { |
204 | 205 | // the total null bytes are 1 less than the value of the final byte |
205 | 206 | $nc = $c - 1; |
@@ -224,8 +225,9 @@ discard block |
||
224 | 225 | // create the random pad bytes, we do one less than |
225 | 226 | // needed because the last byte is reserved for the |
226 | 227 | // number of padded bytes |
227 | - for($i = 0; $i < ($bytes - 1); ++$i) |
|
228 | - $text .= chr(mt_rand(0, 255)); |
|
228 | + for($i = 0; $i < ($bytes - 1); ++$i) { |
|
229 | + $text .= chr(mt_rand(0, 255)); |
|
230 | + } |
|
229 | 231 | |
230 | 232 | // add the byte to indicate the padding length |
231 | 233 | $text .= chr($bytes); |
@@ -246,8 +248,9 @@ discard block |
||
246 | 248 | |
247 | 249 | // if we got a null byte at the end of the string, |
248 | 250 | // just return |
249 | - if($c == 0) |
|
250 | - return true; |
|
251 | + if($c == 0) { |
|
252 | + return true; |
|
253 | + } |
|
251 | 254 | |
252 | 255 | $text = substr($text, 0, $c); |
253 | 256 | return true; |
@@ -282,8 +285,9 @@ discard block |
||
282 | 285 | $pos = strlen($text) - 1; |
283 | 286 | $c = ord($text[$pos]); |
284 | 287 | |
285 | - if($c == 0) |
|
286 | - return true; |
|
288 | + if($c == 0) { |
|
289 | + return true; |
|
290 | + } |
|
287 | 291 | |
288 | 292 | $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
289 | 293 | return true; |
@@ -305,8 +309,9 @@ discard block |
||
305 | 309 | |
306 | 310 | // if we are only padding one byte, then 0x80 is all we need |
307 | 311 | // else we follow up with null bytes |
308 | - if($bytes > 1) |
|
309 | - $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
312 | + if($bytes > 1) { |
|
313 | + $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
314 | + } |
|
310 | 315 | |
311 | 316 | return true; |
312 | 317 | } |
@@ -97,8 +97,8 @@ |
||
97 | 97 | * @param string $key The key to use for the selected Cipher |
98 | 98 | * @param string $cipher The type of cipher to use |
99 | 99 | * @param string $mode The encrypt mode to use with the cipher |
100 | - * @param string $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | - * @return void |
|
100 | + * @param integer $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | + * @return string |
|
102 | 102 | */ |
103 | 103 | public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
104 | 104 | { |
@@ -35,423 +35,423 @@ |
||
35 | 35 | */ |
36 | 36 | class PHP_Crypt |
37 | 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 string $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | - * @return void |
|
102 | - */ |
|
103 | - public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
104 | - { |
|
105 | - /* |
|
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 string $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | + * @return void |
|
102 | + */ |
|
103 | + public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
104 | + { |
|
105 | + /* |
|
106 | 106 | * CIPHERS |
107 | 107 | */ |
108 | - switch($cipher) |
|
109 | - { |
|
110 | - case self::CIPHER_3DES: |
|
111 | - $this->cipher = new Cipher_3DES($key); |
|
112 | - break; |
|
108 | + switch($cipher) |
|
109 | + { |
|
110 | + case self::CIPHER_3DES: |
|
111 | + $this->cipher = new Cipher_3DES($key); |
|
112 | + break; |
|
113 | 113 | |
114 | - case self::CIPHER_3WAY: |
|
115 | - $this->cipher = new Cipher_3WAY($key); |
|
116 | - break; |
|
114 | + case self::CIPHER_3WAY: |
|
115 | + $this->cipher = new Cipher_3WAY($key); |
|
116 | + break; |
|
117 | 117 | |
118 | - case self::CIPHER_AES_128: |
|
119 | - $this->cipher = new Cipher_AES_128($key); |
|
120 | - break; |
|
118 | + case self::CIPHER_AES_128: |
|
119 | + $this->cipher = new Cipher_AES_128($key); |
|
120 | + break; |
|
121 | 121 | |
122 | - case self::CIPHER_AES_192: |
|
123 | - $this->cipher = new Cipher_AES_192($key); |
|
124 | - break; |
|
122 | + case self::CIPHER_AES_192: |
|
123 | + $this->cipher = new Cipher_AES_192($key); |
|
124 | + break; |
|
125 | 125 | |
126 | - case self::CIPHER_AES_256: |
|
127 | - $this->cipher = new Cipher_AES_256($key); |
|
128 | - break; |
|
126 | + case self::CIPHER_AES_256: |
|
127 | + $this->cipher = new Cipher_AES_256($key); |
|
128 | + break; |
|
129 | 129 | |
130 | - case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | - $this->cipher = new Cipher_ARC4($key); |
|
132 | - break; |
|
130 | + case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | + $this->cipher = new Cipher_ARC4($key); |
|
132 | + break; |
|
133 | 133 | |
134 | - case self::CIPHER_BLOWFISH: |
|
135 | - $this->cipher = new Cipher_Blowfish($key); |
|
136 | - break; |
|
134 | + case self::CIPHER_BLOWFISH: |
|
135 | + $this->cipher = new Cipher_Blowfish($key); |
|
136 | + break; |
|
137 | 137 | |
138 | - case self::CIPHER_CAST_128: |
|
139 | - $this->cipher = new Cipher_CAST_128($key); |
|
140 | - break; |
|
138 | + case self::CIPHER_CAST_128: |
|
139 | + $this->cipher = new Cipher_CAST_128($key); |
|
140 | + break; |
|
141 | 141 | |
142 | - case self::CIPHER_CAST_256: |
|
143 | - $this->cipher = new Cipher_CAST_256($key); |
|
144 | - break; |
|
142 | + case self::CIPHER_CAST_256: |
|
143 | + $this->cipher = new Cipher_CAST_256($key); |
|
144 | + break; |
|
145 | 145 | |
146 | - case self::CIPHER_DES: |
|
147 | - $this->cipher = new Cipher_DES($key); |
|
148 | - break; |
|
146 | + case self::CIPHER_DES: |
|
147 | + $this->cipher = new Cipher_DES($key); |
|
148 | + break; |
|
149 | 149 | |
150 | - case self::CIPHER_ENIGMA: |
|
151 | - $this->cipher = new Cipher_Enigma($key); |
|
152 | - break; |
|
150 | + case self::CIPHER_ENIGMA: |
|
151 | + $this->cipher = new Cipher_Enigma($key); |
|
152 | + break; |
|
153 | 153 | |
154 | - case self::CIPHER_GOST: |
|
155 | - $this->cipher = new Cipher_GOST($key); |
|
156 | - break; |
|
154 | + case self::CIPHER_GOST: |
|
155 | + $this->cipher = new Cipher_GOST($key); |
|
156 | + break; |
|
157 | 157 | |
158 | - case self::CIPHER_RC2: |
|
159 | - $this->cipher = new Cipher_RC2($key); |
|
160 | - break; |
|
158 | + case self::CIPHER_RC2: |
|
159 | + $this->cipher = new Cipher_RC2($key); |
|
160 | + break; |
|
161 | 161 | |
162 | - case self::CIPHER_RIJNDAEL_128: |
|
163 | - $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | - break; |
|
162 | + case self::CIPHER_RIJNDAEL_128: |
|
163 | + $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | + break; |
|
165 | 165 | |
166 | - case self::CIPHER_RIJNDAEL_192: |
|
167 | - $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | - break; |
|
166 | + case self::CIPHER_RIJNDAEL_192: |
|
167 | + $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | + break; |
|
169 | 169 | |
170 | - case self::CIPHER_RIJNDAEL_256: |
|
171 | - $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | - break; |
|
170 | + case self::CIPHER_RIJNDAEL_256: |
|
171 | + $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | + break; |
|
173 | 173 | |
174 | - case self::CIPHER_SIMPLEXOR: |
|
175 | - $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | - break; |
|
174 | + case self::CIPHER_SIMPLEXOR: |
|
175 | + $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | + break; |
|
177 | 177 | |
178 | - case self::CIPHER_SKIPJACK: |
|
179 | - $this->cipher = new Cipher_Skipjack($key); |
|
180 | - break; |
|
178 | + case self::CIPHER_SKIPJACK: |
|
179 | + $this->cipher = new Cipher_Skipjack($key); |
|
180 | + break; |
|
181 | 181 | |
182 | - case self::CIPHER_VIGENERE: |
|
183 | - $this->cipher = new Cipher_Vigenere($key); |
|
184 | - break; |
|
182 | + case self::CIPHER_VIGENERE: |
|
183 | + $this->cipher = new Cipher_Vigenere($key); |
|
184 | + break; |
|
185 | 185 | |
186 | - default: |
|
187 | - trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
188 | - } |
|
186 | + default: |
|
187 | + trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
188 | + } |
|
189 | 189 | |
190 | 190 | |
191 | - /* |
|
191 | + /* |
|
192 | 192 | * MODES |
193 | 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 | - } |
|
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 | 456 | } |
457 | 457 | ?> |
@@ -107,84 +107,84 @@ discard block |
||
107 | 107 | */ |
108 | 108 | switch($cipher) |
109 | 109 | { |
110 | - case self::CIPHER_3DES: |
|
111 | - $this->cipher = new Cipher_3DES($key); |
|
112 | - break; |
|
110 | + case self::CIPHER_3DES: |
|
111 | + $this->cipher = new Cipher_3DES($key); |
|
112 | + break; |
|
113 | 113 | |
114 | - case self::CIPHER_3WAY: |
|
115 | - $this->cipher = new Cipher_3WAY($key); |
|
116 | - break; |
|
114 | + case self::CIPHER_3WAY: |
|
115 | + $this->cipher = new Cipher_3WAY($key); |
|
116 | + break; |
|
117 | 117 | |
118 | - case self::CIPHER_AES_128: |
|
119 | - $this->cipher = new Cipher_AES_128($key); |
|
120 | - break; |
|
118 | + case self::CIPHER_AES_128: |
|
119 | + $this->cipher = new Cipher_AES_128($key); |
|
120 | + break; |
|
121 | 121 | |
122 | - case self::CIPHER_AES_192: |
|
123 | - $this->cipher = new Cipher_AES_192($key); |
|
124 | - break; |
|
122 | + case self::CIPHER_AES_192: |
|
123 | + $this->cipher = new Cipher_AES_192($key); |
|
124 | + break; |
|
125 | 125 | |
126 | - case self::CIPHER_AES_256: |
|
127 | - $this->cipher = new Cipher_AES_256($key); |
|
128 | - break; |
|
126 | + case self::CIPHER_AES_256: |
|
127 | + $this->cipher = new Cipher_AES_256($key); |
|
128 | + break; |
|
129 | 129 | |
130 | - case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | - $this->cipher = new Cipher_ARC4($key); |
|
132 | - break; |
|
130 | + case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | + $this->cipher = new Cipher_ARC4($key); |
|
132 | + break; |
|
133 | 133 | |
134 | - case self::CIPHER_BLOWFISH: |
|
135 | - $this->cipher = new Cipher_Blowfish($key); |
|
136 | - break; |
|
134 | + case self::CIPHER_BLOWFISH: |
|
135 | + $this->cipher = new Cipher_Blowfish($key); |
|
136 | + break; |
|
137 | 137 | |
138 | - case self::CIPHER_CAST_128: |
|
139 | - $this->cipher = new Cipher_CAST_128($key); |
|
140 | - break; |
|
138 | + case self::CIPHER_CAST_128: |
|
139 | + $this->cipher = new Cipher_CAST_128($key); |
|
140 | + break; |
|
141 | 141 | |
142 | - case self::CIPHER_CAST_256: |
|
143 | - $this->cipher = new Cipher_CAST_256($key); |
|
144 | - break; |
|
142 | + case self::CIPHER_CAST_256: |
|
143 | + $this->cipher = new Cipher_CAST_256($key); |
|
144 | + break; |
|
145 | 145 | |
146 | - case self::CIPHER_DES: |
|
147 | - $this->cipher = new Cipher_DES($key); |
|
148 | - break; |
|
146 | + case self::CIPHER_DES: |
|
147 | + $this->cipher = new Cipher_DES($key); |
|
148 | + break; |
|
149 | 149 | |
150 | - case self::CIPHER_ENIGMA: |
|
151 | - $this->cipher = new Cipher_Enigma($key); |
|
152 | - break; |
|
150 | + case self::CIPHER_ENIGMA: |
|
151 | + $this->cipher = new Cipher_Enigma($key); |
|
152 | + break; |
|
153 | 153 | |
154 | - case self::CIPHER_GOST: |
|
155 | - $this->cipher = new Cipher_GOST($key); |
|
156 | - break; |
|
154 | + case self::CIPHER_GOST: |
|
155 | + $this->cipher = new Cipher_GOST($key); |
|
156 | + break; |
|
157 | 157 | |
158 | - case self::CIPHER_RC2: |
|
159 | - $this->cipher = new Cipher_RC2($key); |
|
160 | - break; |
|
158 | + case self::CIPHER_RC2: |
|
159 | + $this->cipher = new Cipher_RC2($key); |
|
160 | + break; |
|
161 | 161 | |
162 | - case self::CIPHER_RIJNDAEL_128: |
|
163 | - $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | - break; |
|
162 | + case self::CIPHER_RIJNDAEL_128: |
|
163 | + $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | + break; |
|
165 | 165 | |
166 | - case self::CIPHER_RIJNDAEL_192: |
|
167 | - $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | - break; |
|
166 | + case self::CIPHER_RIJNDAEL_192: |
|
167 | + $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | + break; |
|
169 | 169 | |
170 | - case self::CIPHER_RIJNDAEL_256: |
|
171 | - $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | - break; |
|
170 | + case self::CIPHER_RIJNDAEL_256: |
|
171 | + $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | + break; |
|
173 | 173 | |
174 | - case self::CIPHER_SIMPLEXOR: |
|
175 | - $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | - break; |
|
174 | + case self::CIPHER_SIMPLEXOR: |
|
175 | + $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | + break; |
|
177 | 177 | |
178 | - case self::CIPHER_SKIPJACK: |
|
179 | - $this->cipher = new Cipher_Skipjack($key); |
|
180 | - break; |
|
178 | + case self::CIPHER_SKIPJACK: |
|
179 | + $this->cipher = new Cipher_Skipjack($key); |
|
180 | + break; |
|
181 | 181 | |
182 | - case self::CIPHER_VIGENERE: |
|
183 | - $this->cipher = new Cipher_Vigenere($key); |
|
184 | - break; |
|
182 | + case self::CIPHER_VIGENERE: |
|
183 | + $this->cipher = new Cipher_Vigenere($key); |
|
184 | + break; |
|
185 | 185 | |
186 | - default: |
|
187 | - trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
186 | + default: |
|
187 | + trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | |
@@ -193,48 +193,48 @@ discard block |
||
193 | 193 | */ |
194 | 194 | switch($mode) |
195 | 195 | { |
196 | - case self::MODE_CBC: |
|
197 | - $this->mode = new Mode_CBC($this->cipher); |
|
198 | - break; |
|
196 | + case self::MODE_CBC: |
|
197 | + $this->mode = new Mode_CBC($this->cipher); |
|
198 | + break; |
|
199 | 199 | |
200 | - case self::MODE_CFB: |
|
201 | - $this->mode = new Mode_CFB($this->cipher); |
|
202 | - break; |
|
200 | + case self::MODE_CFB: |
|
201 | + $this->mode = new Mode_CFB($this->cipher); |
|
202 | + break; |
|
203 | 203 | |
204 | - case self::MODE_CTR: |
|
205 | - $this->mode = new Mode_CTR($this->cipher); |
|
206 | - break; |
|
204 | + case self::MODE_CTR: |
|
205 | + $this->mode = new Mode_CTR($this->cipher); |
|
206 | + break; |
|
207 | 207 | |
208 | - case self::MODE_ECB: |
|
209 | - $this->mode = new Mode_ECB($this->cipher); |
|
210 | - break; |
|
208 | + case self::MODE_ECB: |
|
209 | + $this->mode = new Mode_ECB($this->cipher); |
|
210 | + break; |
|
211 | 211 | |
212 | - case self::MODE_NCFB: |
|
213 | - $this->mode = new Mode_NCFB($this->cipher); |
|
214 | - break; |
|
212 | + case self::MODE_NCFB: |
|
213 | + $this->mode = new Mode_NCFB($this->cipher); |
|
214 | + break; |
|
215 | 215 | |
216 | - case self::MODE_NOFB: |
|
217 | - $this->mode = new Mode_NOFB($this->cipher); |
|
218 | - break; |
|
216 | + case self::MODE_NOFB: |
|
217 | + $this->mode = new Mode_NOFB($this->cipher); |
|
218 | + break; |
|
219 | 219 | |
220 | - case self::MODE_OFB: |
|
221 | - $this->mode = new Mode_OFB($this->cipher); |
|
222 | - break; |
|
220 | + case self::MODE_OFB: |
|
221 | + $this->mode = new Mode_OFB($this->cipher); |
|
222 | + break; |
|
223 | 223 | |
224 | - case self::MODE_PCBC: |
|
225 | - $this->mode = new Mode_PCBC($this->cipher); |
|
226 | - break; |
|
224 | + case self::MODE_PCBC: |
|
225 | + $this->mode = new Mode_PCBC($this->cipher); |
|
226 | + break; |
|
227 | 227 | |
228 | - case self::MODE_RAW: |
|
229 | - $this->mode = new Mode_RAW($this->cipher); |
|
230 | - break; |
|
228 | + case self::MODE_RAW: |
|
229 | + $this->mode = new Mode_RAW($this->cipher); |
|
230 | + break; |
|
231 | 231 | |
232 | - case self::MODE_STREAM: |
|
233 | - $this->mode = new Mode_Stream($this->cipher); |
|
234 | - break; |
|
232 | + case self::MODE_STREAM: |
|
233 | + $this->mode = new Mode_Stream($this->cipher); |
|
234 | + break; |
|
235 | 235 | |
236 | - default: |
|
237 | - trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
236 | + default: |
|
237 | + trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
238 | 238 | } |
239 | 239 | |
240 | 240 | // set the default padding |
@@ -41,46 +41,46 @@ discard block |
||
41 | 41 | const CIPHER_AES_128 = "AES-128"; |
42 | 42 | const CIPHER_AES_192 = "AES-192"; |
43 | 43 | const CIPHER_AES_256 = "AES-256"; |
44 | - const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
44 | + const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
45 | 45 | const CIPHER_BLOWFISH = "Blowfish"; |
46 | 46 | const CIPHER_CAST_128 = "CAST-128"; |
47 | 47 | const CIPHER_CAST_256 = "CAST-256"; |
48 | 48 | const CIPHER_DES = "DES"; |
49 | - const CIPHER_ENIGMA = "Enigma"; |
|
50 | - const CIPHER_GOST = "GOST"; |
|
49 | + const CIPHER_ENIGMA = "Enigma"; |
|
50 | + const CIPHER_GOST = "GOST"; |
|
51 | 51 | const CIPHER_RC2 = "RC2"; |
52 | 52 | const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
53 | 53 | const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
54 | 54 | const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
55 | 55 | const CIPHER_SKIPJACK = "Skipjack"; |
56 | - const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
56 | + const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
57 | 57 | const CIPHER_VIGENERE = "Vigenere"; // historical |
58 | 58 | |
59 | 59 | // Modes |
60 | 60 | const MODE_CBC = "CBC"; |
61 | - const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
61 | + const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
62 | 62 | const MODE_CTR = "CTR"; |
63 | 63 | const MODE_ECB = "ECB"; |
64 | 64 | const MODE_NCFB = "NCFB"; // blocksize cfb mode |
65 | 65 | const MODE_NOFB = "NOFB"; // blocksize ofb mode |
66 | - const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
66 | + const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
67 | 67 | const MODE_PCBC = "PCBC"; |
68 | - const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
68 | + const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
69 | 69 | const MODE_STREAM = "Stream"; // used only for stream ciphers |
70 | 70 | |
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 | - const RAND = "rand"; // uses mt_rand(), windows & unix |
|
73 | + const RAND = "rand"; // uses mt_rand(), windows & unix |
|
74 | 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 |
|
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 | 78 | |
79 | 79 | // Padding types |
80 | - const PAD_ZERO = 0; |
|
80 | + const PAD_ZERO = 0; |
|
81 | 81 | const PAD_ANSI_X923 = 1; |
82 | 82 | const PAD_ISO_10126 = 2; |
83 | - const PAD_PKCS7 = 3; |
|
83 | + const PAD_PKCS7 = 3; |
|
84 | 84 | const PAD_ISO_7816_4 = 4; |
85 | 85 | |
86 | 86 | |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | /* |
106 | 106 | * CIPHERS |
107 | 107 | */ |
108 | - switch($cipher) |
|
108 | + switch ($cipher) |
|
109 | 109 | { |
110 | 110 | case self::CIPHER_3DES: |
111 | 111 | $this->cipher = new Cipher_3DES($key); |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | /* |
192 | 192 | * MODES |
193 | 193 | */ |
194 | - switch($mode) |
|
194 | + switch ($mode) |
|
195 | 195 | { |
196 | 196 | case self::MODE_CBC: |
197 | 197 | $this->mode = new Mode_CBC($this->cipher); |
@@ -443,6 +443,7 @@ |
||
443 | 443 | * @param int $id The ID of the current node to process |
444 | 444 | * @param int $level The nlevel to assign to the current node |
445 | 445 | * @param int &$n A reference to the running tally for the n-value |
446 | + * @param integer $n |
|
446 | 447 | */ |
447 | 448 | public function generateTreeData(&$arr, $id, $level, &$n) |
448 | 449 | { |
@@ -35,8 +35,8 @@ discard block |
||
35 | 35 | $this->table = $table; |
36 | 36 | |
37 | 37 | $this->fields = array('id' => $idField, |
38 | - 'parent' => $parentField, |
|
39 | - 'sort' => $sortField |
|
38 | + 'parent' => $parentField, |
|
39 | + 'sort' => $sortField |
|
40 | 40 | ); |
41 | 41 | } |
42 | 42 | |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | public function getFields() |
50 | 50 | { |
51 | 51 | return array($this->fields['id'], $this->fields['parent'], $this->fields['sort'], |
52 | - 'nleft', 'nright', 'nlevel', 'personal_folder', 'renewal_period', 'bloquer_modification', 'bloquer_creation'); |
|
52 | + 'nleft', 'nright', 'nlevel', 'personal_folder', 'renewal_period', 'bloquer_modification', 'bloquer_creation'); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -356,12 +356,12 @@ discard block |
||
356 | 356 | $idField = $this->fields['id']; |
357 | 357 | $parentField = $this->fields['parent']; |
358 | 358 | |
359 | - $query = sprintf( |
|
360 | - 'select %s from %s order by %s', |
|
361 | - join(',', $this->getFields()), |
|
362 | - $this->table, |
|
363 | - $this->fields['sort'] |
|
364 | - ); |
|
359 | + $query = sprintf( |
|
360 | + 'select %s from %s order by %s', |
|
361 | + join(',', $this->getFields()), |
|
362 | + $this->table, |
|
363 | + $this->fields['sort'] |
|
364 | + ); |
|
365 | 365 | |
366 | 366 | $result = mysqli_query($link, $query); |
367 | 367 |
@@ -50,6 +50,7 @@ |
||
50 | 50 | * genHash() |
51 | 51 | * |
52 | 52 | * Generate a hash for user login |
53 | + * @param string $password |
|
53 | 54 | */ |
54 | 55 | function bCrypt($password, $cost) |
55 | 56 | { |
@@ -22,13 +22,13 @@ discard block |
||
22 | 22 | function chmod_r($dir, $dirPermissions, $filePermissions) { |
23 | 23 | $dp = opendir($dir); |
24 | 24 | $res = true; |
25 | - while($file = readdir($dp)) { |
|
25 | + while ($file = readdir($dp)) { |
|
26 | 26 | if (($file == ".") || ($file == "..")) |
27 | 27 | continue; |
28 | 28 | |
29 | 29 | $fullPath = $dir."/".$file; |
30 | 30 | |
31 | - if(is_dir($fullPath)) { |
|
31 | + if (is_dir($fullPath)) { |
|
32 | 32 | if ($res = @chmod($fullPath, $dirPermissions)) |
33 | 33 | $res = @chmod_r($fullPath, $dirPermissions, $filePermissions); |
34 | 34 | } else { |
@@ -57,9 +57,9 @@ discard block |
||
57 | 57 | if (function_exists('openssl_random_pseudo_bytes')) { |
58 | 58 | $salt .= bin2hex(openssl_random_pseudo_bytes(11)); |
59 | 59 | } else { |
60 | - $chars='./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
61 | - for ($i=0; $i<22; $i++) { |
|
62 | - $salt.=$chars[mt_rand(0, 63)]; |
|
60 | + $chars = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
61 | + for ($i = 0; $i < 22; $i++) { |
|
62 | + $salt .= $chars[mt_rand(0, 63)]; |
|
63 | 63 | } |
64 | 64 | } |
65 | 65 | return crypt($password, $salt); |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | switch ($_POST['type']) { |
70 | 70 | case "step_2": |
71 | 71 | //decrypt |
72 | - require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
72 | + require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
73 | 73 | $json = Encryption\Crypt\aesctr::decrypt($_POST['data'], "cpm", 128); |
74 | 74 | $data = json_decode($json, true); |
75 | 75 | $json = Encryption\Crypt\aesctr::decrypt($_POST['activity'], "cpm", 128); |
@@ -78,8 +78,8 @@ discard block |
||
78 | 78 | $data = array_merge($data, array("task" => $json)); |
79 | 79 | |
80 | 80 | $abspath = str_replace('\\', '/', $data['root_path']); |
81 | - if (substr($abspath, strlen($abspath)-1) == "/") { |
|
82 | - $abspath = substr($abspath, 0, strlen($abspath)-1); |
|
81 | + if (substr($abspath, strlen($abspath) - 1) == "/") { |
|
82 | + $abspath = substr($abspath, 0, strlen($abspath) - 1); |
|
83 | 83 | } |
84 | 84 | $_SESSION['abspath'] = $abspath; |
85 | 85 | $_SESSION['url_path'] = $data['url_path']; |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | } |
122 | 122 | |
123 | 123 | if (isset($data['activity']) && $data['activity'] == "ini") { |
124 | - if (ini_get($data['task'])>=60) { |
|
124 | + if (ini_get($data['task']) >= 60) { |
|
125 | 125 | echo '[{"error" : "", "index" : "'.$_POST['index'].'"}]'; |
126 | 126 | } else { |
127 | 127 | echo '[{"error" : "PHP \"Maximum execution time\" is set to '.ini_get('max_execution_time').' seconds. Please try to set to 60s at least during installation.", "index" : "'.$_POST['index'].'", "multiple" : "'.$_POST['multiple'].'"}]'; |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | |
133 | 133 | case "step_3": |
134 | 134 | //decrypt |
135 | - require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
135 | + require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
136 | 136 | $json = Encryption\Crypt\aesctr::decrypt($_POST['data'], "cpm", 128); |
137 | 137 | $data = json_decode($json, true); |
138 | 138 | $json = Encryption\Crypt\aesctr::decrypt($_POST['db'], "cpm", 128); |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | ) CHARSET=utf8;" |
149 | 149 | ); |
150 | 150 | // store values |
151 | - foreach($data as $key=>$value) { |
|
151 | + foreach ($data as $key=>$value) { |
|
152 | 152 | $_SESSION[$key] = $value; |
153 | 153 | $tmp = mysqli_fetch_row(mysqli_query($dbTmp, "SELECT COUNT(*) FROM `_install` WHERE `key` = '".$key."'")); |
154 | 154 | if ($tmp[0] == 0 || empty($tmp[0])) { |
@@ -161,13 +161,13 @@ discard block |
||
161 | 161 | if ($tmp[0] == 0 || empty($tmp[0])) { |
162 | 162 | mysqli_query($dbTmp, "INSERT INTO `_install` (`key`, `value`) VALUES ('url_path', '", empty($_SESSION['url_path']) ? $db['url_path'] : $_SESSION['url_path'], "');"); |
163 | 163 | } else { |
164 | - mysqli_query($dbTmp, "UPDATE `_install` SET `value` = '", empty($_SESSION['url_path']) ? $db['url_path'] : $_SESSION['url_path'],"' WHERE `key` = 'url_path';"); |
|
164 | + mysqli_query($dbTmp, "UPDATE `_install` SET `value` = '", empty($_SESSION['url_path']) ? $db['url_path'] : $_SESSION['url_path'], "' WHERE `key` = 'url_path';"); |
|
165 | 165 | } |
166 | 166 | $tmp = mysqli_fetch_row(mysqli_query($dbTmp, "SELECT COUNT(*) FROM `_install` WHERE `key` = 'abspath'")); |
167 | 167 | if ($tmp[0] == 0 || empty($tmp[0])) { |
168 | - mysqli_query($dbTmp, "INSERT INTO `_install` (`key`, `value`) VALUES ('abspath', '", empty($_SESSION['abspath']) ? $db['abspath'] : $_SESSION['abspath'],"');"); |
|
168 | + mysqli_query($dbTmp, "INSERT INTO `_install` (`key`, `value`) VALUES ('abspath', '", empty($_SESSION['abspath']) ? $db['abspath'] : $_SESSION['abspath'], "');"); |
|
169 | 169 | } else { |
170 | - mysqli_query($dbTmp, "UPDATE `_install` SET `value` = '", empty($_SESSION['abspath']) ? $db['abspath'] : $_SESSION['abspath'],"' WHERE `key` = 'abspath';"); |
|
170 | + mysqli_query($dbTmp, "UPDATE `_install` SET `value` = '", empty($_SESSION['abspath']) ? $db['abspath'] : $_SESSION['abspath'], "' WHERE `key` = 'abspath';"); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | echo '[{"error" : "", "result" : "Connection is successful", "multiple" : ""}]'; |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | |
180 | 180 | case "step_4": |
181 | 181 | //decrypt |
182 | - require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
182 | + require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
183 | 183 | $json = Encryption\Crypt\aesctr::decrypt($_POST['data'], "cpm", 128); |
184 | 184 | $data = json_decode($json, true); |
185 | 185 | $json = Encryption\Crypt\aesctr::decrypt($_POST['db'], "cpm", 128); |
@@ -188,8 +188,8 @@ discard block |
||
188 | 188 | $dbTmp = mysqli_connect($db['db_host'], $db['db_login'], $db['db_pw'], $db['db_bdd'], $db['db_port']); |
189 | 189 | |
190 | 190 | // prepare data |
191 | - foreach($data as $key=>$value) { |
|
192 | - $data[$key] = str_replace(array('"', '\'), array('""','\\\\'), $value); |
|
191 | + foreach ($data as $key=>$value) { |
|
192 | + $data[$key] = str_replace(array('"', '\'), array('""', '\\\\'), $value); |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | // check skpath |
@@ -198,13 +198,13 @@ discard block |
||
198 | 198 | } else { |
199 | 199 | $data['sk_path'] = str_replace("\", "/", $data['sk_path']); |
200 | 200 | } |
201 | - if (substr($data['sk_path'], strlen($data['sk_path'])-1) == "/" || substr($data['sk_path'], strlen($data['sk_path'])-1) == "\"") { |
|
202 | - $data['sk_path'] = substr($data['sk_path'], 0, strlen($data['sk_path'])-1); |
|
201 | + if (substr($data['sk_path'], strlen($data['sk_path']) - 1) == "/" || substr($data['sk_path'], strlen($data['sk_path']) - 1) == "\"") { |
|
202 | + $data['sk_path'] = substr($data['sk_path'], 0, strlen($data['sk_path']) - 1); |
|
203 | 203 | } |
204 | 204 | if (is_dir($data['sk_path'])) { |
205 | 205 | if (is_writable($data['sk_path'])) { |
206 | 206 | // store all variables in SESSION |
207 | - foreach($data as $key=>$value) { |
|
207 | + foreach ($data as $key=>$value) { |
|
208 | 208 | $_SESSION[$key] = $value; |
209 | 209 | $tmp = mysqli_fetch_row(mysqli_query($dbTmp, "SELECT COUNT(*) FROM `_install` WHERE `key` = '".$key."'")); |
210 | 210 | if ($tmp[0] == 0 || empty($tmp[0])) { |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | |
226 | 226 | case "step_5": |
227 | 227 | //decrypt |
228 | - require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
228 | + require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
229 | 229 | $activity = Encryption\Crypt\aesctr::decrypt($_POST['activity'], "cpm", 128); |
230 | 230 | $task = Encryption\Crypt\aesctr::decrypt($_POST['task'], "cpm", 128); |
231 | 231 | $json = Encryption\Crypt\aesctr::decrypt($_POST['db'], "cpm", 128); |
@@ -341,97 +341,97 @@ discard block |
||
341 | 341 | array('admin', 'path_to_files_folder', $var['abspath'].'/files'), |
342 | 342 | array('admin', 'url_to_files_folder', $var['url_path'].'/files'), |
343 | 343 | array('admin', 'activate_expiration', '0'), |
344 | - array('admin','pw_life_duration','0'), |
|
345 | - array('admin','maintenance_mode','1'), |
|
346 | - array('admin','enable_sts','0'), |
|
347 | - array('admin','encryptClientServer','1'), |
|
348 | - array('admin','cpassman_version',$k['version']), |
|
349 | - array('admin','ldap_mode','0'), |
|
350 | - array('admin','ldap_type','0'), |
|
351 | - array('admin','ldap_suffix','0'), |
|
352 | - array('admin','ldap_domain_dn','0'), |
|
353 | - array('admin','ldap_domain_controler','0'), |
|
354 | - array('admin','ldap_user_attribute','0'), |
|
355 | - array('admin','ldap_ssl','0'), |
|
356 | - array('admin','ldap_tls','0'), |
|
357 | - array('admin','ldap_elusers','0'), |
|
358 | - array('admin','ldap_search_base','0'), |
|
359 | - array('admin','richtext','0'), |
|
360 | - array('admin','allow_print','0'), |
|
361 | - array('admin','roles_allowed_to_print','0'), |
|
362 | - array('admin','show_description','1'), |
|
363 | - array('admin','anyone_can_modify','0'), |
|
364 | - array('admin','anyone_can_modify_bydefault','0'), |
|
365 | - array('admin','nb_bad_authentication','0'), |
|
366 | - array('admin','utf8_enabled','1'), |
|
367 | - array('admin','restricted_to','0'), |
|
368 | - array('admin','restricted_to_roles','0'), |
|
369 | - array('admin','enable_send_email_on_user_login','0'), |
|
370 | - array('admin','enable_user_can_create_folders','0'), |
|
371 | - array('admin','insert_manual_entry_item_history','0'), |
|
372 | - array('admin','enable_kb','0'), |
|
373 | - array('admin','enable_email_notification_on_item_shown','0'), |
|
374 | - array('admin','enable_email_notification_on_user_pw_change','0'), |
|
375 | - array('admin','custom_logo',''), |
|
376 | - array('admin','custom_login_text',''), |
|
377 | - array('admin','default_language','english'), |
|
378 | - array('admin','send_stats', '0'), |
|
379 | - array('admin','send_statistics_items', 'stat_country;stat_users;stat_items;stat_items_shared;stat_folders;stat_folders_shared;stat_admins;stat_managers;stat_ro;stat_mysqlversion;stat_phpversion;stat_teampassversion;stat_languages;stat_kb;stat_suggestion;stat_customfields;stat_api;stat_2fa;stat_agses;stat_duo;stat_ldap;stat_syslog;stat_stricthttps;stat_fav;stat_pf;'), |
|
380 | - array('admin','send_stats_time', time()-2592000), |
|
381 | - array('admin','get_tp_info', '1'), |
|
382 | - array('admin','send_mail_on_user_login', '0'), |
|
344 | + array('admin', 'pw_life_duration', '0'), |
|
345 | + array('admin', 'maintenance_mode', '1'), |
|
346 | + array('admin', 'enable_sts', '0'), |
|
347 | + array('admin', 'encryptClientServer', '1'), |
|
348 | + array('admin', 'cpassman_version', $k['version']), |
|
349 | + array('admin', 'ldap_mode', '0'), |
|
350 | + array('admin', 'ldap_type', '0'), |
|
351 | + array('admin', 'ldap_suffix', '0'), |
|
352 | + array('admin', 'ldap_domain_dn', '0'), |
|
353 | + array('admin', 'ldap_domain_controler', '0'), |
|
354 | + array('admin', 'ldap_user_attribute', '0'), |
|
355 | + array('admin', 'ldap_ssl', '0'), |
|
356 | + array('admin', 'ldap_tls', '0'), |
|
357 | + array('admin', 'ldap_elusers', '0'), |
|
358 | + array('admin', 'ldap_search_base', '0'), |
|
359 | + array('admin', 'richtext', '0'), |
|
360 | + array('admin', 'allow_print', '0'), |
|
361 | + array('admin', 'roles_allowed_to_print', '0'), |
|
362 | + array('admin', 'show_description', '1'), |
|
363 | + array('admin', 'anyone_can_modify', '0'), |
|
364 | + array('admin', 'anyone_can_modify_bydefault', '0'), |
|
365 | + array('admin', 'nb_bad_authentication', '0'), |
|
366 | + array('admin', 'utf8_enabled', '1'), |
|
367 | + array('admin', 'restricted_to', '0'), |
|
368 | + array('admin', 'restricted_to_roles', '0'), |
|
369 | + array('admin', 'enable_send_email_on_user_login', '0'), |
|
370 | + array('admin', 'enable_user_can_create_folders', '0'), |
|
371 | + array('admin', 'insert_manual_entry_item_history', '0'), |
|
372 | + array('admin', 'enable_kb', '0'), |
|
373 | + array('admin', 'enable_email_notification_on_item_shown', '0'), |
|
374 | + array('admin', 'enable_email_notification_on_user_pw_change', '0'), |
|
375 | + array('admin', 'custom_logo', ''), |
|
376 | + array('admin', 'custom_login_text', ''), |
|
377 | + array('admin', 'default_language', 'english'), |
|
378 | + array('admin', 'send_stats', '0'), |
|
379 | + array('admin', 'send_statistics_items', 'stat_country;stat_users;stat_items;stat_items_shared;stat_folders;stat_folders_shared;stat_admins;stat_managers;stat_ro;stat_mysqlversion;stat_phpversion;stat_teampassversion;stat_languages;stat_kb;stat_suggestion;stat_customfields;stat_api;stat_2fa;stat_agses;stat_duo;stat_ldap;stat_syslog;stat_stricthttps;stat_fav;stat_pf;'), |
|
380 | + array('admin', 'send_stats_time', time() - 2592000), |
|
381 | + array('admin', 'get_tp_info', '1'), |
|
382 | + array('admin', 'send_mail_on_user_login', '0'), |
|
383 | 383 | array('cron', 'sending_emails', '0'), |
384 | - array('admin','nb_items_by_query', 'auto'), |
|
385 | - array('admin','enable_delete_after_consultation', '0'), |
|
386 | - array('admin','enable_personal_saltkey_cookie', '0'), |
|
387 | - array('admin','personal_saltkey_cookie_duration', '31'), |
|
388 | - array('admin','email_smtp_server', ''), |
|
389 | - array('admin','email_smtp_auth', ''), |
|
390 | - array('admin','email_auth_username', ''), |
|
391 | - array('admin','email_auth_pwd', ''), |
|
392 | - array('admin','email_port', ''), |
|
393 | - array('admin','email_security',''), |
|
394 | - array('admin','email_server_url', ''), |
|
395 | - array('admin','email_from', ''), |
|
396 | - array('admin','email_from_name', ''), |
|
397 | - array('admin','pwd_maximum_length', '40'), |
|
398 | - array('admin','google_authentication', '0'), |
|
399 | - array('admin','delay_item_edition', '0'), |
|
400 | - array('admin','allow_import','0'), |
|
401 | - array('admin','proxy_ip',''), |
|
402 | - array('admin','proxy_port',''), |
|
403 | - array('admin','upload_maxfilesize','10mb'), |
|
404 | - array('admin','upload_docext','doc,docx,dotx,xls,xlsx,xltx,rtf,csv,txt,pdf,ppt,pptx,pot,dotx,xltx'), |
|
405 | - array('admin','upload_imagesext','jpg,jpeg,gif,png'), |
|
406 | - array('admin','upload_pkgext','7z,rar,tar,zip'), |
|
407 | - array('admin','upload_otherext','sql,xml'), |
|
408 | - array('admin','upload_imageresize_options','1'), |
|
409 | - array('admin','upload_imageresize_width','800'), |
|
410 | - array('admin','upload_imageresize_height','600'), |
|
411 | - array('admin','upload_imageresize_quality','90'), |
|
412 | - array('admin','use_md5_password_as_salt','0'), |
|
413 | - array('admin','ga_website_name','TeamPass for ChangeMe'), |
|
414 | - array('admin','api','0'), |
|
415 | - array('admin','subfolder_rights_as_parent','0'), |
|
416 | - array('admin','show_only_accessible_folders','0'), |
|
417 | - array('admin','enable_suggestion','0'), |
|
418 | - array('admin','otv_expiration_period','7'), |
|
419 | - array('admin','default_session_expiration_time','60'), |
|
420 | - array('admin','duo','0'), |
|
421 | - array('admin','enable_server_password_change','0'), |
|
422 | - array('admin','ldap_object_class','0'), |
|
423 | - array('admin','bck_script_path', $var['abspath']."/backups"), |
|
424 | - array('admin','bck_script_filename', 'bck_teampass'), |
|
425 | - array('admin','syslog_enable','0'), |
|
426 | - array('admin','syslog_host','localhost'), |
|
427 | - array('admin','syslog_port','514'), |
|
428 | - array('admin','manager_move_item','0'), |
|
429 | - array('admin','create_item_without_password','0'), |
|
430 | - array('admin','otv_is_enabled','0'), |
|
431 | - array('admin','agses_authentication_enabled','0'), |
|
432 | - array('admin','item_extra_fields','0'), |
|
433 | - array('admin','saltkey_ante_2127','none'), |
|
434 | - array('admin','migration_to_2127','done') |
|
384 | + array('admin', 'nb_items_by_query', 'auto'), |
|
385 | + array('admin', 'enable_delete_after_consultation', '0'), |
|
386 | + array('admin', 'enable_personal_saltkey_cookie', '0'), |
|
387 | + array('admin', 'personal_saltkey_cookie_duration', '31'), |
|
388 | + array('admin', 'email_smtp_server', ''), |
|
389 | + array('admin', 'email_smtp_auth', ''), |
|
390 | + array('admin', 'email_auth_username', ''), |
|
391 | + array('admin', 'email_auth_pwd', ''), |
|
392 | + array('admin', 'email_port', ''), |
|
393 | + array('admin', 'email_security', ''), |
|
394 | + array('admin', 'email_server_url', ''), |
|
395 | + array('admin', 'email_from', ''), |
|
396 | + array('admin', 'email_from_name', ''), |
|
397 | + array('admin', 'pwd_maximum_length', '40'), |
|
398 | + array('admin', 'google_authentication', '0'), |
|
399 | + array('admin', 'delay_item_edition', '0'), |
|
400 | + array('admin', 'allow_import', '0'), |
|
401 | + array('admin', 'proxy_ip', ''), |
|
402 | + array('admin', 'proxy_port', ''), |
|
403 | + array('admin', 'upload_maxfilesize', '10mb'), |
|
404 | + array('admin', 'upload_docext', 'doc,docx,dotx,xls,xlsx,xltx,rtf,csv,txt,pdf,ppt,pptx,pot,dotx,xltx'), |
|
405 | + array('admin', 'upload_imagesext', 'jpg,jpeg,gif,png'), |
|
406 | + array('admin', 'upload_pkgext', '7z,rar,tar,zip'), |
|
407 | + array('admin', 'upload_otherext', 'sql,xml'), |
|
408 | + array('admin', 'upload_imageresize_options', '1'), |
|
409 | + array('admin', 'upload_imageresize_width', '800'), |
|
410 | + array('admin', 'upload_imageresize_height', '600'), |
|
411 | + array('admin', 'upload_imageresize_quality', '90'), |
|
412 | + array('admin', 'use_md5_password_as_salt', '0'), |
|
413 | + array('admin', 'ga_website_name', 'TeamPass for ChangeMe'), |
|
414 | + array('admin', 'api', '0'), |
|
415 | + array('admin', 'subfolder_rights_as_parent', '0'), |
|
416 | + array('admin', 'show_only_accessible_folders', '0'), |
|
417 | + array('admin', 'enable_suggestion', '0'), |
|
418 | + array('admin', 'otv_expiration_period', '7'), |
|
419 | + array('admin', 'default_session_expiration_time', '60'), |
|
420 | + array('admin', 'duo', '0'), |
|
421 | + array('admin', 'enable_server_password_change', '0'), |
|
422 | + array('admin', 'ldap_object_class', '0'), |
|
423 | + array('admin', 'bck_script_path', $var['abspath']."/backups"), |
|
424 | + array('admin', 'bck_script_filename', 'bck_teampass'), |
|
425 | + array('admin', 'syslog_enable', '0'), |
|
426 | + array('admin', 'syslog_host', 'localhost'), |
|
427 | + array('admin', 'syslog_port', '514'), |
|
428 | + array('admin', 'manager_move_item', '0'), |
|
429 | + array('admin', 'create_item_without_password', '0'), |
|
430 | + array('admin', 'otv_is_enabled', '0'), |
|
431 | + array('admin', 'agses_authentication_enabled', '0'), |
|
432 | + array('admin', 'item_extra_fields', '0'), |
|
433 | + array('admin', 'saltkey_ante_2127', 'none'), |
|
434 | + array('admin', 'migration_to_2127', 'done') |
|
435 | 435 | ); |
436 | 436 | foreach ($aMiscVal as $elem) { |
437 | 437 | //Check if exists before inserting |
@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | (`type`, `intitule`, `valeur`) VALUES |
449 | 449 | ('".$elem[0]."', '".$elem[1]."', '". |
450 | 450 | str_replace("'", "", $elem[2])."');" |
451 | - ); // or die(mysqli_error($dbTmp)) |
|
451 | + ); // or die(mysqli_error($dbTmp)) |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | // append new setting in config file |
@@ -843,10 +843,10 @@ discard block |
||
843 | 843 | $tmp = mysqli_num_rows(mysqli_query($dbTmp, "SELECT * FROM `".$var['tbl_prefix']."users` WHERE login = 'admin'")); |
844 | 844 | if ($tmp === "0") { |
845 | 845 | $mysqli_result = mysqli_query($dbTmp, |
846 | - "INSERT INTO `".$var['tbl_prefix']."users` (`id`, `login`, `pw`, `admin`, `gestionnaire`, `personal_folder`, `groupes_visibles`, `email`, `encrypted_psk`) VALUES ('1', 'admin', '".bCrypt($var['admin_pwd'],'13' )."', '1', '0', '0', '', '', '')" |
|
846 | + "INSERT INTO `".$var['tbl_prefix']."users` (`id`, `login`, `pw`, `admin`, `gestionnaire`, `personal_folder`, `groupes_visibles`, `email`, `encrypted_psk`) VALUES ('1', 'admin', '".bCrypt($var['admin_pwd'], '13')."', '1', '0', '0', '', '', '')" |
|
847 | 847 | ); |
848 | 848 | } else { |
849 | - $mysqli_result = mysqli_query($dbTmp, "UPDATE `".$var['tbl_prefix']."users` SET `pw` = '".bCrypt($var['admin_pwd'],'13' )."' WHERE login = 'admin' AND id = '1'"); |
|
849 | + $mysqli_result = mysqli_query($dbTmp, "UPDATE `".$var['tbl_prefix']."users` SET `pw` = '".bCrypt($var['admin_pwd'], '13')."' WHERE login = 'admin' AND id = '1'"); |
|
850 | 850 | } |
851 | 851 | |
852 | 852 | // check that API doesn't exist |
@@ -878,13 +878,13 @@ discard block |
||
878 | 878 | |
879 | 879 | mysqli_close($dbTmp); |
880 | 880 | // Destroy session without writing to disk |
881 | - define('NODESTROY_SESSION','true'); |
|
881 | + define('NODESTROY_SESSION', 'true'); |
|
882 | 882 | session_destroy(); |
883 | 883 | break; |
884 | 884 | |
885 | 885 | case "step_6": |
886 | 886 | //decrypt |
887 | - require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
887 | + require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
888 | 888 | $activity = Encryption\Crypt\aesctr::decrypt($_POST['activity'], "cpm", 128); |
889 | 889 | $data_sent = Encryption\Crypt\aesctr::decrypt($_POST['data'], "cpm", 128); |
890 | 890 | $data_sent = json_decode($data_sent, true); |
@@ -995,9 +995,9 @@ discard block |
||
995 | 995 | if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') { |
996 | 996 | // Change directory permissions |
997 | 997 | $result = chmod_r($_SESSION['abspath'], 0770, 0740); |
998 | - if ($result ) |
|
998 | + if ($result) |
|
999 | 999 | $result = chmod_r($_SESSION['abspath'].'/files', 0770, 0770); |
1000 | - if ($result) |
|
1000 | + if ($result) |
|
1001 | 1001 | $result = chmod_r($_SESSION['abspath'].'/upload', 0770, 0770); |
1002 | 1002 | } |
1003 | 1003 | |
@@ -1039,8 +1039,8 @@ discard block |
||
1039 | 1039 | $events .= "The file $csrfp_file already exist. A copy has been created.<br />"; |
1040 | 1040 | } |
1041 | 1041 | } |
1042 | - unlink($csrfp_file); // delete existing csrfp.config file |
|
1043 | - copy($csrfp_file_sample, $csrfp_file); // make a copy of csrfp.config.sample file |
|
1042 | + unlink($csrfp_file); // delete existing csrfp.config file |
|
1043 | + copy($csrfp_file_sample, $csrfp_file); // make a copy of csrfp.config.sample file |
|
1044 | 1044 | $data = file_get_contents($csrfp_file); |
1045 | 1045 | $newdata = str_replace('"CSRFP_TOKEN" => ""', '"CSRFP_TOKEN" => "'.bin2hex(openssl_random_pseudo_bytes(25)).'"', $data); |
1046 | 1046 | $jsUrl = $data_sent['url_path'].'/includes/libraries/csrfp/js/csrfprotector.js'; |
@@ -1053,13 +1053,13 @@ discard block |
||
1053 | 1053 | |
1054 | 1054 | mysqli_close($dbTmp); |
1055 | 1055 | // Destroy session without writing to disk |
1056 | - define('NODESTROY_SESSION','true'); |
|
1056 | + define('NODESTROY_SESSION', 'true'); |
|
1057 | 1057 | session_destroy(); |
1058 | 1058 | break; |
1059 | 1059 | case "step_7": |
1060 | 1060 | |
1061 | 1061 | //decrypt |
1062 | - require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
1062 | + require_once 'libs/aesctr.php'; // AES Counter Mode implementation |
|
1063 | 1063 | $activity = Encryption\Crypt\aesctr::decrypt($_POST['activity'], "cpm", 128); |
1064 | 1064 | $task = Encryption\Crypt\aesctr::decrypt($_POST['task'], "cpm", 128); |
1065 | 1065 | // launch |
@@ -1068,7 +1068,7 @@ discard block |
||
1068 | 1068 | if ($activity == "file") { |
1069 | 1069 | if ($task == "deleteInstall") { |
1070 | 1070 | function delTree($dir) { |
1071 | - $files = array_diff(scandir($dir), array('.','..')); |
|
1071 | + $files = array_diff(scandir($dir), array('.', '..')); |
|
1072 | 1072 | |
1073 | 1073 | foreach ($files as $file) { |
1074 | 1074 | (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file"); |
@@ -1076,7 +1076,7 @@ discard block |
||
1076 | 1076 | return rmdir($dir); |
1077 | 1077 | } |
1078 | 1078 | |
1079 | - $result=true; |
|
1079 | + $result = true; |
|
1080 | 1080 | $errorMsg = "Cannot delete installation directory"; |
1081 | 1081 | if (file_exists($_SESSION['abspath'].'/install')) { |
1082 | 1082 | // set the permissions on the install directory and delete |
@@ -1102,7 +1102,7 @@ discard block |
||
1102 | 1102 | // |
1103 | 1103 | mysqli_close($dbTmp); |
1104 | 1104 | // Destroy session without writing to disk |
1105 | - define('NODESTROY_SESSION','true'); |
|
1105 | + define('NODESTROY_SESSION', 'true'); |
|
1106 | 1106 | session_destroy(); |
1107 | 1107 | break; |
1108 | 1108 | } |
@@ -23,14 +23,16 @@ discard block |
||
23 | 23 | $dp = opendir($dir); |
24 | 24 | $res = true; |
25 | 25 | while($file = readdir($dp)) { |
26 | - if (($file == ".") || ($file == "..")) |
|
27 | - continue; |
|
26 | + if (($file == ".") || ($file == "..")) { |
|
27 | + continue; |
|
28 | + } |
|
28 | 29 | |
29 | 30 | $fullPath = $dir."/".$file; |
30 | 31 | |
31 | 32 | if(is_dir($fullPath)) { |
32 | - if ($res = @chmod($fullPath, $dirPermissions)) |
|
33 | - $res = @chmod_r($fullPath, $dirPermissions, $filePermissions); |
|
33 | + if ($res = @chmod($fullPath, $dirPermissions)) { |
|
34 | + $res = @chmod_r($fullPath, $dirPermissions, $filePermissions); |
|
35 | + } |
|
34 | 36 | } else { |
35 | 37 | $res = chmod($fullPath, $filePermissions); |
36 | 38 | } |
@@ -40,8 +42,9 @@ discard block |
||
40 | 42 | } |
41 | 43 | } |
42 | 44 | closedir($dp); |
43 | - if (is_dir($dir) && $res) |
|
44 | - $res = @chmod($dir, $dirPermissions); |
|
45 | + if (is_dir($dir) && $res) { |
|
46 | + $res = @chmod($dir, $dirPermissions); |
|
47 | + } |
|
45 | 48 | |
46 | 49 | return $res; |
47 | 50 | } |
@@ -995,10 +998,12 @@ discard block |
||
995 | 998 | if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') { |
996 | 999 | // Change directory permissions |
997 | 1000 | $result = chmod_r($_SESSION['abspath'], 0770, 0740); |
998 | - if ($result ) |
|
999 | - $result = chmod_r($_SESSION['abspath'].'/files', 0770, 0770); |
|
1000 | - if ($result) |
|
1001 | - $result = chmod_r($_SESSION['abspath'].'/upload', 0770, 0770); |
|
1001 | + if ($result ) { |
|
1002 | + $result = chmod_r($_SESSION['abspath'].'/files', 0770, 0770); |
|
1003 | + } |
|
1004 | + if ($result) { |
|
1005 | + $result = chmod_r($_SESSION['abspath'].'/upload', 0770, 0770); |
|
1006 | + } |
|
1002 | 1007 | } |
1003 | 1008 | |
1004 | 1009 | if ($result === false) { |
@@ -40,6 +40,10 @@ discard block |
||
40 | 40 | return $output; |
41 | 41 | } |
42 | 42 | |
43 | + /** |
|
44 | + * @param integer $rnd |
|
45 | + * @param integer $Nb |
|
46 | + */ |
|
43 | 47 | private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
44 | 48 | for ($r=0; $r<4; $r++) { |
45 | 49 | for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
@@ -48,6 +52,9 @@ discard block |
||
48 | 52 | return $state; |
49 | 53 | } |
50 | 54 | |
55 | + /** |
|
56 | + * @param integer $Nb |
|
57 | + */ |
|
51 | 58 | private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
52 | 59 | for ($r=0; $r<4; $r++) { |
53 | 60 | for ($c=0; $c<$Nb; $c++) $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
@@ -56,6 +63,9 @@ discard block |
||
56 | 63 | return $s; |
57 | 64 | } |
58 | 65 | |
66 | + /** |
|
67 | + * @param integer $Nb |
|
68 | + */ |
|
59 | 69 | private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
60 | 70 | $t = array(4); |
61 | 71 | for ($r=1; $r<4; $r++) { |
@@ -65,6 +75,9 @@ discard block |
||
65 | 75 | return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
66 | 76 | } |
67 | 77 | |
78 | + /** |
|
79 | + * @param integer $Nb |
|
80 | + */ |
|
68 | 81 | private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
69 | 82 | for ($c=0; $c<4; $c++) { |
70 | 83 | $a = array(4); // 'a' is a copy of the current column from 's' |
@@ -6,15 +6,15 @@ discard block |
||
6 | 6 | |
7 | 7 | class Aes |
8 | 8 | { |
9 | - /** |
|
10 | - * AES Cipher function: encrypt 'input' with Rijndael algorithm |
|
11 | - * |
|
12 | - * @param input message as byte-array (16 bytes) |
|
13 | - * @param w key schedule as 2D byte-array (Nr+1 x Nb bytes) - |
|
14 | - * generated from the cipher key by keyExpansion() |
|
15 | - * @return ciphertext as byte-array (16 bytes) |
|
16 | - */ |
|
17 | - public static function cipher($input, $w) { // main cipher function [é5.1] |
|
9 | + /** |
|
10 | + * AES Cipher function: encrypt 'input' with Rijndael algorithm |
|
11 | + * |
|
12 | + * @param input message as byte-array (16 bytes) |
|
13 | + * @param w key schedule as 2D byte-array (Nr+1 x Nb bytes) - |
|
14 | + * generated from the cipher key by keyExpansion() |
|
15 | + * @return ciphertext as byte-array (16 bytes) |
|
16 | + */ |
|
17 | + public static function cipher($input, $w) { // main cipher function [é5.1] |
|
18 | 18 | $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
19 | 19 | $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
20 | 20 | |
@@ -24,10 +24,10 @@ discard block |
||
24 | 24 | $state = self::addRoundKey($state, $w, 0, $Nb); |
25 | 25 | |
26 | 26 | for ($round=1; $round<$Nr; $round++) { // apply Nr rounds |
27 | - $state = self::subBytes($state, $Nb); |
|
28 | - $state = self::shiftRows($state, $Nb); |
|
29 | - $state = self::mixColumns($state, $Nb); |
|
30 | - $state = self::addRoundKey($state, $w, $round, $Nb); |
|
27 | + $state = self::subBytes($state, $Nb); |
|
28 | + $state = self::shiftRows($state, $Nb); |
|
29 | + $state = self::mixColumns($state, $Nb); |
|
30 | + $state = self::addRoundKey($state, $w, $round, $Nb); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | $state = self::subBytes($state, $Nb); |
@@ -38,59 +38,59 @@ discard block |
||
38 | 38 | for ($i=0; $i<4*$Nb; $i++) $output[$i] = $state[$i%4][floor($i/4)]; |
39 | 39 | |
40 | 40 | return $output; |
41 | - } |
|
41 | + } |
|
42 | 42 | |
43 | - private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
|
43 | + private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
|
44 | 44 | for ($r=0; $r<4; $r++) { |
45 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
45 | + for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | return $state; |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
|
51 | + private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
|
52 | 52 | for ($r=0; $r<4; $r++) { |
53 | - for ($c=0; $c<$Nb; $c++) $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
53 | + for ($c=0; $c<$Nb; $c++) $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | return $s; |
57 | - } |
|
57 | + } |
|
58 | 58 | |
59 | - private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
|
59 | + private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
|
60 | 60 | $t = array(4); |
61 | 61 | for ($r=1; $r<4; $r++) { |
62 | - for ($c=0; $c<4; $c++) $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy |
|
63 | - for ($c=0; $c<4; $c++) $s[$r][$c] = $t[$c]; // and copy back |
|
62 | + for ($c=0; $c<4; $c++) $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy |
|
63 | + for ($c=0; $c<4; $c++) $s[$r][$c] = $t[$c]; // and copy back |
|
64 | 64 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
65 | 65 | return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
66 | - } |
|
66 | + } |
|
67 | 67 | |
68 | - private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
|
68 | + private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
|
69 | 69 | for ($c=0; $c<4; $c++) { |
70 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
71 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
72 | - for ($i=0; $i<4; $i++) { |
|
70 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
71 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
72 | + for ($i=0; $i<4; $i++) { |
|
73 | 73 | $a[$i] = $s[$i][$c]; |
74 | 74 | $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
75 | - } |
|
76 | - // a[n] ^ b[n] is aé{03} in GF(2^8) |
|
77 | - $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
|
78 | - $s[1][$c] = $a[0] ^ $b[1] ^ $a[2] ^ $b[2] ^ $a[3]; // a0 * 2*a1 + 3*a2 + a3 |
|
79 | - $s[2][$c] = $a[0] ^ $a[1] ^ $b[2] ^ $a[3] ^ $b[3]; // a0 + a1 + 2*a2 + 3*a3 |
|
80 | - $s[3][$c] = $a[0] ^ $b[0] ^ $a[1] ^ $a[2] ^ $b[3]; // 3*a0 + a1 + a2 + 2*a3 |
|
75 | + } |
|
76 | + // a[n] ^ b[n] is aé{03} in GF(2^8) |
|
77 | + $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
|
78 | + $s[1][$c] = $a[0] ^ $b[1] ^ $a[2] ^ $b[2] ^ $a[3]; // a0 * 2*a1 + 3*a2 + a3 |
|
79 | + $s[2][$c] = $a[0] ^ $a[1] ^ $b[2] ^ $a[3] ^ $b[3]; // a0 + a1 + 2*a2 + 3*a3 |
|
80 | + $s[3][$c] = $a[0] ^ $b[0] ^ $a[1] ^ $a[2] ^ $b[3]; // 3*a0 + a1 + a2 + 2*a3 |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | return $s; |
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Key expansion for Rijndael cipher(): performs key expansion on cipher key |
|
88 | - * to generate a key schedule |
|
89 | - * |
|
90 | - * @param key cipher key byte-array (16 bytes) |
|
91 | - * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
|
92 | - */ |
|
93 | - public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Key expansion for Rijndael cipher(): performs key expansion on cipher key |
|
88 | + * to generate a key schedule |
|
89 | + * |
|
90 | + * @param key cipher key byte-array (16 bytes) |
|
91 | + * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
|
92 | + */ |
|
93 | + public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
|
94 | 94 | $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
95 | 95 | $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
96 | 96 | $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
@@ -99,41 +99,41 @@ discard block |
||
99 | 99 | $temp = array(); |
100 | 100 | |
101 | 101 | for ($i=0; $i<$Nk; $i++) { |
102 | - $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
103 | - $w[$i] = $r; |
|
102 | + $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
103 | + $w[$i] = $r; |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
107 | - $w[$i] = array(); |
|
108 | - for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t]; |
|
109 | - if ($i % $Nk == 0) { |
|
107 | + $w[$i] = array(); |
|
108 | + for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t]; |
|
109 | + if ($i % $Nk == 0) { |
|
110 | 110 | $temp = self::subWord(self::rotWord($temp)); |
111 | 111 | for ($t=0; $t<4; $t++) $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
112 | - } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
112 | + } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
113 | 113 | $temp = self::subWord($temp); |
114 | - } |
|
115 | - for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
114 | + } |
|
115 | + for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | return $w; |
119 | - } |
|
119 | + } |
|
120 | 120 | |
121 | - private static function subWord($w) { // apply SBox to 4-byte word w |
|
121 | + private static function subWord($w) { // apply SBox to 4-byte word w |
|
122 | 122 | for ($i=0; $i<4; $i++) $w[$i] = self::$sBox[$w[$i]]; |
123 | 123 | |
124 | 124 | return $w; |
125 | - } |
|
125 | + } |
|
126 | 126 | |
127 | - private static function rotWord($w) { // rotate 4-byte word w left by one byte |
|
127 | + private static function rotWord($w) { // rotate 4-byte word w left by one byte |
|
128 | 128 | $tmp = $w[0]; |
129 | 129 | for ($i=0; $i<3; $i++) $w[$i] = $w[$i+1]; |
130 | 130 | $w[3] = $tmp; |
131 | 131 | |
132 | 132 | return $w; |
133 | - } |
|
133 | + } |
|
134 | 134 | |
135 | - // sBox is pre-computed multiplicative inverse in GF(2^8) used in subBytes and keyExpansion [é5.1.1] |
|
136 | - private static $sBox = array( |
|
135 | + // sBox is pre-computed multiplicative inverse in GF(2^8) used in subBytes and keyExpansion [é5.1.1] |
|
136 | + private static $sBox = array( |
|
137 | 137 | 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, |
138 | 138 | 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, |
139 | 139 | 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, |
@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, |
152 | 152 | 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16); |
153 | 153 | |
154 | - // rCon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [é5.2] |
|
155 | - private static $rCon = array( |
|
154 | + // rCon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [é5.2] |
|
155 | + private static $rCon = array( |
|
156 | 156 | array(0x00, 0x00, 0x00, 0x00), |
157 | 157 | array(0x01, 0x00, 0x00, 0x00), |
158 | 158 | array(0x02, 0x00, 0x00, 0x00), |
@@ -15,15 +15,15 @@ discard block |
||
15 | 15 | * @return ciphertext as byte-array (16 bytes) |
16 | 16 | */ |
17 | 17 | public static function cipher($input, $w) { // main cipher function [é5.1] |
18 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
19 | - $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
18 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
19 | + $Nr = count($w) / $Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
20 | 20 | |
21 | - $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
22 | - for ($i=0; $i<4*$Nb; $i++) $state[$i%4][floor($i/4)] = $input[$i]; |
|
21 | + $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
22 | + for ($i = 0; $i < 4 * $Nb; $i++) $state[$i % 4][floor($i / 4)] = $input[$i]; |
|
23 | 23 | |
24 | 24 | $state = self::addRoundKey($state, $w, 0, $Nb); |
25 | 25 | |
26 | - for ($round=1; $round<$Nr; $round++) { // apply Nr rounds |
|
26 | + for ($round = 1; $round < $Nr; $round++) { // apply Nr rounds |
|
27 | 27 | $state = self::subBytes($state, $Nb); |
28 | 28 | $state = self::shiftRows($state, $Nb); |
29 | 29 | $state = self::mixColumns($state, $Nb); |
@@ -34,23 +34,23 @@ discard block |
||
34 | 34 | $state = self::shiftRows($state, $Nb); |
35 | 35 | $state = self::addRoundKey($state, $w, $Nr, $Nb); |
36 | 36 | |
37 | - $output = array(4*$Nb); // convert state to 1-d array before returning [é3.4] |
|
38 | - for ($i=0; $i<4*$Nb; $i++) $output[$i] = $state[$i%4][floor($i/4)]; |
|
37 | + $output = array(4 * $Nb); // convert state to 1-d array before returning [é3.4] |
|
38 | + for ($i = 0; $i < 4 * $Nb; $i++) $output[$i] = $state[$i % 4][floor($i / 4)]; |
|
39 | 39 | |
40 | 40 | return $output; |
41 | 41 | } |
42 | 42 | |
43 | 43 | private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
44 | - for ($r=0; $r<4; $r++) { |
|
45 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
44 | + for ($r = 0; $r < 4; $r++) { |
|
45 | + for ($c = 0; $c < $Nb; $c++) $state[$r][$c] ^= $w[$rnd * 4 + $c][$r]; |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | return $state; |
49 | 49 | } |
50 | 50 | |
51 | 51 | private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
52 | - for ($r=0; $r<4; $r++) { |
|
53 | - for ($c=0; $c<$Nb; $c++) $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
52 | + for ($r = 0; $r < 4; $r++) { |
|
53 | + for ($c = 0; $c < $Nb; $c++) $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | return $s; |
@@ -58,20 +58,20 @@ discard block |
||
58 | 58 | |
59 | 59 | private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
60 | 60 | $t = array(4); |
61 | - for ($r=1; $r<4; $r++) { |
|
62 | - for ($c=0; $c<4; $c++) $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy |
|
63 | - for ($c=0; $c<4; $c++) $s[$r][$c] = $t[$c]; // and copy back |
|
61 | + for ($r = 1; $r < 4; $r++) { |
|
62 | + for ($c = 0; $c < 4; $c++) $t[$c] = $s[$r][($c + $r) % $Nb]; // shift into temp copy |
|
63 | + for ($c = 0; $c < 4; $c++) $s[$r][$c] = $t[$c]; // and copy back |
|
64 | 64 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
65 | - return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
65 | + return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
69 | - for ($c=0; $c<4; $c++) { |
|
70 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
71 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
72 | - for ($i=0; $i<4; $i++) { |
|
69 | + for ($c = 0; $c < 4; $c++) { |
|
70 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
71 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
72 | + for ($i = 0; $i < 4; $i++) { |
|
73 | 73 | $a[$i] = $s[$i][$c]; |
74 | - $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
|
74 | + $b[$i] = $s[$i][$c] & 0x80 ? $s[$i][$c] << 1 ^ 0x011b : $s[$i][$c] << 1; |
|
75 | 75 | } |
76 | 76 | // a[n] ^ b[n] is aé{03} in GF(2^8) |
77 | 77 | $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
@@ -91,42 +91,42 @@ discard block |
||
91 | 91 | * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
92 | 92 | */ |
93 | 93 | public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
94 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
95 | - $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
96 | - $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
94 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
95 | + $Nk = count($key) / 4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
96 | + $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
97 | 97 | |
98 | 98 | $w = array(); |
99 | 99 | $temp = array(); |
100 | 100 | |
101 | - for ($i=0; $i<$Nk; $i++) { |
|
102 | - $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
101 | + for ($i = 0; $i < $Nk; $i++) { |
|
102 | + $r = array($key[4 * $i], $key[4 * $i + 1], $key[4 * $i + 2], $key[4 * $i + 3]); |
|
103 | 103 | $w[$i] = $r; |
104 | 104 | } |
105 | 105 | |
106 | - for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
|
106 | + for ($i = $Nk; $i < ($Nb * ($Nr + 1)); $i++) { |
|
107 | 107 | $w[$i] = array(); |
108 | - for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t]; |
|
108 | + for ($t = 0; $t < 4; $t++) $temp[$t] = $w[$i - 1][$t]; |
|
109 | 109 | if ($i % $Nk == 0) { |
110 | 110 | $temp = self::subWord(self::rotWord($temp)); |
111 | - for ($t=0; $t<4; $t++) $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
112 | - } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
111 | + for ($t = 0; $t < 4; $t++) $temp[$t] ^= self::$rCon[$i / $Nk][$t]; |
|
112 | + } elseif ($Nk > 6 && $i % $Nk == 4) { |
|
113 | 113 | $temp = self::subWord($temp); |
114 | 114 | } |
115 | - for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
115 | + for ($t = 0; $t < 4; $t++) $w[$i][$t] = $w[$i - $Nk][$t] ^ $temp[$t]; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | return $w; |
119 | 119 | } |
120 | 120 | |
121 | 121 | private static function subWord($w) { // apply SBox to 4-byte word w |
122 | - for ($i=0; $i<4; $i++) $w[$i] = self::$sBox[$w[$i]]; |
|
122 | + for ($i = 0; $i < 4; $i++) $w[$i] = self::$sBox[$w[$i]]; |
|
123 | 123 | |
124 | 124 | return $w; |
125 | 125 | } |
126 | 126 | |
127 | 127 | private static function rotWord($w) { // rotate 4-byte word w left by one byte |
128 | 128 | $tmp = $w[0]; |
129 | - for ($i=0; $i<3; $i++) $w[$i] = $w[$i+1]; |
|
129 | + for ($i = 0; $i < 3; $i++) $w[$i] = $w[$i + 1]; |
|
130 | 130 | $w[3] = $tmp; |
131 | 131 | |
132 | 132 | return $w; |
@@ -134,22 +134,22 @@ discard block |
||
134 | 134 | |
135 | 135 | // sBox is pre-computed multiplicative inverse in GF(2^8) used in subBytes and keyExpansion [é5.1.1] |
136 | 136 | private static $sBox = array( |
137 | - 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, |
|
138 | - 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, |
|
139 | - 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, |
|
140 | - 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, |
|
141 | - 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, |
|
142 | - 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, |
|
143 | - 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, |
|
144 | - 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, |
|
145 | - 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, |
|
146 | - 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, |
|
147 | - 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, |
|
148 | - 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, |
|
149 | - 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, |
|
150 | - 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, |
|
151 | - 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, |
|
152 | - 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16); |
|
137 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
138 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
139 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
140 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
141 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
142 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
143 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
144 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
145 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
146 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
147 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
148 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
149 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
150 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
151 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
152 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); |
|
153 | 153 | |
154 | 154 | // rCon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [é5.2] |
155 | 155 | private static $rCon = array( |
@@ -19,7 +19,9 @@ discard block |
||
19 | 19 | $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
20 | 20 | |
21 | 21 | $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
22 | - for ($i=0; $i<4*$Nb; $i++) $state[$i%4][floor($i/4)] = $input[$i]; |
|
22 | + for ($i=0; $i<4*$Nb; $i++) { |
|
23 | + $state[$i%4][floor($i/4)] = $input[$i]; |
|
24 | + } |
|
23 | 25 | |
24 | 26 | $state = self::addRoundKey($state, $w, 0, $Nb); |
25 | 27 | |
@@ -35,14 +37,18 @@ discard block |
||
35 | 37 | $state = self::addRoundKey($state, $w, $Nr, $Nb); |
36 | 38 | |
37 | 39 | $output = array(4*$Nb); // convert state to 1-d array before returning [é3.4] |
38 | - for ($i=0; $i<4*$Nb; $i++) $output[$i] = $state[$i%4][floor($i/4)]; |
|
40 | + for ($i=0; $i<4*$Nb; $i++) { |
|
41 | + $output[$i] = $state[$i%4][floor($i/4)]; |
|
42 | + } |
|
39 | 43 | |
40 | 44 | return $output; |
41 | 45 | } |
42 | 46 | |
43 | 47 | private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
44 | 48 | for ($r=0; $r<4; $r++) { |
45 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
49 | + for ($c=0; $c<$Nb; $c++) { |
|
50 | + $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
51 | + } |
|
46 | 52 | } |
47 | 53 | |
48 | 54 | return $state; |
@@ -50,7 +56,9 @@ discard block |
||
50 | 56 | |
51 | 57 | private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
52 | 58 | for ($r=0; $r<4; $r++) { |
53 | - for ($c=0; $c<$Nb; $c++) $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
59 | + for ($c=0; $c<$Nb; $c++) { |
|
60 | + $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
61 | + } |
|
54 | 62 | } |
55 | 63 | |
56 | 64 | return $s; |
@@ -59,8 +67,14 @@ discard block |
||
59 | 67 | private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
60 | 68 | $t = array(4); |
61 | 69 | for ($r=1; $r<4; $r++) { |
62 | - for ($c=0; $c<4; $c++) $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy |
|
63 | - for ($c=0; $c<4; $c++) $s[$r][$c] = $t[$c]; // and copy back |
|
70 | + for ($c=0; $c<4; $c++) { |
|
71 | + $t[$c] = $s[$r][($c+$r)%$Nb]; |
|
72 | + } |
|
73 | + // shift into temp copy |
|
74 | + for ($c=0; $c<4; $c++) { |
|
75 | + $s[$r][$c] = $t[$c]; |
|
76 | + } |
|
77 | + // and copy back |
|
64 | 78 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
65 | 79 | return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
66 | 80 | } |
@@ -105,28 +119,38 @@ discard block |
||
105 | 119 | |
106 | 120 | for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
107 | 121 | $w[$i] = array(); |
108 | - for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t]; |
|
122 | + for ($t=0; $t<4; $t++) { |
|
123 | + $temp[$t] = $w[$i-1][$t]; |
|
124 | + } |
|
109 | 125 | if ($i % $Nk == 0) { |
110 | 126 | $temp = self::subWord(self::rotWord($temp)); |
111 | - for ($t=0; $t<4; $t++) $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
127 | + for ($t=0; $t<4; $t++) { |
|
128 | + $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
129 | + } |
|
112 | 130 | } elseif ($Nk > 6 && $i%$Nk == 4) { |
113 | 131 | $temp = self::subWord($temp); |
114 | 132 | } |
115 | - for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
133 | + for ($t=0; $t<4; $t++) { |
|
134 | + $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
135 | + } |
|
116 | 136 | } |
117 | 137 | |
118 | 138 | return $w; |
119 | 139 | } |
120 | 140 | |
121 | 141 | private static function subWord($w) { // apply SBox to 4-byte word w |
122 | - for ($i=0; $i<4; $i++) $w[$i] = self::$sBox[$w[$i]]; |
|
142 | + for ($i=0; $i<4; $i++) { |
|
143 | + $w[$i] = self::$sBox[$w[$i]]; |
|
144 | + } |
|
123 | 145 | |
124 | 146 | return $w; |
125 | 147 | } |
126 | 148 | |
127 | 149 | private static function rotWord($w) { // rotate 4-byte word w left by one byte |
128 | 150 | $tmp = $w[0]; |
129 | - for ($i=0; $i<3; $i++) $w[$i] = $w[$i+1]; |
|
151 | + for ($i=0; $i<3; $i++) { |
|
152 | + $w[$i] = $w[$i+1]; |
|
153 | + } |
|
130 | 154 | $w[3] = $tmp; |
131 | 155 | |
132 | 156 | return $w; |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * @param plaintext source text to be encrypted |
17 | 17 | * @param password the password to use to generate a key |
18 | 18 | * @param nBits number of bits to be used in the key (128, 192, or 256) |
19 | - * @return encrypted text |
|
19 | + * @return string text |
|
20 | 20 | */ |
21 | 21 | public static function encrypt($plaintext, $password, $nBits) |
22 | 22 | { |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | * @param ciphertext source text to be decrypted |
85 | 85 | * @param password the password to use to generate a key |
86 | 86 | * @param nBits number of bits to be used in the key (128, 192, or 256) |
87 | - * @return decrypted text |
|
87 | + * @return string text |
|
88 | 88 | */ |
89 | 89 | public static function decrypt($ciphertext, $password, $nBits) |
90 | 90 | { |
@@ -7,19 +7,19 @@ discard block |
||
7 | 7 | |
8 | 8 | class AesCtr extends Aes |
9 | 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 encrypted text |
|
20 | - */ |
|
21 | - public static function encrypt($plaintext, $password, $nBits) |
|
22 | - { |
|
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 encrypted text |
|
20 | + */ |
|
21 | + public static function encrypt($plaintext, $password, $nBits) |
|
22 | + { |
|
23 | 23 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
24 | 24 | if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // standard allows 128/192/256 bit keys |
25 | 25 | // note PHP (5) gives us plaintext and password in UTF8 encoding! |
@@ -53,22 +53,22 @@ discard block |
||
53 | 53 | $ciphertxt = array(); // ciphertext as array of strings |
54 | 54 | |
55 | 55 | for ($b=0; $b<$blockCount; $b++) { |
56 | - // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
|
57 | - // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
|
58 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
59 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8); |
|
56 | + // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
|
57 | + // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
|
58 | + for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
59 | + for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8); |
|
60 | 60 | |
61 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
61 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
62 | 62 | |
63 | - // block size is reduced on final block |
|
64 | - $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1; |
|
65 | - $cipherByte = array(); |
|
63 | + // block size is reduced on final block |
|
64 | + $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1; |
|
65 | + $cipherByte = array(); |
|
66 | 66 | |
67 | - for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
67 | + for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
68 | 68 | $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$i, 1)); |
69 | 69 | $cipherByte[$i] = chr($cipherByte[$i]); |
70 | - } |
|
71 | - $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
70 | + } |
|
71 | + $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | // implode is more efficient than repeated string concatenation |
@@ -76,18 +76,18 @@ discard block |
||
76 | 76 | $ciphertext = base64_encode($ciphertext); |
77 | 77 | |
78 | 78 | return $ciphertext; |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Decrypt a text encrypted by AES in counter mode of operation |
|
83 | - * |
|
84 | - * @param ciphertext source text to be decrypted |
|
85 | - * @param password the password to use to generate a key |
|
86 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
87 | - * @return decrypted text |
|
88 | - */ |
|
89 | - public static function decrypt($ciphertext, $password, $nBits) |
|
90 | - { |
|
81 | + /** |
|
82 | + * Decrypt a text encrypted by AES in counter mode of operation |
|
83 | + * |
|
84 | + * @param ciphertext source text to be decrypted |
|
85 | + * @param password the password to use to generate a key |
|
86 | + * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
87 | + * @return decrypted text |
|
88 | + */ |
|
89 | + public static function decrypt($ciphertext, $password, $nBits) |
|
90 | + { |
|
91 | 91 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
92 | 92 | if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // standard allows 128/192/256 bit keys |
93 | 93 | $ciphertext = base64_decode($ciphertext); |
@@ -117,47 +117,47 @@ discard block |
||
117 | 117 | $plaintxt = array(); |
118 | 118 | |
119 | 119 | for ($b=0; $b<$nBlocks; $b++) { |
120 | - // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
|
121 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
122 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
120 | + // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
|
121 | + for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
122 | + for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
123 | 123 | |
124 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
124 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
125 | 125 | |
126 | - $plaintxtByte = array(); |
|
127 | - for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
126 | + $plaintxtByte = array(); |
|
127 | + for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
128 | 128 | // -- xor plaintext with ciphered counter byte-by-byte -- |
129 | 129 | $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b],$i,1)); |
130 | 130 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
131 | 131 | |
132 | - } |
|
133 | - $plaintxt[$b] = implode('', $plaintxtByte); |
|
132 | + } |
|
133 | + $plaintxt[$b] = implode('', $plaintxtByte); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | // join array of blocks into single plaintext string |
137 | 137 | $plaintext = implode('',$plaintxt); |
138 | 138 | |
139 | 139 | return $plaintext; |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | - /* |
|
142 | + /* |
|
143 | 143 | * Unsigned right shift function, since PHP has neither >>> operator nor unsigned ints |
144 | 144 | * |
145 | 145 | * @param a number to be shifted (32-bit integer) |
146 | 146 | * @param b number of bits to shift a to the right (0..31) |
147 | 147 | * @return a right-shifted and zero-filled by b bits |
148 | 148 | */ |
149 | - private static function urs($a, $b) |
|
150 | - { |
|
149 | + private static function urs($a, $b) |
|
150 | + { |
|
151 | 151 | $a &= 0xffffffff; $b &= 0x1f; // (bounds check) |
152 | 152 | if ($a&0x80000000 && $b>0) { // if left-most bit set |
153 | - $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
154 | - $a = $a >> ($b-1); // remaining right-shifts |
|
153 | + $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
154 | + $a = $a >> ($b-1); // remaining right-shifts |
|
155 | 155 | } else { // otherwise |
156 | - $a = ($a>>$b); // use normal right-shift |
|
156 | + $a = ($a>>$b); // use normal right-shift |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | return $a; |
160 | - } |
|
160 | + } |
|
161 | 161 | |
162 | 162 | } |
163 | 163 | /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ |
@@ -20,55 +20,55 @@ 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)) return ''; // standard allows 128/192/256 bit keys |
|
23 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
24 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) return ''; // standard allows 128/192/256 bit keys |
|
25 | 25 | // note PHP (5) gives us plaintext and password in UTF8 encoding! |
26 | 26 | |
27 | 27 | // use AES itself to encrypt password to get cipher key (using plain password as source for |
28 | 28 | // key expansion) - gives us well encrypted key |
29 | - $nBytes = $nBits/8; // no bytes in key |
|
29 | + $nBytes = $nBits / 8; // no bytes in key |
|
30 | 30 | $pwBytes = array(); |
31 | - for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
31 | + for ($i = 0; $i < $nBytes; $i++) $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
|
32 | 32 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
33 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
33 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
34 | 34 | |
35 | 35 | // initialise counter block (NIST SP800-38A §B.2): millisecond time-stamp for nonce in |
36 | 36 | // 1st 8 bytes, block counter in 2nd 8 bytes |
37 | 37 | $counterBlock = array(); |
38 | - $nonce = floor(microtime(true)*1000); // timestamp: milliseconds since 1-Jan-1970 |
|
39 | - $nonceSec = floor($nonce/1000); |
|
40 | - $nonceMs = $nonce%1000; |
|
38 | + $nonce = floor(microtime(true) * 1000); // timestamp: milliseconds since 1-Jan-1970 |
|
39 | + $nonceSec = floor($nonce / 1000); |
|
40 | + $nonceMs = $nonce % 1000; |
|
41 | 41 | // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes |
42 | - for ($i=0; $i<4; $i++) $counterBlock[$i] = self::urs($nonceSec, $i*8) & 0xff; |
|
43 | - for ($i=0; $i<4; $i++) $counterBlock[$i+4] = $nonceMs & 0xff; |
|
42 | + for ($i = 0; $i < 4; $i++) $counterBlock[$i] = self::urs($nonceSec, $i * 8) & 0xff; |
|
43 | + for ($i = 0; $i < 4; $i++) $counterBlock[$i + 4] = $nonceMs & 0xff; |
|
44 | 44 | // and convert it to a string to go on the front of the ciphertext |
45 | 45 | $ctrTxt = ''; |
46 | - for ($i=0; $i<8; $i++) $ctrTxt .= chr($counterBlock[$i]); |
|
46 | + for ($i = 0; $i < 8; $i++) $ctrTxt .= chr($counterBlock[$i]); |
|
47 | 47 | |
48 | 48 | // generate key schedule - an expansion of the key into distinct Key Rounds for each round |
49 | 49 | $keySchedule = Aes::keyExpansion($key); |
50 | 50 | //print_r($keySchedule); |
51 | 51 | |
52 | - $blockCount = ceil(strlen($plaintext)/$blockSize); |
|
53 | - $ciphertxt = array(); // ciphertext as array of strings |
|
52 | + $blockCount = ceil(strlen($plaintext) / $blockSize); |
|
53 | + $ciphertxt = array(); // ciphertext as array of strings |
|
54 | 54 | |
55 | - for ($b=0; $b<$blockCount; $b++) { |
|
55 | + for ($b = 0; $b < $blockCount; $b++) { |
|
56 | 56 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
57 | 57 | // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
58 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
59 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8); |
|
58 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
59 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); |
|
60 | 60 | |
61 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
61 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
62 | 62 | |
63 | 63 | // block size is reduced on final block |
64 | - $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1; |
|
64 | + $blockLength = $b < $blockCount - 1 ? $blockSize : (strlen($plaintext) - 1) % $blockSize + 1; |
|
65 | 65 | $cipherByte = array(); |
66 | 66 | |
67 | - for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
68 | - $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$i, 1)); |
|
67 | + for ($i = 0; $i < $blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
68 | + $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b * $blockSize + $i, 1)); |
|
69 | 69 | $cipherByte[$i] = chr($cipherByte[$i]); |
70 | 70 | } |
71 | - $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
71 | + $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | // implode is more efficient than repeated string concatenation |
@@ -88,45 +88,45 @@ discard block |
||
88 | 88 | */ |
89 | 89 | public static function decrypt($ciphertext, $password, $nBits) |
90 | 90 | { |
91 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
92 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // standard allows 128/192/256 bit keys |
|
91 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
92 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) return ''; // standard allows 128/192/256 bit keys |
|
93 | 93 | $ciphertext = base64_decode($ciphertext); |
94 | 94 | |
95 | 95 | // use AES to encrypt password (mirroring encrypt routine) |
96 | - $nBytes = $nBits/8; // no bytes in key |
|
96 | + $nBytes = $nBits / 8; // no bytes in key |
|
97 | 97 | $pwBytes = array(); |
98 | - for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
98 | + for ($i = 0; $i < $nBytes; $i++) $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
|
99 | 99 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
100 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
100 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
101 | 101 | |
102 | 102 | // recover nonce from 1st element of ciphertext |
103 | 103 | $counterBlock = array(); |
104 | 104 | $ctrTxt = substr($ciphertext, 0, 8); |
105 | - for ($i=0; $i<8; $i++) $counterBlock[$i] = ord(substr($ctrTxt,$i,1)); |
|
105 | + for ($i = 0; $i < 8; $i++) $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); |
|
106 | 106 | |
107 | 107 | // generate key schedule |
108 | 108 | $keySchedule = Aes::keyExpansion($key); |
109 | 109 | |
110 | 110 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
111 | - $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); |
|
111 | + $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); |
|
112 | 112 | $ct = array(); |
113 | - for ($b=0; $b<$nBlocks; $b++) $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
114 | - $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
113 | + for ($b = 0; $b < $nBlocks; $b++) $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); |
|
114 | + $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
115 | 115 | |
116 | 116 | // plaintext will get generated block-by-block into array of block-length strings |
117 | 117 | $plaintxt = array(); |
118 | 118 | |
119 | - for ($b=0; $b<$nBlocks; $b++) { |
|
119 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
120 | 120 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
121 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
122 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
121 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
122 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; |
|
123 | 123 | |
124 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
124 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
125 | 125 | |
126 | 126 | $plaintxtByte = array(); |
127 | - for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
127 | + for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { |
|
128 | 128 | // -- xor plaintext with ciphered counter byte-by-byte -- |
129 | - $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b],$i,1)); |
|
129 | + $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); |
|
130 | 130 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
131 | 131 | |
132 | 132 | } |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | } |
135 | 135 | |
136 | 136 | // join array of blocks into single plaintext string |
137 | - $plaintext = implode('',$plaintxt); |
|
137 | + $plaintext = implode('', $plaintxt); |
|
138 | 138 | |
139 | 139 | return $plaintext; |
140 | 140 | } |
@@ -148,12 +148,12 @@ discard block |
||
148 | 148 | */ |
149 | 149 | private static function urs($a, $b) |
150 | 150 | { |
151 | - $a &= 0xffffffff; $b &= 0x1f; // (bounds check) |
|
152 | - if ($a&0x80000000 && $b>0) { // if left-most bit set |
|
153 | - $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
154 | - $a = $a >> ($b-1); // remaining right-shifts |
|
151 | + $a &= 0xffffffff; $b &= 0x1f; // (bounds check) |
|
152 | + if ($a & 0x80000000 && $b > 0) { // if left-most bit set |
|
153 | + $a = ($a >> 1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
154 | + $a = $a >> ($b - 1); // remaining right-shifts |
|
155 | 155 | } else { // otherwise |
156 | - $a = ($a>>$b); // use normal right-shift |
|
156 | + $a = ($a >> $b); // use normal right-shift |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | return $a; |
@@ -21,14 +21,19 @@ discard block |
||
21 | 21 | public static function encrypt($plaintext, $password, $nBits) |
22 | 22 | { |
23 | 23 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
24 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // standard allows 128/192/256 bit keys |
|
24 | + if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
25 | + return ''; |
|
26 | + } |
|
27 | + // standard allows 128/192/256 bit keys |
|
25 | 28 | // note PHP (5) gives us plaintext and password in UTF8 encoding! |
26 | 29 | |
27 | 30 | // use AES itself to encrypt password to get cipher key (using plain password as source for |
28 | 31 | // key expansion) - gives us well encrypted key |
29 | 32 | $nBytes = $nBits/8; // no bytes in key |
30 | 33 | $pwBytes = array(); |
31 | - for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
34 | + for ($i=0; $i<$nBytes; $i++) { |
|
35 | + $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
36 | + } |
|
32 | 37 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
33 | 38 | $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
34 | 39 | |
@@ -39,11 +44,17 @@ discard block |
||
39 | 44 | $nonceSec = floor($nonce/1000); |
40 | 45 | $nonceMs = $nonce%1000; |
41 | 46 | // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes |
42 | - for ($i=0; $i<4; $i++) $counterBlock[$i] = self::urs($nonceSec, $i*8) & 0xff; |
|
43 | - for ($i=0; $i<4; $i++) $counterBlock[$i+4] = $nonceMs & 0xff; |
|
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 | + } |
|
44 | 53 | // and convert it to a string to go on the front of the ciphertext |
45 | 54 | $ctrTxt = ''; |
46 | - for ($i=0; $i<8; $i++) $ctrTxt .= chr($counterBlock[$i]); |
|
55 | + for ($i=0; $i<8; $i++) { |
|
56 | + $ctrTxt .= chr($counterBlock[$i]); |
|
57 | + } |
|
47 | 58 | |
48 | 59 | // generate key schedule - an expansion of the key into distinct Key Rounds for each round |
49 | 60 | $keySchedule = Aes::keyExpansion($key); |
@@ -55,8 +66,12 @@ discard block |
||
55 | 66 | for ($b=0; $b<$blockCount; $b++) { |
56 | 67 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
57 | 68 | // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
58 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
59 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8); |
|
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 | + } |
|
60 | 75 | |
61 | 76 | $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
62 | 77 | |
@@ -89,20 +104,27 @@ discard block |
||
89 | 104 | public static function decrypt($ciphertext, $password, $nBits) |
90 | 105 | { |
91 | 106 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
92 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // standard allows 128/192/256 bit keys |
|
107 | + if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
108 | + return ''; |
|
109 | + } |
|
110 | + // standard allows 128/192/256 bit keys |
|
93 | 111 | $ciphertext = base64_decode($ciphertext); |
94 | 112 | |
95 | 113 | // use AES to encrypt password (mirroring encrypt routine) |
96 | 114 | $nBytes = $nBits/8; // no bytes in key |
97 | 115 | $pwBytes = array(); |
98 | - for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
116 | + for ($i=0; $i<$nBytes; $i++) { |
|
117 | + $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
118 | + } |
|
99 | 119 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
100 | 120 | $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
101 | 121 | |
102 | 122 | // recover nonce from 1st element of ciphertext |
103 | 123 | $counterBlock = array(); |
104 | 124 | $ctrTxt = substr($ciphertext, 0, 8); |
105 | - for ($i=0; $i<8; $i++) $counterBlock[$i] = ord(substr($ctrTxt,$i,1)); |
|
125 | + for ($i=0; $i<8; $i++) { |
|
126 | + $counterBlock[$i] = ord(substr($ctrTxt,$i,1)); |
|
127 | + } |
|
106 | 128 | |
107 | 129 | // generate key schedule |
108 | 130 | $keySchedule = Aes::keyExpansion($key); |
@@ -110,7 +132,9 @@ discard block |
||
110 | 132 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
111 | 133 | $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); |
112 | 134 | $ct = array(); |
113 | - for ($b=0; $b<$nBlocks; $b++) $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
135 | + for ($b=0; $b<$nBlocks; $b++) { |
|
136 | + $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
137 | + } |
|
114 | 138 | $ciphertext = $ct; // ciphertext is now array of block-length strings |
115 | 139 | |
116 | 140 | // plaintext will get generated block-by-block into array of block-length strings |
@@ -118,8 +142,12 @@ discard block |
||
118 | 142 | |
119 | 143 | for ($b=0; $b<$nBlocks; $b++) { |
120 | 144 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
121 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
122 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
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 | + } |
|
123 | 151 | |
124 | 152 | $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
125 | 153 |
@@ -49,6 +49,10 @@ discard block |
||
49 | 49 | return $output; |
50 | 50 | } |
51 | 51 | |
52 | + /** |
|
53 | + * @param integer $rnd |
|
54 | + * @param integer $Nb |
|
55 | + */ |
|
52 | 56 | private static function addRoundKey($state, $w, $rnd, $Nb) |
53 | 57 | { |
54 | 58 | // xor Round Key into state S [é5.1.4] |
@@ -61,6 +65,9 @@ discard block |
||
61 | 65 | return $state; |
62 | 66 | } |
63 | 67 | |
68 | + /** |
|
69 | + * @param integer $Nb |
|
70 | + */ |
|
64 | 71 | private static function subBytes($s, $Nb) |
65 | 72 | { |
66 | 73 | // apply SBox to state S [é5.1.1] |
@@ -73,6 +80,9 @@ discard block |
||
73 | 80 | return $s; |
74 | 81 | } |
75 | 82 | |
83 | + /** |
|
84 | + * @param integer $Nb |
|
85 | + */ |
|
76 | 86 | private static function shiftRows($s, $Nb) |
77 | 87 | { |
78 | 88 | // shift row r of state S left by r bytes [é5.1.2] |
@@ -88,6 +98,9 @@ discard block |
||
88 | 98 | return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
89 | 99 | } |
90 | 100 | |
101 | + /** |
|
102 | + * @param integer $Nb |
|
103 | + */ |
|
91 | 104 | private static function mixColumns($s, $Nb) |
92 | 105 | { |
93 | 106 | // combine bytes of each col of state S [é5.1.3] |
@@ -219,10 +232,10 @@ discard block |
||
219 | 232 | * |
220 | 233 | * Unicode multi-byte character safe |
221 | 234 | * |
222 | - * @param plaintext source text to be encrypted |
|
235 | + * @param plaintext string text to be encrypted |
|
223 | 236 | * @param password the password to use to generate a key |
224 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
225 | - * @return encrypted text |
|
237 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
238 | + * @return string text |
|
226 | 239 | */ |
227 | 240 | public static function encrypt($plaintext, $password, $nBits) |
228 | 241 | { |
@@ -302,8 +315,8 @@ discard block |
||
302 | 315 | * |
303 | 316 | * @param ciphertext source text to be decrypted |
304 | 317 | * @param password the password to use to generate a key |
305 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
306 | - * @return decrypted text |
|
318 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
319 | + * @return string text |
|
307 | 320 | */ |
308 | 321 | public static function decrypt($ciphertext, $password, $nBits) |
309 | 322 | { |
@@ -214,16 +214,16 @@ discard block |
||
214 | 214 | { |
215 | 215 | |
216 | 216 | /** |
217 | - * Encrypt a text using AES encryption in Counter mode of operation |
|
218 | - * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
219 | - * |
|
220 | - * Unicode multi-byte character safe |
|
221 | - * |
|
222 | - * @param plaintext source text to be encrypted |
|
223 | - * @param password the password to use to generate a key |
|
224 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
225 | - * @return encrypted text |
|
226 | - */ |
|
217 | + * Encrypt a text using AES encryption in Counter mode of operation |
|
218 | + * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
219 | + * |
|
220 | + * Unicode multi-byte character safe |
|
221 | + * |
|
222 | + * @param plaintext source text to be encrypted |
|
223 | + * @param password the password to use to generate a key |
|
224 | + * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
225 | + * @return encrypted text |
|
226 | + */ |
|
227 | 227 | public static function encrypt($plaintext, $password, $nBits) |
228 | 228 | { |
229 | 229 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
@@ -298,13 +298,13 @@ discard block |
||
298 | 298 | } |
299 | 299 | |
300 | 300 | /** |
301 | - * Decrypt a text encrypted by AES in counter mode of operation |
|
302 | - * |
|
303 | - * @param ciphertext source text to be decrypted |
|
304 | - * @param password the password to use to generate a key |
|
305 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
306 | - * @return decrypted text |
|
307 | - */ |
|
301 | + * Decrypt a text encrypted by AES in counter mode of operation |
|
302 | + * |
|
303 | + * @param ciphertext source text to be decrypted |
|
304 | + * @param password the password to use to generate a key |
|
305 | + * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
306 | + * @return decrypted text |
|
307 | + */ |
|
308 | 308 | public static function decrypt($ciphertext, $password, $nBits) |
309 | 309 | { |
310 | 310 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
@@ -20,17 +20,17 @@ discard block |
||
20 | 20 | public static function cipher($input, $w) |
21 | 21 | { |
22 | 22 | // main cipher function [é5.1] |
23 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
24 | - $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
23 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
24 | + $Nr = count($w) / $Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
25 | 25 | |
26 | - $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
27 | - for ($i=0; $i<4*$Nb; $i++) { |
|
28 | - $state[$i%4][floor($i/4)] = $input[$i]; |
|
26 | + $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
27 | + for ($i = 0; $i < 4 * $Nb; $i++) { |
|
28 | + $state[$i % 4][floor($i / 4)] = $input[$i]; |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | $state = self::addRoundKey($state, $w, 0, $Nb); |
32 | 32 | |
33 | - for ($round=1; $round<$Nr; $round++) { // apply Nr rounds |
|
33 | + for ($round = 1; $round < $Nr; $round++) { // apply Nr rounds |
|
34 | 34 | $state = self::subBytes($state, $Nb); |
35 | 35 | $state = self::shiftRows($state, $Nb); |
36 | 36 | $state = self::mixColumns($state, $Nb); |
@@ -41,9 +41,9 @@ discard block |
||
41 | 41 | $state = self::shiftRows($state, $Nb); |
42 | 42 | $state = self::addRoundKey($state, $w, $Nr, $Nb); |
43 | 43 | |
44 | - $output = array(4*$Nb); // convert state to 1-d array before returning [é3.4] |
|
45 | - for ($i=0; $i<4*$Nb; $i++) { |
|
46 | - $output[$i] = $state[$i%4][floor($i/4)]; |
|
44 | + $output = array(4 * $Nb); // convert state to 1-d array before returning [é3.4] |
|
45 | + for ($i = 0; $i < 4 * $Nb; $i++) { |
|
46 | + $output[$i] = $state[$i % 4][floor($i / 4)]; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | return $output; |
@@ -52,9 +52,9 @@ discard block |
||
52 | 52 | private static function addRoundKey($state, $w, $rnd, $Nb) |
53 | 53 | { |
54 | 54 | // xor Round Key into state S [é5.1.4] |
55 | - for ($r=0; $r<4; $r++) { |
|
56 | - for ($c=0; $c<$Nb; $c++) { |
|
57 | - $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
55 | + for ($r = 0; $r < 4; $r++) { |
|
56 | + for ($c = 0; $c < $Nb; $c++) { |
|
57 | + $state[$r][$c] ^= $w[$rnd * 4 + $c][$r]; |
|
58 | 58 | } |
59 | 59 | } |
60 | 60 | |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | private static function subBytes($s, $Nb) |
65 | 65 | { |
66 | 66 | // apply SBox to state S [é5.1.1] |
67 | - for ($r=0; $r<4; $r++) { |
|
68 | - for ($c=0; $c<$Nb; $c++) { |
|
67 | + for ($r = 0; $r < 4; $r++) { |
|
68 | + for ($c = 0; $c < $Nb; $c++) { |
|
69 | 69 | $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
70 | 70 | } |
71 | 71 | } |
@@ -77,26 +77,26 @@ discard block |
||
77 | 77 | { |
78 | 78 | // shift row r of state S left by r bytes [é5.1.2] |
79 | 79 | $t = array(4); |
80 | - for ($r=1; $r<4; $r++) { |
|
81 | - for ($c=0; $c<4; $c++) { |
|
82 | - $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy |
|
80 | + for ($r = 1; $r < 4; $r++) { |
|
81 | + for ($c = 0; $c < 4; $c++) { |
|
82 | + $t[$c] = $s[$r][($c + $r) % $Nb]; // shift into temp copy |
|
83 | 83 | } |
84 | - for ($c=0; $c<4; $c++) { |
|
85 | - $s[$r][$c] = $t[$c]; // and copy back |
|
84 | + for ($c = 0; $c < 4; $c++) { |
|
85 | + $s[$r][$c] = $t[$c]; // and copy back |
|
86 | 86 | } |
87 | 87 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
88 | - return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
88 | + return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | private static function mixColumns($s, $Nb) |
92 | 92 | { |
93 | 93 | // combine bytes of each col of state S [é5.1.3] |
94 | - for ($c=0; $c<4; $c++) { |
|
95 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
96 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
97 | - for ($i=0; $i<4; $i++) { |
|
94 | + for ($c = 0; $c < 4; $c++) { |
|
95 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
96 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
97 | + for ($i = 0; $i < 4; $i++) { |
|
98 | 98 | $a[$i] = $s[$i][$c]; |
99 | - $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
|
99 | + $b[$i] = $s[$i][$c] & 0x80 ? $s[$i][$c] << 1 ^ 0x011b : $s[$i][$c] << 1; |
|
100 | 100 | } |
101 | 101 | // a[n] ^ b[n] is aé{03} in GF(2^8) |
102 | 102 | $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
@@ -118,33 +118,33 @@ discard block |
||
118 | 118 | public static function keyExpansion($key) |
119 | 119 | { |
120 | 120 | // generate Key Schedule from Cipher Key [é5.2] |
121 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
122 | - $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
123 | - $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
121 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
122 | + $Nk = count($key) / 4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
123 | + $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
124 | 124 | |
125 | 125 | $w = array(); |
126 | 126 | $temp = array(); |
127 | 127 | |
128 | - for ($i=0; $i<$Nk; $i++) { |
|
129 | - $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
128 | + for ($i = 0; $i < $Nk; $i++) { |
|
129 | + $r = array($key[4 * $i], $key[4 * $i + 1], $key[4 * $i + 2], $key[4 * $i + 3]); |
|
130 | 130 | $w[$i] = $r; |
131 | 131 | } |
132 | 132 | |
133 | - for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
|
133 | + for ($i = $Nk; $i < ($Nb * ($Nr + 1)); $i++) { |
|
134 | 134 | $w[$i] = array(); |
135 | - for ($t=0; $t<4; $t++) { |
|
136 | - $temp[$t] = $w[$i-1][$t]; |
|
135 | + for ($t = 0; $t < 4; $t++) { |
|
136 | + $temp[$t] = $w[$i - 1][$t]; |
|
137 | 137 | } |
138 | 138 | if ($i % $Nk == 0) { |
139 | 139 | $temp = self::subWord(self::rotWord($temp)); |
140 | - for ($t=0; $t<4; $t++) { |
|
141 | - $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
140 | + for ($t = 0; $t < 4; $t++) { |
|
141 | + $temp[$t] ^= self::$rCon[$i / $Nk][$t]; |
|
142 | 142 | } |
143 | - } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
143 | + } elseif ($Nk > 6 && $i % $Nk == 4) { |
|
144 | 144 | $temp = self::subWord($temp); |
145 | 145 | } |
146 | - for ($t=0; $t<4; $t++) { |
|
147 | - $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
146 | + for ($t = 0; $t < 4; $t++) { |
|
147 | + $w[$i][$t] = $w[$i - $Nk][$t] ^ $temp[$t]; |
|
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | private static function subWord($w) |
155 | 155 | { |
156 | 156 | // apply SBox to 4-byte word w |
157 | - for ($i=0; $i<4; $i++) { |
|
157 | + for ($i = 0; $i < 4; $i++) { |
|
158 | 158 | $w[$i] = self::$sBox[$w[$i]]; |
159 | 159 | } |
160 | 160 | |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | { |
166 | 166 | // rotate 4-byte word w left by one byte |
167 | 167 | $tmp = $w[0]; |
168 | - for ($i=0; $i<3; $i++) { |
|
169 | - $w[$i] = $w[$i+1]; |
|
168 | + for ($i = 0; $i < 3; $i++) { |
|
169 | + $w[$i] = $w[$i + 1]; |
|
170 | 170 | } |
171 | 171 | $w[3] = $tmp; |
172 | 172 | |
@@ -175,22 +175,22 @@ discard block |
||
175 | 175 | |
176 | 176 | // sBox is pre-computed multiplicative inverse in GF(2^8) used in subBytes and keyExpansion [é5.1.1] |
177 | 177 | private static $sBox = array( |
178 | - 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, |
|
179 | - 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, |
|
180 | - 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, |
|
181 | - 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, |
|
182 | - 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, |
|
183 | - 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, |
|
184 | - 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, |
|
185 | - 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, |
|
186 | - 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, |
|
187 | - 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, |
|
188 | - 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, |
|
189 | - 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, |
|
190 | - 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, |
|
191 | - 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, |
|
192 | - 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, |
|
193 | - 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 |
|
178 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
179 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
180 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
181 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
182 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
183 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
184 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
185 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
186 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
187 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
188 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
189 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
190 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
191 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
192 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
193 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
194 | 194 | ); |
195 | 195 | |
196 | 196 | // rCon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [é5.2] |
@@ -226,72 +226,72 @@ discard block |
||
226 | 226 | */ |
227 | 227 | public static function encrypt($plaintext, $password, $nBits) |
228 | 228 | { |
229 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
230 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
231 | - return ''; // standard allows 128/192/256 bit keys |
|
229 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
230 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
231 | + return ''; // standard allows 128/192/256 bit keys |
|
232 | 232 | } |
233 | 233 | // note PHP (5) gives us plaintext and password in UTF8 encoding! |
234 | 234 | |
235 | 235 | // use AES itself to encrypt password to get cipher key (using plain password as source for |
236 | 236 | // key expansion) - gives us well encrypted key |
237 | - $nBytes = $nBits/8; // no bytes in key |
|
237 | + $nBytes = $nBits / 8; // no bytes in key |
|
238 | 238 | $pwBytes = array(); |
239 | - for ($i=0; $i<$nBytes; $i++) { |
|
239 | + for ($i = 0; $i < $nBytes; $i++) { |
|
240 | 240 | $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
241 | 241 | } |
242 | 242 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
243 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
243 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
244 | 244 | |
245 | 245 | // initialise counter block (NIST SP800-38A §B.2): millisecond time-stamp for nonce in |
246 | 246 | // 1st 8 bytes, block counter in 2nd 8 bytes |
247 | 247 | $counterBlock = array(); |
248 | - $nonce = floor(microtime(true)*1000); // timestamp: milliseconds since 1-Jan-1970 |
|
249 | - $nonceSec = floor($nonce/1000); |
|
250 | - $nonceMs = $nonce%1000; |
|
248 | + $nonce = floor(microtime(true) * 1000); // timestamp: milliseconds since 1-Jan-1970 |
|
249 | + $nonceSec = floor($nonce / 1000); |
|
250 | + $nonceMs = $nonce % 1000; |
|
251 | 251 | // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes |
252 | - for ($i=0; $i<4; $i++) { |
|
253 | - $counterBlock[$i] = self::urs($nonceSec, $i*8) & 0xff; |
|
252 | + for ($i = 0; $i < 4; $i++) { |
|
253 | + $counterBlock[$i] = self::urs($nonceSec, $i * 8) & 0xff; |
|
254 | 254 | } |
255 | - for ($i=0; $i<4; $i++) { |
|
256 | - $counterBlock[$i+4] = $nonceMs & 0xff; |
|
255 | + for ($i = 0; $i < 4; $i++) { |
|
256 | + $counterBlock[$i + 4] = $nonceMs & 0xff; |
|
257 | 257 | } |
258 | 258 | // and convert it to a string to go on the front of the ciphertext |
259 | 259 | $ctrTxt = ''; |
260 | - for ($i=0; $i<8; $i++) { |
|
260 | + for ($i = 0; $i < 8; $i++) { |
|
261 | 261 | $ctrTxt .= chr($counterBlock[$i]); |
262 | 262 | } |
263 | 263 | |
264 | 264 | // generate key schedule - an expansion of the key into distinct Key Rounds for each round |
265 | 265 | $keySchedule = Aes::keyExpansion($key); |
266 | 266 | |
267 | - $blockCount = ceil(strlen($plaintext)/$blockSize); |
|
268 | - $ciphertxt = array(); // ciphertext as array of strings |
|
267 | + $blockCount = ceil(strlen($plaintext) / $blockSize); |
|
268 | + $ciphertxt = array(); // ciphertext as array of strings |
|
269 | 269 | |
270 | - for ($b=0; $b<$blockCount; $b++) { |
|
270 | + for ($b = 0; $b < $blockCount; $b++) { |
|
271 | 271 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
272 | 272 | // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
273 | - for ($c=0; $c<4; $c++) { |
|
274 | - $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
273 | + for ($c = 0; $c < 4; $c++) { |
|
274 | + $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
275 | 275 | } |
276 | - for ($c=0; $c<4; $c++) { |
|
277 | - $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8); |
|
276 | + for ($c = 0; $c < 4; $c++) { |
|
277 | + $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); |
|
278 | 278 | } |
279 | 279 | |
280 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
280 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
281 | 281 | |
282 | 282 | // block size is reduced on final block |
283 | - $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1; |
|
283 | + $blockLength = $b < $blockCount - 1 ? $blockSize : (strlen($plaintext) - 1) % $blockSize + 1; |
|
284 | 284 | $cipherByte = array(); |
285 | 285 | |
286 | - for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
287 | - $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$i, 1)); |
|
286 | + for ($i = 0; $i < $blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
287 | + $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b * $blockSize + $i, 1)); |
|
288 | 288 | $cipherByte[$i] = chr($cipherByte[$i]); |
289 | 289 | } |
290 | - $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
290 | + $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
291 | 291 | } |
292 | 292 | |
293 | 293 | // implode is more efficient than repeated string concatenation |
294 | - $ciphertext = $ctrTxt . implode('', $ciphertxt); |
|
294 | + $ciphertext = $ctrTxt.implode('', $ciphertxt); |
|
295 | 295 | $ciphertext = base64_encode($ciphertext); |
296 | 296 | |
297 | 297 | return $ciphertext; |
@@ -307,25 +307,25 @@ discard block |
||
307 | 307 | */ |
308 | 308 | public static function decrypt($ciphertext, $password, $nBits) |
309 | 309 | { |
310 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
311 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
312 | - return ''; // standard allows 128/192/256 bit keys |
|
310 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
311 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
312 | + return ''; // standard allows 128/192/256 bit keys |
|
313 | 313 | } |
314 | 314 | $ciphertext = base64_decode($ciphertext); |
315 | 315 | |
316 | 316 | // use AES to encrypt password (mirroring encrypt routine) |
317 | - $nBytes = $nBits/8; // no bytes in key |
|
317 | + $nBytes = $nBits / 8; // no bytes in key |
|
318 | 318 | $pwBytes = array(); |
319 | - for ($i=0; $i<$nBytes; $i++) { |
|
319 | + for ($i = 0; $i < $nBytes; $i++) { |
|
320 | 320 | $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
321 | 321 | } |
322 | 322 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
323 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
323 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
324 | 324 | |
325 | 325 | // recover nonce from 1st element of ciphertext |
326 | 326 | $counterBlock = array(); |
327 | 327 | $ctrTxt = substr($ciphertext, 0, 8); |
328 | - for ($i=0; $i<8; $i++) { |
|
328 | + for ($i = 0; $i < 8; $i++) { |
|
329 | 329 | $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); |
330 | 330 | } |
331 | 331 | |
@@ -333,29 +333,29 @@ discard block |
||
333 | 333 | $keySchedule = Aes::keyExpansion($key); |
334 | 334 | |
335 | 335 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
336 | - $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); |
|
336 | + $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); |
|
337 | 337 | $ct = array(); |
338 | - for ($b=0; $b<$nBlocks; $b++) { |
|
339 | - $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
338 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
339 | + $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); |
|
340 | 340 | } |
341 | - $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
341 | + $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
342 | 342 | |
343 | 343 | // plaintext will get generated block-by-block into array of block-length strings |
344 | 344 | $plaintxt = array(); |
345 | 345 | |
346 | - for ($b=0; $b<$nBlocks; $b++) { |
|
346 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
347 | 347 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
348 | - for ($c=0; $c<4; $c++) { |
|
349 | - $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
348 | + for ($c = 0; $c < 4; $c++) { |
|
349 | + $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
350 | 350 | } |
351 | - for ($c=0; $c<4; $c++) { |
|
352 | - $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
351 | + for ($c = 0; $c < 4; $c++) { |
|
352 | + $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; |
|
353 | 353 | } |
354 | 354 | |
355 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
355 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
356 | 356 | |
357 | 357 | $plaintxtByte = array(); |
358 | - for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
358 | + for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { |
|
359 | 359 | // -- xor plaintext with ciphered counter byte-by-byte -- |
360 | 360 | $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); |
361 | 361 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
@@ -379,12 +379,12 @@ discard block |
||
379 | 379 | private static function urs($a, $b) |
380 | 380 | { |
381 | 381 | $a &= 0xffffffff; |
382 | - $b &= 0x1f; // (bounds check) |
|
383 | - if ($a&0x80000000 && $b>0) { // if left-most bit set |
|
384 | - $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
385 | - $a = $a >> ($b-1); // remaining right-shifts |
|
382 | + $b &= 0x1f; // (bounds check) |
|
383 | + if ($a & 0x80000000 && $b > 0) { // if left-most bit set |
|
384 | + $a = ($a >> 1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
385 | + $a = $a >> ($b - 1); // remaining right-shifts |
|
386 | 386 | } else { // otherwise |
387 | - $a = ($a>>$b); // use normal right-shift |
|
387 | + $a = ($a >> $b); // use normal right-shift |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | return $a; |
@@ -7,6 +7,9 @@ |
||
7 | 7 | ################ |
8 | 8 | ## Function permits to get the value from a line |
9 | 9 | ################ |
10 | +/** |
|
11 | + * @param string $val |
|
12 | + */ |
|
10 | 13 | function getSettingValue($val) |
11 | 14 | { |
12 | 15 | $val = trim(strstr($val, "=")); |
@@ -423,8 +423,8 @@ discard block |
||
423 | 423 | <h3>Step 2</h3> |
424 | 424 | <fieldset><legend>DataBase Informations</legend>'; |
425 | 425 | |
426 | - // check if all database info are available |
|
427 | - if ( |
|
426 | + // check if all database info are available |
|
427 | + if ( |
|
428 | 428 | isset($_SESSION['server']) && !empty($_SESSION['server']) |
429 | 429 | && isset($_SESSION['database']) && !empty($_SESSION['database']) |
430 | 430 | && isset($_SESSION['user']) && !empty($_SESSION['user']) |
@@ -437,15 +437,15 @@ discard block |
||
437 | 437 | The database information has been retreived from the settings file.<br> |
438 | 438 | If you need to change them, please edit file `/includes/config/settings.php` and relaunch the upgrade process. |
439 | 439 | </div>'; |
440 | - } else { |
|
440 | + } else { |
|
441 | 441 | echo ' |
442 | 442 | <div style=""> |
443 | 443 | The database information has not been retreived from the settings file.<br> |
444 | 444 | You need to adapt the file `/includes/config/settings.php` and relaunch the upgrade process. |
445 | 445 | </div>'; |
446 | - } |
|
446 | + } |
|
447 | 447 | |
448 | - echo ' |
|
448 | + echo ' |
|
449 | 449 | <a href="'.$protocol.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/') - 8).'/install/upgrade.php">Restart upgrade process</a> |
450 | 450 | </fieldset> |
451 | 451 | |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | </div>'; |
476 | 476 | |
477 | 477 | // teampass_version = 2.1.27 and no encrypt_key in db |
478 | - echo ' |
|
478 | + echo ' |
|
479 | 479 | <div id="no_encrypt_key" style="display:none;"> |
480 | 480 | <fieldset> |
481 | 481 | <legend>Database Origine</legend> |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | function getSettingValue($val) |
11 | 11 | { |
12 | 12 | $val = trim(strstr($val, "=")); |
13 | - return trim(str_replace('"', '', substr($val, 1, strpos($val, ";")-1))); |
|
13 | + return trim(str_replace('"', '', substr($val, 1, strpos($val, ";") - 1))); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | //get infos from SETTINGS.PHP file |
@@ -19,41 +19,41 @@ discard block |
||
19 | 19 | if (file_exists($filename)) { |
20 | 20 | //copy some constants from this existing file |
21 | 21 | $settings_file = file($filename); |
22 | - while (list($key,$val) = each($settings_file)) { |
|
23 | - if (substr_count($val,'charset')>0) { |
|
22 | + while (list($key, $val) = each($settings_file)) { |
|
23 | + if (substr_count($val, 'charset') > 0) { |
|
24 | 24 | $_SESSION['charset'] = getSettingValue($val); |
25 | - } elseif (substr_count($val,'@define(')>0 && substr_count($val, 'SALT')>0) { |
|
26 | - $_SESSION['encrypt_key'] = substr($val,17,strpos($val,"')")-17); |
|
27 | - } elseif (substr_count($val,'$smtp_server = ')>0) { |
|
25 | + } elseif (substr_count($val, '@define(') > 0 && substr_count($val, 'SALT') > 0) { |
|
26 | + $_SESSION['encrypt_key'] = substr($val, 17, strpos($val, "')") - 17); |
|
27 | + } elseif (substr_count($val, '$smtp_server = ') > 0) { |
|
28 | 28 | $_SESSION['smtp_server'] = getSettingValue($val); |
29 | - } elseif (substr_count($val,'$smtp_auth = ')>0) { |
|
29 | + } elseif (substr_count($val, '$smtp_auth = ') > 0) { |
|
30 | 30 | $_SESSION['smtp_auth'] = getSettingValue($val); |
31 | - } elseif (substr_count($val,'$smtp_port = ')>0) { |
|
31 | + } elseif (substr_count($val, '$smtp_port = ') > 0) { |
|
32 | 32 | $_SESSION['smtp_port'] = getSettingValue($val); |
33 | - } elseif (substr_count($val,'$smtp_security = ')>0) { |
|
33 | + } elseif (substr_count($val, '$smtp_security = ') > 0) { |
|
34 | 34 | $_SESSION['smtp_security'] = getSettingValue($val); |
35 | - } elseif (substr_count($val,'$smtp_auth_username = ')>0) { |
|
35 | + } elseif (substr_count($val, '$smtp_auth_username = ') > 0) { |
|
36 | 36 | $_SESSION['smtp_auth_username'] = getSettingValue($val); |
37 | - } elseif (substr_count($val,'$smtp_auth_password = ')>0) { |
|
37 | + } elseif (substr_count($val, '$smtp_auth_password = ') > 0) { |
|
38 | 38 | $_SESSION['smtp_auth_password'] = getSettingValue($val); |
39 | - } elseif (substr_count($val,'$email_from = ')>0) { |
|
39 | + } elseif (substr_count($val, '$email_from = ') > 0) { |
|
40 | 40 | $_SESSION['email_from'] = getSettingValue($val); |
41 | - } elseif (substr_count($val,'$email_from_name = ')>0) { |
|
41 | + } elseif (substr_count($val, '$email_from_name = ') > 0) { |
|
42 | 42 | $_SESSION['email_from_name'] = getSettingValue($val); |
43 | - } elseif (substr_count($val,'$server = ')>0) { |
|
43 | + } elseif (substr_count($val, '$server = ') > 0) { |
|
44 | 44 | $_SESSION['server'] = getSettingValue($val); |
45 | - } elseif (substr_count($val,'$user = ')>0) { |
|
45 | + } elseif (substr_count($val, '$user = ') > 0) { |
|
46 | 46 | $_SESSION['user'] = getSettingValue($val); |
47 | - } elseif (substr_count($val,'$pass = ')>0) { |
|
47 | + } elseif (substr_count($val, '$pass = ') > 0) { |
|
48 | 48 | $_SESSION['pass'] = getSettingValue($val); |
49 | - } elseif (substr_count($val,'$port = ')>0) { |
|
49 | + } elseif (substr_count($val, '$port = ') > 0) { |
|
50 | 50 | $_SESSION['port'] = getSettingValue($val); |
51 | - } elseif (substr_count($val,'$database = ')>0) { |
|
51 | + } elseif (substr_count($val, '$database = ') > 0) { |
|
52 | 52 | $_SESSION['database'] = getSettingValue($val); |
53 | - } elseif (substr_count($val,'$pre = ')>0) { |
|
53 | + } elseif (substr_count($val, '$pre = ') > 0) { |
|
54 | 54 | $_SESSION['pre'] = getSettingValue($val); |
55 | - } elseif (substr_count($val,'require_once "')>0 && substr_count($val, 'sk.php')>0) { |
|
56 | - $_SESSION['sk_path'] = substr($val,14,strpos($val,'";')-14); |
|
55 | + } elseif (substr_count($val, 'require_once "') > 0 && substr_count($val, 'sk.php') > 0) { |
|
56 | + $_SESSION['sk_path'] = substr($val, 14, strpos($val, '";') - 14); |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | } |
@@ -63,9 +63,9 @@ discard block |
||
63 | 63 | ) { |
64 | 64 | //copy some constants from this existing file |
65 | 65 | $skFile = file($_SESSION['sk_file']); |
66 | - while (list($key,$val) = each($skFile)) { |
|
67 | - if (substr_count($val, '@define(')>0) { |
|
68 | - $_SESSION['encrypt_key'] = substr($val, 17, strpos($val, "')")-17); |
|
66 | + while (list($key, $val) = each($skFile)) { |
|
67 | + if (substr_count($val, '@define(') > 0) { |
|
68 | + $_SESSION['encrypt_key'] = substr($val, 17, strpos($val, "')") - 17); |
|
69 | 69 | } |
70 | 70 | } |
71 | 71 | } |
@@ -310,8 +310,8 @@ discard block |
||
310 | 310 | |
311 | 311 | |
312 | 312 | //define root path |
313 | -$abs_path = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . substr($_SERVER['PHP_SELF'], 0, strlen($_SERVER['PHP_SELF'])-20); |
|
314 | -if( isset($_SERVER['HTTPS'] ) ) { |
|
313 | +$abs_path = rtrim($_SERVER['DOCUMENT_ROOT'], '/').substr($_SERVER['PHP_SELF'], 0, strlen($_SERVER['PHP_SELF']) - 20); |
|
314 | +if (isset($_SERVER['HTTPS'])) { |
|
315 | 315 | $protocol = 'https://'; |
316 | 316 | } else { |
317 | 317 | $protocol = 'http://'; |
@@ -334,12 +334,12 @@ discard block |
||
334 | 334 | |
335 | 335 | //HIDDEN THINGS |
336 | 336 | echo ' |
337 | - <input type="hidden" id="step" name="step" value="', isset($_POST['step']) ? $_POST['step']:'', '" /> |
|
338 | - <input type="hidden" id="actual_cpm_version" name="actual_cpm_version" value="', isset($_POST['actual_cpm_version']) ? $_POST['actual_cpm_version']:'', '" /> |
|
339 | - <input type="hidden" id="cpm_isUTF8" name="cpm_isUTF8" value="', isset($_POST['cpm_isUTF8']) ? $_POST['cpm_isUTF8']:'', '" /> |
|
337 | + <input type="hidden" id="step" name="step" value="', isset($_POST['step']) ? $_POST['step'] : '', '" /> |
|
338 | + <input type="hidden" id="actual_cpm_version" name="actual_cpm_version" value="', isset($_POST['actual_cpm_version']) ? $_POST['actual_cpm_version'] : '', '" /> |
|
339 | + <input type="hidden" id="cpm_isUTF8" name="cpm_isUTF8" value="', isset($_POST['cpm_isUTF8']) ? $_POST['cpm_isUTF8'] : '', '" /> |
|
340 | 340 | <input type="hidden" name="menu_action" id="menu_action" value="" /> |
341 | 341 | <input type="hidden" name="user_granted" id="user_granted" value="" /> |
342 | - <input type="hidden" name="session_salt" id="session_salt" value="', (isset($_POST['session_salt']) && !empty($_POST['session_salt'])) ? $_POST['session_salt']:@$_SESSION['encrypt_key'], '" />'; |
|
342 | + <input type="hidden" name="session_salt" id="session_salt" value="', (isset($_POST['session_salt']) && !empty($_POST['session_salt'])) ? $_POST['session_salt'] : @$_SESSION['encrypt_key'], '" />'; |
|
343 | 343 | |
344 | 344 | if (!isset($_GET['step']) && !isset($_POST['step'])) { |
345 | 345 | //ETAPE O |
@@ -571,7 +571,7 @@ discard block |
||
571 | 571 | echo '<br /><br /> |
572 | 572 | <label for="sk_path" style="width:300px;">Absolute path to SaltKey : |
573 | 573 | <img src="images/information-white.png" alt="" title="The SaltKey is stored in a file called sk.php. But for security reasons, this file should be stored in a folder outside the www folder of your server. So please, indicate here the path to this folder."> |
574 | - </label><input type="text" id="sk_path" name="sk_path" value="'.substr($_SESSION['sk_path'], 0, strlen($_SESSION['sk_path'])-7).'" size="75" /><br /> |
|
574 | + </label><input type="text" id="sk_path" name="sk_path" value="'.substr($_SESSION['sk_path'], 0, strlen($_SESSION['sk_path']) - 7).'" size="75" /><br /> |
|
575 | 575 | '; |
576 | 576 | } |
577 | 577 | echo ' |
@@ -602,7 +602,7 @@ discard block |
||
602 | 602 | <div style="width:900px;margin:auto;margin-top:30px;"> |
603 | 603 | <div id="progressbar" style="float:left;margin-top:9px;"></div> |
604 | 604 | <div id="buttons_bottom"> |
605 | - <input type="button" id="but_next" target_id="'. (intval($_POST['step'])+1).'" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="NEXT" /> |
|
605 | + <input type="button" id="but_next" target_id="'. (intval($_POST['step']) + 1).'" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="NEXT" /> |
|
606 | 606 | </div> |
607 | 607 | </div>'; |
608 | 608 | } elseif ($_POST['step'] == 3 && $conversion_utf8 == true && $_SESSION['user_granted'] === "1") { |
@@ -610,22 +610,22 @@ discard block |
||
610 | 610 | <div style="width:900px;margin:auto;margin-top:30px;"> |
611 | 611 | <div id="progressbar" style="float:left;margin-top:9px;"></div> |
612 | 612 | <div id="buttons_bottom"> |
613 | - <input type="button" id="but_launch" onclick="Check(\'step'.$_POST['step'] .'\')" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="LAUNCH" /> |
|
614 | - <input type="button" id="but_next" target_id="'. (intval($_POST['step'])+1).'" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="NEXT" disabled="disabled" /> |
|
613 | + <input type="button" id="but_launch" onclick="Check(\'step'.$_POST['step'].'\')" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="LAUNCH" /> |
|
614 | + <input type="button" id="but_next" target_id="'. (intval($_POST['step']) + 1).'" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="NEXT" disabled="disabled" /> |
|
615 | 615 | </div> |
616 | 616 | </div>'; |
617 | 617 | } elseif ($_POST['step'] == 6 && $_SESSION['user_granted'] === "1") { |
618 | 618 | echo ' |
619 | 619 | <div style="margin-top:30px; text-align:center; width:100%; font-size:24px;"> |
620 | - <a href="#" onclick="javascript:window.location.href=\'', (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ? 'https' : 'http', '://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')-8).'\';"><b>Open TeamPass</b></a> |
|
620 | + <a href="#" onclick="javascript:window.location.href=\'', (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ? 'https' : 'http', '://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/') - 8).'\';"><b>Open TeamPass</b></a> |
|
621 | 621 | </div>'; |
622 | 622 | } else { |
623 | 623 | echo ' |
624 | 624 | <div style="width:900px;margin:auto;margin-top:30px;"> |
625 | 625 | <div id="progressbar" style="float:left;margin-top:9px;"></div> |
626 | 626 | <div id="buttons_bottom"> |
627 | - <input type="button" id="but_launch" onclick="Check(\'step'.$_POST['step'] .'\')" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="LAUNCH" /> |
|
628 | - <input type="button" id="but_next" target_id="'. (intval($_POST['step'])+1).'" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="NEXT" disabled="disabled" /> |
|
627 | + <input type="button" id="but_launch" onclick="Check(\'step'.$_POST['step'].'\')" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="LAUNCH" /> |
|
628 | + <input type="button" id="but_next" target_id="'. (intval($_POST['step']) + 1).'" style="padding:3px;cursor:pointer;font-size:20px;" class="ui-state-default ui-corner-all" value="NEXT" disabled="disabled" /> |
|
629 | 629 | </div> |
630 | 630 | </div>'; |
631 | 631 | } |
@@ -50,6 +50,9 @@ |
||
50 | 50 | ################ |
51 | 51 | ## Function permits to get the value from a line |
52 | 52 | ################ |
53 | +/** |
|
54 | + * @param string $val |
|
55 | + */ |
|
53 | 56 | function getSettingValue($val) |
54 | 57 | { |
55 | 58 | $val = trim(strstr($val, "=")); |
@@ -469,7 +469,7 @@ |
||
469 | 469 | } else { |
470 | 470 | // user said that database has not being used for an older version |
471 | 471 | // no old sk is available |
472 | - $tmp = mysqli_num_rows(mysqli_query( |
|
472 | + $tmp = mysqli_num_rows(mysqli_query( |
|
473 | 473 | $dbTmp, |
474 | 474 | "SELECT * FROM `".$var['tbl_prefix']."misc` WHERE type = 'admin' AND intitule = 'saltkey_ante_2127'" |
475 | 475 | )); |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | function getSettingValue($val) |
54 | 54 | { |
55 | 55 | $val = trim(strstr($val, "=")); |
56 | - return trim(str_replace('"', '', substr($val, 1, strpos($val, ";")-1))); |
|
56 | + return trim(str_replace('"', '', substr($val, 1, strpos($val, ";") - 1))); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | ################ |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | global $dbTmp; |
65 | 65 | $exists = false; |
66 | 66 | $columns = mysqli_query($dbTmp, "show columns from $db"); |
67 | - while ($c = mysqli_fetch_assoc( $columns)) { |
|
67 | + while ($c = mysqli_fetch_assoc($columns)) { |
|
68 | 68 | if ($c['Field'] == $column) { |
69 | 69 | $exists = true; |
70 | 70 | break; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | } |
76 | 76 | } |
77 | 77 | |
78 | -function addIndexIfNotExist($table, $index, $sql ) { |
|
78 | +function addIndexIfNotExist($table, $index, $sql) { |
|
79 | 79 | global $dbTmp; |
80 | 80 | |
81 | 81 | $mysqli_result = mysqli_query($dbTmp, "SHOW INDEX FROM $table WHERE key_name LIKE \"$index\""); |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | |
84 | 84 | // if index does not exist, then add it |
85 | 85 | if (!$res) { |
86 | - $res = mysqli_query($dbTmp, "ALTER TABLE `$table` " . $sql); |
|
86 | + $res = mysqli_query($dbTmp, "ALTER TABLE `$table` ".$sql); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | return $res; |
@@ -187,13 +187,13 @@ discard block |
||
187 | 187 | $_SESSION['fullurl'] = $_POST['fullurl']; |
188 | 188 | $abspath = str_replace('\\', '/', $_POST['abspath']); |
189 | 189 | $_SESSION['abspath'] = $abspath; |
190 | - if (substr($abspath, strlen($abspath)-1) == "/") { |
|
191 | - $abspath = substr($abspath, 0, strlen($abspath)-1); |
|
190 | + if (substr($abspath, strlen($abspath) - 1) == "/") { |
|
191 | + $abspath = substr($abspath, 0, strlen($abspath) - 1); |
|
192 | 192 | } |
193 | 193 | $okWritable = true; |
194 | 194 | $okExtensions = true; |
195 | 195 | $txt = ""; |
196 | - $x=1; |
|
196 | + $x = 1; |
|
197 | 197 | $tab = array( |
198 | 198 | $abspath."/includes/config/settings.php", |
199 | 199 | $abspath."/includes/libraries/csrfp/libs/", |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | ); |
207 | 207 | foreach ($tab as $elem) { |
208 | 208 | // try to create it if not existing |
209 | - if(substr($elem, -1) === '/' && !is_dir($elem)) { |
|
209 | + if (substr($elem, -1) === '/' && !is_dir($elem)) { |
|
210 | 210 | mkdir($elem); |
211 | 211 | } |
212 | 212 | // check if writable |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | $txt .= '<span style=\"padding-left:30px;font-size:13pt;\">PHP extension \"curl\"'. |
285 | 285 | ' <img src=\"images/tick-circle.png\"></span><br />'; |
286 | 286 | } |
287 | - if (ini_get('max_execution_time')<60) { |
|
287 | + if (ini_get('max_execution_time') < 60) { |
|
288 | 288 | $txt .= '<span style=\"padding-left:30px;font-size:13pt;\">PHP \"Maximum '. |
289 | 289 | 'execution time\" is set to '.ini_get('max_execution_time').' seconds.'. |
290 | 290 | ' Please try to set to 60s at least until Upgrade is finished. '. |
@@ -311,41 +311,41 @@ discard block |
||
311 | 311 | if (file_exists($filename)) { |
312 | 312 | //copy some constants from this existing file |
313 | 313 | $settingsFile = file($filename); |
314 | - while (list($key,$val) = each($settingsFile)) { |
|
315 | - if (substr_count($val, 'charset')>0) { |
|
314 | + while (list($key, $val) = each($settingsFile)) { |
|
315 | + if (substr_count($val, 'charset') > 0) { |
|
316 | 316 | $_SESSION['charset'] = getSettingValue($val); |
317 | - } elseif (substr_count($val, '@define(')>0 && substr_count($val, 'SALT')>0) { |
|
318 | - $_SESSION['encrypt_key'] = substr($val, 17, strpos($val, "')")-17); |
|
319 | - } elseif (substr_count($val, '$smtp_server')>0) { |
|
317 | + } elseif (substr_count($val, '@define(') > 0 && substr_count($val, 'SALT') > 0) { |
|
318 | + $_SESSION['encrypt_key'] = substr($val, 17, strpos($val, "')") - 17); |
|
319 | + } elseif (substr_count($val, '$smtp_server') > 0) { |
|
320 | 320 | $_SESSION['smtp_server'] = getSettingValue($val); |
321 | - } elseif (substr_count($val, '$smtp_auth')>0) { |
|
321 | + } elseif (substr_count($val, '$smtp_auth') > 0) { |
|
322 | 322 | $_SESSION['smtp_auth'] = getSettingValue($val); |
323 | - } elseif (substr_count($val, '$smtp_auth_username')>0) { |
|
323 | + } elseif (substr_count($val, '$smtp_auth_username') > 0) { |
|
324 | 324 | $_SESSION['smtp_auth_username'] = getSettingValue($val); |
325 | - } elseif (substr_count($val, '$smtp_auth_password')>0) { |
|
325 | + } elseif (substr_count($val, '$smtp_auth_password') > 0) { |
|
326 | 326 | $_SESSION['smtp_auth_password'] = getSettingValue($val); |
327 | - } elseif (substr_count($val, '$smtp_port')>0) { |
|
327 | + } elseif (substr_count($val, '$smtp_port') > 0) { |
|
328 | 328 | $_SESSION['smtp_port'] = getSettingValue($val); |
329 | - } elseif (substr_count($val, '$smtp_security')>0) { |
|
329 | + } elseif (substr_count($val, '$smtp_security') > 0) { |
|
330 | 330 | $_SESSION['smtp_security'] = getSettingValue($val); |
331 | - } elseif (substr_count($val, '$email_from')>0) { |
|
331 | + } elseif (substr_count($val, '$email_from') > 0) { |
|
332 | 332 | $_SESSION['email_from'] = getSettingValue($val); |
333 | - } elseif (substr_count($val, '$email_from_name')>0) { |
|
333 | + } elseif (substr_count($val, '$email_from_name') > 0) { |
|
334 | 334 | $_SESSION['email_from_name'] = getSettingValue($val); |
335 | - } elseif (substr_count($val, '$server')>0) { |
|
335 | + } elseif (substr_count($val, '$server') > 0) { |
|
336 | 336 | $_SESSION['server'] = getSettingValue($val); |
337 | - } elseif (substr_count($val, '$user')>0) { |
|
337 | + } elseif (substr_count($val, '$user') > 0) { |
|
338 | 338 | $_SESSION['user'] = getSettingValue($val); |
339 | - } elseif (substr_count($val, '$pass')>0) { |
|
339 | + } elseif (substr_count($val, '$pass') > 0) { |
|
340 | 340 | $_SESSION['pass'] = getSettingValue($val); |
341 | - } elseif (substr_count($val, '$port')>0) { |
|
341 | + } elseif (substr_count($val, '$port') > 0) { |
|
342 | 342 | $_SESSION['port'] = getSettingValue($val); |
343 | - } elseif (substr_count($val, '$database')>0) { |
|
343 | + } elseif (substr_count($val, '$database') > 0) { |
|
344 | 344 | $_SESSION['database'] = getSettingValue($val); |
345 | - } elseif (substr_count($val, '$pre')>0) { |
|
345 | + } elseif (substr_count($val, '$pre') > 0) { |
|
346 | 346 | $_SESSION['pre'] = getSettingValue($val); |
347 | - } elseif (substr_count($val, 'require_once "')>0 && substr_count($val, 'sk.php')>0) { |
|
348 | - $_SESSION['sk_file'] = substr($val, 14, strpos($val, '";')-14); |
|
347 | + } elseif (substr_count($val, 'require_once "') > 0 && substr_count($val, 'sk.php') > 0) { |
|
348 | + $_SESSION['sk_file'] = substr($val, 14, strpos($val, '";') - 14); |
|
349 | 349 | } |
350 | 350 | } |
351 | 351 | } |
@@ -358,9 +358,9 @@ discard block |
||
358 | 358 | '</span><br />'; |
359 | 359 | //copy some constants from this existing file |
360 | 360 | $skFile = file($_SESSION['sk_file']); |
361 | - while (list($key,$val) = each($skFile)) { |
|
362 | - if (substr_count($val, "@define('SALT'")>0) { |
|
363 | - $_SESSION['encrypt_key'] = substr($val, 17, strpos($val, "')")-17); |
|
361 | + while (list($key, $val) = each($skFile)) { |
|
362 | + if (substr_count($val, "@define('SALT'") > 0) { |
|
363 | + $_SESSION['encrypt_key'] = substr($val, 17, strpos($val, "')") - 17); |
|
364 | 364 | echo '$("#session_salt").val("'.$_SESSION['encrypt_key'].'");'; |
365 | 365 | } |
366 | 366 | } |
@@ -568,11 +568,11 @@ discard block |
||
568 | 568 | if ( |
569 | 569 | isset($_POST['prefix_before_convert']) && $_POST['prefix_before_convert'] == "true" |
570 | 570 | ) { |
571 | - $tables =mysqli_query($dbTmp,'SHOW TABLES'); |
|
571 | + $tables = mysqli_query($dbTmp, 'SHOW TABLES'); |
|
572 | 572 | while ($table = mysqli_fetch_row($tables)) { |
573 | 573 | if (tableExists("old_".$table[0]) != 1 && substr($table[0], 0, 4) != "old_") { |
574 | - mysqli_query($dbTmp,"CREATE TABLE old_".$table[0]." LIKE ".$table[0]); |
|
575 | - mysqli_query($dbTmp,"INSERT INTO old_".$table[0]." SELECT * FROM ".$table[0]); |
|
574 | + mysqli_query($dbTmp, "CREATE TABLE old_".$table[0]." LIKE ".$table[0]); |
|
575 | + mysqli_query($dbTmp, "INSERT INTO old_".$table[0]." SELECT * FROM ".$table[0]); |
|
576 | 576 | } |
577 | 577 | } |
578 | 578 | } |
@@ -584,7 +584,7 @@ discard block |
||
584 | 584 | ); |
585 | 585 | |
586 | 586 | //convert tables |
587 | - $res = mysqli_query($dbTmp,"SHOW TABLES FROM `".$_SESSION['database']."`"); |
|
587 | + $res = mysqli_query($dbTmp, "SHOW TABLES FROM `".$_SESSION['database']."`"); |
|
588 | 588 | while ($table = mysqli_fetch_row($res)) { |
589 | 589 | if (substr($table[0], 0, 4) != "old_") { |
590 | 590 | mysqli_query($dbTmp, |
@@ -692,16 +692,16 @@ discard block |
||
692 | 692 | global \$server, \$user, \$pass, \$database, \$pre, \$db, \$port, \$encoding; |
693 | 693 | |
694 | 694 | ### DATABASE connexion parameters ### |
695 | -\$server = \"". $_SESSION['server'] ."\"; |
|
696 | -\$user = \"". $_SESSION['user'] ."\"; |
|
697 | -\$pass = \"". str_replace("$", "\\$", $_SESSION['pass']) ."\"; |
|
698 | -\$database = \"". $_SESSION['database'] ."\"; |
|
699 | -\$port = ". $_SESSION['port'] ."; |
|
700 | -\$pre = \"". $_SESSION['pre'] ."\"; |
|
695 | +\$server = \"". $_SESSION['server']."\"; |
|
696 | +\$user = \"". $_SESSION['user']."\"; |
|
697 | +\$pass = \"". str_replace("$", "\\$", $_SESSION['pass'])."\"; |
|
698 | +\$database = \"". $_SESSION['database']."\"; |
|
699 | +\$port = ". $_SESSION['port']."; |
|
700 | +\$pre = \"". $_SESSION['pre']."\"; |
|
701 | 701 | \$encoding = \"".$_SESSION['db_encoding']."\"; |
702 | 702 | |
703 | 703 | @date_default_timezone_set(\$_SESSION['settings']['timezone']); |
704 | -@define('SECUREPATH', '".substr($skFile, 0, strlen($skFile)-7)."'); |
|
704 | +@define('SECUREPATH', '".substr($skFile, 0, strlen($skFile) - 7)."'); |
|
705 | 705 | require_once \"".$skFile."\"; |
706 | 706 | @define('COST', '13'); // Don't change this." |
707 | 707 | ) |
@@ -747,8 +747,8 @@ discard block |
||
747 | 747 | $events .= "The file $csrfp_file already exist. A copy has been created.<br />"; |
748 | 748 | } |
749 | 749 | } |
750 | - unlink($csrfp_file); // delete existing csrfp.config file |
|
751 | - copy($csrfp_file_sample, $csrfp_file); // make a copy of csrfp.config.sample file |
|
750 | + unlink($csrfp_file); // delete existing csrfp.config file |
|
751 | + copy($csrfp_file_sample, $csrfp_file); // make a copy of csrfp.config.sample file |
|
752 | 752 | $data = file_get_contents("../includes/libraries/csrfp/libs/csrfp.config.php"); |
753 | 753 | $newdata = str_replace('"CSRFP_TOKEN" => ""', '"CSRFP_TOKEN" => "'.bin2hex(openssl_random_pseudo_bytes(25)).'"', $data); |
754 | 754 | $newdata = str_replace('"tokenLength" => "25"', '"tokenLength" => "50"', $newdata); |
@@ -797,20 +797,20 @@ discard block |
||
797 | 797 | |
798 | 798 | $mysqli = new mysqli($_SESSION['server'], $_SESSION['user'], $_SESSION['pass'], $_SESSION['database'], $_SESSION['port']); |
799 | 799 | if ($mysqli->connect_error) { |
800 | - die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); |
|
800 | + die('Error : ('.$mysqli->connect_errno.') '.$mysqli->connect_error); |
|
801 | 801 | } |
802 | 802 | |
803 | 803 | $results = $mysqli->query("SHOW TABLES"); |
804 | 804 | |
805 | - while($row = $results->fetch_array()){ |
|
805 | + while ($row = $results->fetch_array()) { |
|
806 | 806 | $mtables[] = $row[0]; |
807 | 807 | } |
808 | 808 | |
809 | - foreach($mtables as $table){ |
|
809 | + foreach ($mtables as $table) { |
|
810 | 810 | $contents .= "-- Table `".$table."` --\n"; |
811 | 811 | |
812 | 812 | $results = $mysqli->query("SHOW CREATE TABLE ".$table); |
813 | - while($row = $results->fetch_array()){ |
|
813 | + while ($row = $results->fetch_array()) { |
|
814 | 814 | $contents .= $row[1].";\n\n"; |
815 | 815 | } |
816 | 816 | |
@@ -820,39 +820,39 @@ discard block |
||
820 | 820 | $fields_count = count($fields); |
821 | 821 | |
822 | 822 | $insert_head = "INSERT INTO `".$table."` ("; |
823 | - for($i=0; $i < $fields_count; $i++){ |
|
824 | - $insert_head .= "`".$fields[$i]->name."`"; |
|
825 | - if($i < $fields_count-1){ |
|
826 | - $insert_head .= ', '; |
|
823 | + for ($i = 0; $i < $fields_count; $i++) { |
|
824 | + $insert_head .= "`".$fields[$i]->name."`"; |
|
825 | + if ($i < $fields_count - 1) { |
|
826 | + $insert_head .= ', '; |
|
827 | 827 | } |
828 | 828 | } |
829 | - $insert_head .= ")"; |
|
829 | + $insert_head .= ")"; |
|
830 | 830 | $insert_head .= " VALUES\n"; |
831 | 831 | |
832 | - if($row_count>0){ |
|
832 | + if ($row_count > 0) { |
|
833 | 833 | $r = 0; |
834 | - while($row = $results->fetch_array()){ |
|
835 | - if(($r % 400) == 0){ |
|
834 | + while ($row = $results->fetch_array()) { |
|
835 | + if (($r % 400) == 0) { |
|
836 | 836 | $contents .= $insert_head; |
837 | 837 | } |
838 | 838 | $contents .= "("; |
839 | - for($i=0; $i < $fields_count; $i++){ |
|
840 | - $row_content = str_replace("\n","\\n",$mysqli->real_escape_string($row[$i])); |
|
839 | + for ($i = 0; $i < $fields_count; $i++) { |
|
840 | + $row_content = str_replace("\n", "\\n", $mysqli->real_escape_string($row[$i])); |
|
841 | 841 | |
842 | - switch($fields[$i]->type){ |
|
842 | + switch ($fields[$i]->type) { |
|
843 | 843 | case 8: case 3: |
844 | - $contents .= $row_content; |
|
844 | + $contents .= $row_content; |
|
845 | 845 | break; |
846 | 846 | default: |
847 | - $contents .= "'". $row_content ."'"; |
|
847 | + $contents .= "'".$row_content."'"; |
|
848 | 848 | } |
849 | - if($i < $fields_count-1){ |
|
850 | - $contents .= ', '; |
|
849 | + if ($i < $fields_count - 1) { |
|
850 | + $contents .= ', '; |
|
851 | 851 | } |
852 | 852 | } |
853 | - if(($r+1) == $row_count || ($r % 400) == 399){ |
|
853 | + if (($r + 1) == $row_count || ($r % 400) == 399) { |
|
854 | 854 | $contents .= ");\n\n"; |
855 | - }else{ |
|
855 | + } else { |
|
856 | 856 | $contents .= "),\n"; |
857 | 857 | } |
858 | 858 | $r++; |
@@ -860,9 +860,9 @@ discard block |
||
860 | 860 | } |
861 | 861 | } |
862 | 862 | |
863 | - $backup_file_name = "sql-backup-".date( "d-m-Y--h-i-s").".sql"; |
|
863 | + $backup_file_name = "sql-backup-".date("d-m-Y--h-i-s").".sql"; |
|
864 | 864 | |
865 | - $fp = fopen("../files/".$backup_file_name ,'w+'); |
|
865 | + $fp = fopen("../files/".$backup_file_name, 'w+'); |
|
866 | 866 | if (($result = fwrite($fp, $contents))) { |
867 | 867 | echo '[{ "error" : "" , "file" : "files/'.$backup_file_name.'"}]'; |
868 | 868 | } else { |
@@ -100,9 +100,12 @@ discard block |
||
100 | 100 | AND table_name = '$tablename'" |
101 | 101 | ); |
102 | 102 | |
103 | - if ($res > 0) return true; |
|
104 | - else return false; |
|
105 | -} |
|
103 | + if ($res > 0) { |
|
104 | + return true; |
|
105 | + } else { |
|
106 | + return false; |
|
107 | + } |
|
108 | + } |
|
106 | 109 | |
107 | 110 | //define pbkdf2 iteration count |
108 | 111 | @define('ITCOUNT', '2072'); |
@@ -852,7 +855,7 @@ discard block |
||
852 | 855 | } |
853 | 856 | if(($r+1) == $row_count || ($r % 400) == 399){ |
854 | 857 | $contents .= ");\n\n"; |
855 | - }else{ |
|
858 | + } else{ |
|
856 | 859 | $contents .= "),\n"; |
857 | 860 | } |
858 | 861 | $r++; |