Conditions | 36 |
Paths | 289 |
Total Lines | 111 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
24 | function password_hash($password, $algo, array $options = array()) { |
||
25 | if (!function_exists('crypt')) { |
||
26 | trigger_error("Crypt must be loaded for password_hash to function", E_USER_WARNING); |
||
27 | return null; |
||
28 | } |
||
29 | if (!is_string($password)) { |
||
30 | trigger_error("password_hash(): Password must be a string", E_USER_WARNING); |
||
31 | return null; |
||
32 | } |
||
33 | if (!is_int($algo)) { |
||
34 | trigger_error("password_hash() expects parameter 2 to be long, " . gettype($algo) . " given", E_USER_WARNING); |
||
35 | return null; |
||
36 | } |
||
37 | switch ($algo) { |
||
38 | case PASSWORD_BCRYPT: |
||
39 | // Note that this is a C constant, but not exposed to PHP, so we don't define it here. |
||
40 | $cost = 10; |
||
41 | if (isset($options['cost'])) { |
||
42 | $cost = $options['cost']; |
||
43 | if ($cost < 4 || $cost > 31) { |
||
44 | trigger_error(sprintf("password_hash(): Invalid bcrypt cost parameter specified: %d", $cost), E_USER_WARNING); |
||
45 | return null; |
||
46 | } |
||
47 | } |
||
48 | // The length of salt to generate |
||
49 | $raw_salt_len = 16; |
||
50 | // The length required in the final serialization |
||
51 | $required_salt_len = 22; |
||
52 | $hash_format = sprintf("$2y$%02d$", $cost); |
||
53 | break; |
||
54 | default: |
||
55 | trigger_error(sprintf("password_hash(): Unknown password hashing algorithm: %s", $algo), E_USER_WARNING); |
||
56 | return null; |
||
57 | } |
||
58 | if (isset($options['salt'])) { |
||
59 | switch (gettype($options['salt'])) { |
||
60 | case 'NULL': |
||
61 | case 'boolean': |
||
62 | case 'integer': |
||
63 | case 'double': |
||
64 | case 'string': |
||
65 | $salt = (string) $options['salt']; |
||
66 | break; |
||
67 | case 'object': |
||
|
|||
68 | if (method_exists($options['salt'], '__tostring')) { |
||
69 | $salt = (string) $options['salt']; |
||
70 | break; |
||
71 | } |
||
72 | case 'array': |
||
73 | case 'resource': |
||
74 | default: |
||
75 | trigger_error('password_hash(): Non-string salt parameter supplied', E_USER_WARNING); |
||
76 | return null; |
||
77 | } |
||
78 | if (strlen($salt) < $required_salt_len) { |
||
79 | trigger_error(sprintf("password_hash(): Provided salt is too short: %d expecting %d", strlen($salt), $required_salt_len), E_USER_WARNING); |
||
80 | return null; |
||
81 | } elseif (0 == preg_match('#^[a-zA-Z0-9./]+$#D', $salt)) { |
||
82 | $salt = str_replace('+', '.', base64_encode($salt)); |
||
83 | } |
||
84 | } else { |
||
85 | $buffer = ''; |
||
86 | $buffer_valid = false; |
||
87 | if (function_exists('mcrypt_create_iv') && !defined('PHALANGER')) { |
||
88 | $buffer = mcrypt_create_iv($raw_salt_len, MCRYPT_DEV_URANDOM); |
||
89 | if ($buffer) { |
||
90 | $buffer_valid = true; |
||
91 | } |
||
92 | } |
||
93 | if (!$buffer_valid && function_exists('openssl_random_pseudo_bytes')) { |
||
94 | $buffer = openssl_random_pseudo_bytes($raw_salt_len); |
||
95 | if ($buffer) { |
||
96 | $buffer_valid = true; |
||
97 | } |
||
98 | } |
||
99 | if (!$buffer_valid && is_readable('/dev/urandom')) { |
||
100 | $f = fopen('/dev/urandom', 'r'); |
||
101 | $read = strlen($buffer); |
||
102 | while ($read < $raw_salt_len) { |
||
103 | $buffer .= fread($f, $raw_salt_len - $read); |
||
104 | $read = strlen($buffer); |
||
105 | } |
||
106 | fclose($f); |
||
107 | if ($read >= $raw_salt_len) { |
||
108 | $buffer_valid = true; |
||
109 | } |
||
110 | } |
||
111 | if (!$buffer_valid || strlen($buffer) < $raw_salt_len) { |
||
112 | $bl = strlen($buffer); |
||
113 | for ($i = 0; $i < $raw_salt_len; $i++) { |
||
114 | if ($i < $bl) { |
||
115 | $buffer[$i] = $buffer[$i] ^ chr(mt_rand(0, 255)); |
||
116 | } else { |
||
117 | $buffer .= chr(mt_rand(0, 255)); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | $salt = str_replace('+', '.', base64_encode($buffer)); |
||
122 | } |
||
123 | $salt = substr($salt, 0, $required_salt_len); |
||
124 | |||
125 | $hash = $hash_format . $salt; |
||
126 | |||
127 | $ret = crypt($password, $hash); |
||
128 | |||
129 | if (!is_string($ret) || strlen($ret) <= 13) { |
||
130 | return false; |
||
131 | } |
||
132 | |||
133 | return $ret; |
||
134 | } |
||
135 | |||
223 |