Conditions | 21 |
Paths | 237 |
Total Lines | 125 |
Code Lines | 78 |
Lines | 34 |
Ratio | 27.2 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
120 | function hash_pbkdf2($algo, $password, $salt, $iterations, $length = 0, $raw_output = FALSE) |
||
121 | { |
||
122 | if ( ! in_array($algo, hash_algos(), TRUE)) |
||
123 | { |
||
124 | trigger_error('hash_pbkdf2(): Unknown hashing algorithm: '.$algo, E_USER_WARNING); |
||
125 | return FALSE; |
||
126 | } |
||
127 | |||
128 | View Code Duplication | if (($type = gettype($iterations)) !== 'integer') |
|
129 | { |
||
130 | if ($type === 'object' && method_exists($iterations, '__toString')) |
||
131 | { |
||
132 | $iterations = (string) $iterations; |
||
133 | } |
||
134 | |||
135 | if (is_string($iterations) && is_numeric($iterations)) |
||
136 | { |
||
137 | $iterations = (int) $iterations; |
||
138 | } |
||
139 | else |
||
140 | { |
||
141 | trigger_error('hash_pbkdf2() expects parameter 4 to be long, '.$type.' given', E_USER_WARNING); |
||
142 | return NULL; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | if ($iterations < 1) |
||
147 | { |
||
148 | trigger_error('hash_pbkdf2(): Iterations must be a positive integer: '.$iterations, E_USER_WARNING); |
||
149 | return FALSE; |
||
150 | } |
||
151 | |||
152 | View Code Duplication | if (($type = gettype($length)) !== 'integer') |
|
153 | { |
||
154 | if ($type === 'object' && method_exists($length, '__toString')) |
||
155 | { |
||
156 | $length = (string) $length; |
||
157 | } |
||
158 | |||
159 | if (is_string($length) && is_numeric($length)) |
||
160 | { |
||
161 | $length = (int) $length; |
||
162 | } |
||
163 | else |
||
164 | { |
||
165 | trigger_error('hash_pbkdf2() expects parameter 5 to be long, '.$type.' given', E_USER_WARNING); |
||
166 | return NULL; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if ($length < 0) |
||
171 | { |
||
172 | trigger_error('hash_pbkdf2(): Length must be greater than or equal to 0: '.$length, E_USER_WARNING); |
||
173 | return FALSE; |
||
174 | } |
||
175 | |||
176 | $hash_length = strlen(hash($algo, NULL, TRUE)); |
||
177 | empty($length) && $length = $hash_length; |
||
178 | |||
179 | // Pre-hash password inputs longer than the algorithm's block size |
||
180 | // (i.e. prepare HMAC key) to mitigate potential DoS attacks. |
||
181 | static $block_sizes; |
||
182 | empty($block_sizes) && $block_sizes = array( |
||
183 | 'gost' => 32, |
||
184 | 'haval128,3' => 128, |
||
185 | 'haval160,3' => 128, |
||
186 | 'haval192,3' => 128, |
||
187 | 'haval224,3' => 128, |
||
188 | 'haval256,3' => 128, |
||
189 | 'haval128,4' => 128, |
||
190 | 'haval160,4' => 128, |
||
191 | 'haval192,4' => 128, |
||
192 | 'haval224,4' => 128, |
||
193 | 'haval256,4' => 128, |
||
194 | 'haval128,5' => 128, |
||
195 | 'haval160,5' => 128, |
||
196 | 'haval192,5' => 128, |
||
197 | 'haval224,5' => 128, |
||
198 | 'haval256,5' => 128, |
||
199 | 'md2' => 16, |
||
200 | 'md4' => 64, |
||
201 | 'md5' => 64, |
||
202 | 'ripemd128' => 64, |
||
203 | 'ripemd160' => 64, |
||
204 | 'ripemd256' => 64, |
||
205 | 'ripemd320' => 64, |
||
206 | 'salsa10' => 64, |
||
207 | 'salsa20' => 64, |
||
208 | 'sha1' => 64, |
||
209 | 'sha224' => 64, |
||
210 | 'sha256' => 64, |
||
211 | 'sha384' => 128, |
||
212 | 'sha512' => 128, |
||
213 | 'snefru' => 32, |
||
214 | 'snefru256' => 32, |
||
215 | 'tiger128,3' => 64, |
||
216 | 'tiger160,3' => 64, |
||
217 | 'tiger192,3' => 64, |
||
218 | 'tiger128,4' => 64, |
||
219 | 'tiger160,4' => 64, |
||
220 | 'tiger192,4' => 64, |
||
221 | 'whirlpool' => 64 |
||
222 | ); |
||
223 | |||
224 | if (isset($block_sizes[$algo]) && strlen($password) > $block_sizes[$algo]) |
||
225 | { |
||
226 | $password = hash($algo, $password, TRUE); |
||
227 | } |
||
228 | |||
229 | $hash = ''; |
||
230 | // Note: Blocks are NOT 0-indexed |
||
231 | for ($bc = ceil($length / $hash_length), $bi = 1; $bi <= $bc; $bi++) |
||
232 | { |
||
233 | $key = $derived_key = hash_hmac($algo, $salt.pack('N', $bi), $password, TRUE); |
||
234 | for ($i = 1; $i < $iterations; $i++) |
||
235 | { |
||
236 | $derived_key ^= $key = hash_hmac($algo, $key, $password, TRUE); |
||
237 | } |
||
238 | |||
239 | $hash .= $derived_key; |
||
240 | } |
||
241 | |||
242 | // This is not RFC-compatible, but we're aiming for natural PHP compatibility |
||
243 | return substr($raw_output ? $hash : bin2hex($hash), 0, $length); |
||
244 | } |
||
245 | } |
||
246 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.