Rev 71 | Rev 73 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 71 | Rev 72 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * ViaThinkSoft Modular Crypt Format 1.0 and vts_password_*() functions |
4 | * ViaThinkSoft Modular Crypt Format 1.0 and vts_password_*() functions |
5 | * Copyright 2023 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2023 Daniel Marschall, ViaThinkSoft |
6 | * Revision 2023-03-02 |
6 | * Revision 2023-03-03 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * |
13 | * |
14 | * Unless required by applicable law or agreed to in writing, software |
14 | * Unless required by applicable law or agreed to in writing, software |
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | * See the License for the specific language governing permissions and |
17 | * See the License for the specific language governing permissions and |
18 | * limitations under the License. |
18 | * limitations under the License. |
19 | */ |
19 | */ |
20 | 20 | ||
21 | /* |
21 | /* |
22 | 22 | ||
23 | The function vts_password_hash() replaces password_hash() |
23 | The function vts_password_hash() replaces password_hash() |
24 | and adds the ViaThinkSoft Modular Crypt Format 1.0 hash as well as |
24 | and adds the ViaThinkSoft Modular Crypt Format 1.0 hash as well as |
25 | all hashes from password_hash() and crypt(). |
25 | all hashes from password_hash() and crypt(). |
26 | 26 | ||
27 | The function vts_password_verify() replaces password_verify(). |
27 | The function vts_password_verify() replaces password_verify(). |
28 | 28 | ||
29 | ViaThinkSoft Modular Crypt Format 1.0 performs a simple hash or HMAC operation. |
29 | ViaThinkSoft Modular Crypt Format 1.0 performs a simple hash or HMAC operation. |
30 | No key derivation function or iterations are performed. |
30 | No key derivation function or iterations are performed. |
31 | Format: |
31 | Format: |
32 | $1.3.6.1.4.1.37476.3.0.1.1$a=<algo>,m=<mode>$<salt>$<hash> |
32 | $1.3.6.1.4.1.37476.3.0.1.1$a=<algo>,m=<mode>$<salt>$<hash> |
33 | where <algo> is any valid hash algorithm (name scheme of PHP hash_algos() preferred), e.g. |
33 | where <algo> is any valid hash algorithm (name scheme of PHP hash_algos() preferred), e.g. |
34 | sha3-512 |
34 | sha3-512 |
35 | sha3-384 |
35 | sha3-384 |
36 | sha3-256 |
36 | sha3-256 |
37 | sha3-224 |
37 | sha3-224 |
38 | sha512 |
38 | sha512 |
39 | sha512/256 |
39 | sha512/256 |
40 | sha512/224 |
40 | sha512/224 |
41 | sha384 |
41 | sha384 |
42 | sha256 |
42 | sha256 |
43 | sha224 |
43 | sha224 |
44 | sha1 |
44 | sha1 |
45 | md5 |
45 | md5 |
46 | Not supported are these hashes (because they have a special salt-handling and output their own crypt format): |
46 | Not supported are these hashes (because they have a special salt-handling and output their own crypt format): |
47 | bcrypt [Standardized crypt identifier 2, 2a, 2x, 2y] |
47 | bcrypt [Standardized crypt identifier 2, 2a, 2x, 2y] |
48 | argon2i [Crypt identifier argon2i, not standardized] |
48 | argon2i [Crypt identifier argon2i, not standardized] |
49 | argon2id [Crypt identifier argon2i, not standardized] |
49 | argon2id [Crypt identifier argon2i, not standardized] |
50 | Valid <mode> : |
50 | Valid <mode> : |
51 | sp = salt + password |
51 | sp = salt + password |
52 | ps = password + salt |
52 | ps = password + salt |
53 | sps = salt + password + salt |
53 | sps = salt + password + salt |
54 | hmac = HMAC (salt is the key) |
54 | hmac = HMAC (salt is the key) |
55 | pbkdf2 = PBKDF2-HMAC (Additional param i= contains the number of iterations) |
55 | pbkdf2 = PBKDF2-HMAC (Additional param i= contains the number of iterations) |
56 | Like most Crypt-hashes, <salt> and <hash> are Radix64 coded |
56 | Like most Crypt-hashes, <salt> and <hash> are Radix64 coded |
57 | with alphabet './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' and no padding. |
57 | with alphabet './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' and no padding. |
58 | Link to the online specification: |
58 | Link to the online specification: |
59 | https://oidplus.viathinksoft.com/oidplus/?goto=oid%3A1.3.6.1.4.1.37476.3.0.1.1 |
59 | https://oidplus.viathinksoft.com/oidplus/?goto=oid%3A1.3.6.1.4.1.37476.3.0.1.1 |
60 | Reference implementation in PHP: |
60 | Reference implementation in PHP: |
61 | https://github.com/danielmarschall/php_utils/blob/master/vts_crypt.inc.php |
61 | https://github.com/danielmarschall/php_utils/blob/master/vts_crypt.inc.php |
62 | 62 | ||
63 | */ |
63 | */ |
64 | 64 | ||
65 | require_once __DIR__ . '/misc_functions.inc.php'; |
65 | require_once __DIR__ . '/misc_functions.inc.php'; |
66 | 66 | ||
67 | define('OID_MCF_VTS_V1', '1.3.6.1.4.1.37476.3.0.1.1'); // { iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 37476 specifications(3) misc(0) modular-crypt-format(1) vts-crypt-v1(1) } |
67 | define('OID_MCF_VTS_V1', '1.3.6.1.4.1.37476.3.0.1.1'); // { iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 37476 specifications(3) misc(0) modular-crypt-format(1) vts-crypt-v1(1) } |
68 | 68 | ||
69 | // Valid algorithms for vts_password_hash(): |
69 | // Valid algorithms for vts_password_hash(): |
70 | define('PASSWORD_STD_DES', 'std-des'); // Algorithm from crypt() |
70 | define('PASSWORD_STD_DES', 'std-des'); // Algorithm from crypt() |
71 | define('PASSWORD_EXT_DES', 'ext-des'); // Algorithm from crypt() |
71 | define('PASSWORD_EXT_DES', 'ext-des'); // Algorithm from crypt() |
72 | define('PASSWORD_MD5', 'md5'); // Algorithm from crypt() |
72 | define('PASSWORD_MD5', 'md5'); // Algorithm from crypt() |
73 | define('PASSWORD_BLOWFISH', 'blowfish'); // Algorithm from crypt() |
73 | define('PASSWORD_BLOWFISH', 'blowfish'); // Algorithm from crypt() |
74 | define('PASSWORD_SHA256', 'sha256'); // Algorithm from crypt() |
74 | define('PASSWORD_SHA256', 'sha256'); // Algorithm from crypt() |
75 | define('PASSWORD_SHA512', 'sha512'); // Algorithm from crypt() |
75 | define('PASSWORD_SHA512', 'sha512'); // Algorithm from crypt() |
76 | define('PASSWORD_VTS_MCF1', OID_MCF_VTS_V1); // Algorithm by ViaThinkSoft |
76 | define('PASSWORD_VTS_MCF1', OID_MCF_VTS_V1); // Algorithm by ViaThinkSoft |
77 | // Other valid values (already defined in PHP): |
77 | // Other valid values (already defined in PHP): |
78 | // - PASSWORD_DEFAULT |
78 | // - PASSWORD_DEFAULT |
79 | // - PASSWORD_BCRYPT |
79 | // - PASSWORD_BCRYPT |
80 | // - PASSWORD_ARGON2I |
80 | // - PASSWORD_ARGON2I |
81 | // - PASSWORD_ARGON2ID |
81 | // - PASSWORD_ARGON2ID |
82 | 82 | ||
83 | define('PASSWORD_VTS_MCF1_MODE_SP', 'sp'); // Salt+Password |
83 | define('PASSWORD_VTS_MCF1_MODE_SP', 'sp'); // Salt+Password |
84 | define('PASSWORD_VTS_MCF1_MODE_PS', 'ps'); // Password+Salt |
84 | define('PASSWORD_VTS_MCF1_MODE_PS', 'ps'); // Password+Salt |
85 | define('PASSWORD_VTS_MCF1_MODE_SPS', 'sps'); // Salt+Password+Salt |
85 | define('PASSWORD_VTS_MCF1_MODE_SPS', 'sps'); // Salt+Password+Salt |
86 | define('PASSWORD_VTS_MCF1_MODE_HMAC', 'hmac'); // HMAC |
86 | define('PASSWORD_VTS_MCF1_MODE_HMAC', 'hmac'); // HMAC |
87 | define('PASSWORD_VTS_MCF1_MODE_PBKDF2', 'pbkdf2'); // PBKDF2-HMAC |
87 | define('PASSWORD_VTS_MCF1_MODE_PBKDF2', 'pbkdf2'); // PBKDF2-HMAC |
88 | 88 | ||
89 | define('PASSWORD_EXT_DES_DEFAULT_ITERATIONS', 725); |
89 | define('PASSWORD_EXT_DES_DEFAULT_ITERATIONS', 725); |
90 | define('PASSWORD_BLOWFISH_DEFAULT_COST', 10); |
90 | define('PASSWORD_BLOWFISH_DEFAULT_COST', 10); |
91 | define('PASSWORD_SHA256_DEFAULT_ROUNDS', 5000); |
91 | define('PASSWORD_SHA256_DEFAULT_ROUNDS', 5000); |
92 | define('PASSWORD_SHA512_DEFAULT_ROUNDS', 5000); |
92 | define('PASSWORD_SHA512_DEFAULT_ROUNDS', 5000); |
93 | define('PASSWORD_VTS_MCF1_DEFAULT_ALGO', 'sha3-512'); // any value in hash_algos(), NOT vts_hash_algos() |
93 | define('PASSWORD_VTS_MCF1_DEFAULT_ALGO', 'sha3-512'); // any value in hash_algos(), NOT vts_hash_algos() |
94 | define('PASSWORD_VTS_MCF1_DEFAULT_MODE', PASSWORD_VTS_MCF1_MODE_PS); |
94 | define('PASSWORD_VTS_MCF1_DEFAULT_MODE', PASSWORD_VTS_MCF1_MODE_PS); |
95 | define('PASSWORD_VTS_MCF1_DEFAULT_ITERATIONS', 0); // only for mode=pbkdf2. 0=Default, depending on algo |
95 | define('PASSWORD_VTS_MCF1_DEFAULT_ITERATIONS', 0); // only for mode=pbkdf2. 0=Default, depending on algo |
96 | 96 | ||
97 | // --- Part 1: Modular Crypt Format encode/decode |
97 | // --- Part 1: Modular Crypt Format encode/decode |
98 | 98 | ||
99 | function crypt_modular_format_encode($id, $bin_salt, $bin_hash, $params=null) { |
99 | function crypt_modular_format_encode($id, $bin_salt, $bin_hash, $params=null) { |
100 | // $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]] |
100 | // $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]] |
101 | $out = '$'.$id; |
101 | $out = '$'.$id; |
102 | if (!is_null($params)) { |
102 | if (!is_null($params)) { |
103 | $ary_params = array(); |
103 | $ary_params = array(); |
104 | foreach ($params as $name => $value) { |
104 | foreach ($params as $name => $value) { |
105 | $ary_params[] = "$name=$value"; |
105 | $ary_params[] = "$name=$value"; |
106 | } |
106 | } |
107 | $out .= '$'.implode(',',$ary_params); |
107 | $out .= '$'.implode(',',$ary_params); |
108 | } |
108 | } |
109 | $out .= '$'.crypt_radix64_encode($bin_salt); |
109 | $out .= '$'.crypt_radix64_encode($bin_salt); |
110 | $out .= '$'.crypt_radix64_encode($bin_hash); |
110 | $out .= '$'.crypt_radix64_encode($bin_hash); |
111 | return $out; |
111 | return $out; |
112 | } |
112 | } |
113 | 113 | ||
114 | function crypt_modular_format_decode($mcf) { |
114 | function crypt_modular_format_decode($mcf) { |
115 | $ary = explode('$', $mcf); |
115 | $ary = explode('$', $mcf); |
116 | 116 | ||
117 | $dummy = array_shift($ary); |
117 | $dummy = array_shift($ary); |
118 | if ($dummy !== '') return false; |
118 | if ($dummy !== '') return false; |
119 | 119 | ||
120 | $dummy = array_shift($ary); |
120 | $dummy = array_shift($ary); |
121 | $id = $dummy; |
121 | $id = $dummy; |
122 | 122 | ||
123 | $params = array(); |
123 | $params = array(); |
124 | $dummy = array_shift($ary); |
124 | $dummy = array_shift($ary); |
125 | if (strpos($dummy, '=') !== false) { |
125 | if (strpos($dummy, '=') !== false) { |
126 | $params_ary = explode(',',$dummy); |
126 | $params_ary = explode(',',$dummy); |
127 | foreach ($params_ary as $param) { |
127 | foreach ($params_ary as $param) { |
128 | $bry = explode('=', $param, 2); |
128 | $bry = explode('=', $param, 2); |
129 | if (count($bry) > 1) { |
129 | if (count($bry) > 1) { |
130 | $params[$bry[0]] = $bry[1]; |
130 | $params[$bry[0]] = $bry[1]; |
131 | } |
131 | } |
132 | } |
132 | } |
133 | } else { |
133 | } else { |
134 | array_unshift($ary, $dummy); |
134 | array_unshift($ary, $dummy); |
135 | } |
135 | } |
136 | 136 | ||
137 | if (count($ary) > 1) { |
137 | if (count($ary) > 1) { |
138 | $dummy = array_shift($ary); |
138 | $dummy = array_shift($ary); |
139 | $bin_salt = crypt_radix64_decode($dummy); |
139 | $bin_salt = crypt_radix64_decode($dummy); |
140 | } else { |
140 | } else { |
141 | $bin_salt = ''; |
141 | $bin_salt = ''; |
142 | } |
142 | } |
143 | 143 | ||
144 | $dummy = array_shift($ary); |
144 | $dummy = array_shift($ary); |
145 | $bin_hash = crypt_radix64_decode($dummy); |
145 | $bin_hash = crypt_radix64_decode($dummy); |
146 | 146 | ||
- | 147 | return array('id' => $id, |
|
- | 148 | 'salt' => $bin_salt, |
|
- | 149 | 'hash' => $bin_hash, |
|
147 | return array('id' => $id, 'salt' => $bin_salt, 'hash' => $bin_hash, 'params' => $params); |
150 | 'params' => $params); |
148 | } |
151 | } |
149 | 152 | ||
150 | // --- Part 2: ViaThinkSoft Modular Crypt Format 1.0 |
153 | // --- Part 2: ViaThinkSoft Modular Crypt Format 1.0 |
151 | 154 | ||
152 | function vts_crypt_version($hash) { |
155 | function vts_crypt_version($hash) { |
153 | if (str_starts_with($hash, '$'.OID_MCF_VTS_V1.'$')) { |
156 | if (str_starts_with($hash, '$'.OID_MCF_VTS_V1.'$')) { |
154 | return '1'; |
157 | return '1'; |
155 | } else { |
158 | } else { |
156 | return '0'; |
159 | return '0'; |
157 | } |
160 | } |
158 | } |
161 | } |
159 | 162 | ||
160 | function _default_iterations($algo, $userland) { |
- | |
161 | if ($userland) { |
- | |
162 | return 100; // because the userland implementation is EXTREMELY slow, we must choose a small value, sorry... |
- | |
163 | } else { |
- | |
164 | // Recommendations taken from https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#pbkdf2 |
- | |
165 | // Note that hash_pbkdf2() implements PBKDF2-HMAC-* |
- | |
166 | if ($algo == 'sha3-512') return 100000; |
- | |
167 | else if ($algo == 'sha3-384') return 100000; |
- | |
168 | else if ($algo == 'sha3-256') return 100000; |
- | |
169 | else if ($algo == 'sha3-224') return 100000; |
- | |
170 | else if ($algo == 'sha512') return 210000; // value by owasp.org cheatcheat (28 February 2023) |
- | |
171 | else if ($algo == 'sha512/256') return 210000; // value by owasp.org cheatcheat (28 February 2023) |
- | |
172 | else if ($algo == 'sha512/224') return 210000; // value by owasp.org cheatcheat (28 February 2023) |
- | |
173 | else if ($algo == 'sha384') return 600000; |
- | |
174 | else if ($algo == 'sha256') return 600000; // value by owasp.org cheatcheat (28 February 2023) |
- | |
175 | else if ($algo == 'sha224') return 600000; |
- | |
176 | else if ($algo == 'sha1') return 1300000; // value by owasp.org cheatcheat (28 February 2023) |
- | |
177 | else if ($algo == 'md5') return 5000000; |
- | |
178 | else return 5000; |
- | |
179 | } |
- | |
180 | } |
- | |
181 | - | ||
182 | function vts_crypt_hash($algo, $str_password, $str_salt, $ver='1', $mode=PASSWORD_VTS_MCF1_DEFAULT_MODE, $iterations=PASSWORD_VTS_MCF1_DEFAULT_ITERATIONS) { |
163 | function vts_crypt_hash($algo, $str_password, $str_salt, $ver='1', $mode=PASSWORD_VTS_MCF1_DEFAULT_MODE, $iterations=PASSWORD_VTS_MCF1_DEFAULT_ITERATIONS) { |
183 | if ($ver == '1') { |
164 | if ($ver == '1') { |
184 | if ($mode == PASSWORD_VTS_MCF1_MODE_SP) { |
165 | if ($mode == PASSWORD_VTS_MCF1_MODE_SP) { |
185 | $payload = $str_salt.$str_password; |
166 | $payload = $str_salt.$str_password; |
186 | if (!hash_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash')) { |
- | |
187 | $bits = explode('-',$algo)[1]; |
- | |
188 | $bin_hash = \bb\Sha3\Sha3::hash($payload, $bits, true); |
- | |
189 | } else { |
- | |
190 | $bin_hash = hash($algo, $payload, true); |
167 | $bin_hash = hash_ex($algo, $payload, true); |
191 | } |
- | |
192 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_PS) { |
168 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_PS) { |
193 | $payload = $str_password.$str_salt; |
169 | $payload = $str_password.$str_salt; |
194 | if (!hash_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash')) { |
- | |
195 | $bits = explode('-',$algo)[1]; |
- | |
196 | $bin_hash = \bb\Sha3\Sha3::hash($payload, $bits, true); |
- | |
197 | } else { |
- | |
198 | $bin_hash = hash($algo, $payload, true); |
170 | $bin_hash = hash_ex($algo, $payload, true); |
199 | } |
- | |
200 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_SPS) { |
171 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_SPS) { |
201 | $payload = $str_salt.$str_password.$str_salt; |
172 | $payload = $str_salt.$str_password.$str_salt; |
202 | if (!hash_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash')) { |
- | |
203 | $bits = explode('-',$algo)[1]; |
- | |
204 | $bin_hash = \bb\Sha3\Sha3::hash($payload, $bits, true); |
- | |
205 | } else { |
- | |
206 | $bin_hash = hash($algo, $payload, true); |
173 | $bin_hash = hash_ex($algo, $payload, true); |
207 | } |
- | |
208 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_HMAC) { |
174 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_HMAC) { |
209 | if (!hash_hmac_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash_hmac')) { |
- | |
210 | $bits = explode('-',$algo)[1]; |
- | |
211 | $bin_hash = \bb\Sha3\Sha3::hash_hmac($str_password, $str_salt, $bits, true); |
- | |
212 | } else { |
- | |
213 | $bin_hash = hash_hmac($algo, $str_password, $str_salt, true); |
175 | $bin_hash = hash_hmac_ex($algo, $str_password, $str_salt, true); |
214 | } |
- | |
215 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
176 | } else if ($mode == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
216 | if (!hash_pbkdf2_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash_pbkdf2')) { |
- | |
217 | if ($iterations == 0/*default*/) { |
- | |
218 | $iterations = _default_iterations($algo, true); |
- | |
219 | } |
- | |
220 | $bits = explode('-',$algo)[1]; |
- | |
221 | $bin_hash = \bb\Sha3\Sha3::hash_pbkdf2($str_password, $str_salt, $iterations, $bits, 0, true); |
- | |
222 | } else { |
- | |
223 | if ($iterations == 0/*default*/) { |
- | |
224 | $iterations = _default_iterations($algo, false); |
- | |
225 | } |
- | |
226 | $bin_hash = hash_pbkdf2($algo, $str_password, $str_salt, $iterations, 0, true); |
177 | $bin_hash = hash_pbkdf2_ex($algo, $str_password, $str_salt, $iterations, 0, true); |
227 | } |
- | |
228 | } else { |
178 | } else { |
229 | throw new Exception("Invalid VTS crypt version 1 mode. Expect sp, ps, sps, hmac, or pbkdf2."); |
179 | throw new Exception("Invalid VTS crypt version 1 mode. Expect sp, ps, sps, hmac, or pbkdf2."); |
230 | } |
180 | } |
231 | $bin_salt = $str_salt; |
181 | $bin_salt = $str_salt; |
232 | $params = array(); |
182 | $params = array(); |
233 | $params['a'] = $algo; |
183 | $params['a'] = $algo; |
234 | $params['m'] = $mode; |
184 | $params['m'] = $mode; |
235 | if ($mode == PASSWORD_VTS_MCF1_MODE_PBKDF2) $params['i'] = $iterations; |
185 | if ($mode == PASSWORD_VTS_MCF1_MODE_PBKDF2) $params['i'] = $iterations; |
236 | return crypt_modular_format_encode(OID_MCF_VTS_V1, $bin_salt, $bin_hash, $params); |
186 | return crypt_modular_format_encode(OID_MCF_VTS_V1, $bin_salt, $bin_hash, $params); |
237 | } else { |
187 | } else { |
238 | throw new Exception("Invalid VTS crypt version, expect 1."); |
188 | throw new Exception("Invalid VTS crypt version, expect 1."); |
239 | } |
189 | } |
240 | } |
190 | } |
241 | 191 | ||
242 | function vts_crypt_verify($password, $hash): bool { |
192 | function vts_crypt_verify($password, $hash): bool { |
243 | $ver = vts_crypt_version($hash); |
193 | $ver = vts_crypt_version($hash); |
244 | if ($ver == '1') { |
194 | if ($ver == '1') { |
245 | // Decode the MCF hash parameters |
195 | // Decode the MCF hash parameters |
246 | $data = crypt_modular_format_decode($hash); |
196 | $data = crypt_modular_format_decode($hash); |
247 | if ($data === false) throw new Exception('Invalid auth key'); |
197 | if ($data === false) throw new Exception('Invalid auth key'); |
248 | $id = $data['id']; |
198 | $id = $data['id']; |
249 | $bin_salt = $data['salt']; |
199 | $bin_salt = $data['salt']; |
250 | $bin_hash = $data['hash']; |
200 | $bin_hash = $data['hash']; |
251 | $params = $data['params']; |
201 | $params = $data['params']; |
252 | 202 | ||
253 | if (!isset($params['a'])) throw new Exception('Param "a" (algo) missing'); |
203 | if (!isset($params['a'])) throw new Exception('Param "a" (algo) missing'); |
254 | $algo = $params['a']; |
204 | $algo = $params['a']; |
255 | 205 | ||
256 | if (!isset($params['m'])) throw new Exception('Param "m" (mode) missing'); |
206 | if (!isset($params['m'])) throw new Exception('Param "m" (mode) missing'); |
257 | $mode = $params['m']; |
207 | $mode = $params['m']; |
258 | 208 | ||
259 | if ($mode == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
209 | if ($mode == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
260 | if (!isset($params['i'])) throw new Exception('Param "i" (iterations) missing'); |
210 | if (!isset($params['i'])) throw new Exception('Param "i" (iterations) missing'); |
261 | $iterations = $params['i']; |
211 | $iterations = $params['i']; |
262 | } else { |
212 | } else { |
263 | $iterations = 0; |
213 | $iterations = 0; |
264 | } |
214 | } |
265 | 215 | ||
266 | // Create a VTS MCF 1.0 hash based on the parameters of $hash and the password $password |
216 | // Create a VTS MCF 1.0 hash based on the parameters of $hash and the password $password |
267 | $calc_authkey_1 = vts_crypt_hash($algo, $password, $bin_salt, $ver, $mode, $iterations); |
217 | $calc_authkey_1 = vts_crypt_hash($algo, $password, $bin_salt, $ver, $mode, $iterations); |
268 | 218 | ||
269 | // We rewrite the MCF to make sure that they match (if params have the wrong order) |
219 | // We rewrite the MCF to make sure that they match (if params have the wrong order) |
270 | $calc_authkey_2 = crypt_modular_format_encode($id, $bin_salt, $bin_hash, $params); |
220 | $calc_authkey_2 = crypt_modular_format_encode($id, $bin_salt, $bin_hash, $params); |
271 | 221 | ||
272 | return hash_equals($calc_authkey_1, $calc_authkey_2); |
222 | return hash_equals($calc_authkey_1, $calc_authkey_2); |
273 | } else { |
223 | } else { |
274 | throw new Exception("Invalid VTS crypt version, expect 1."); |
224 | throw new Exception("Invalid VTS crypt version, expect 1."); |
275 | } |
225 | } |
276 | } |
226 | } |
277 | 227 | ||
278 | // --- Part 3: Replacement of vts_password_*() functions |
228 | // --- Part 3: Replacement of vts_password_*() functions |
279 | 229 | ||
280 | /** |
230 | /** |
281 | * This function replaces password_algos() by extending it with |
231 | * This function replaces password_algos() by extending it with |
282 | * password hashes that are implemented in vts_password_hash(). |
232 | * password hashes that are implemented in vts_password_hash(). |
283 | * @return array of hashes that can be used in vts_password_hash(). |
233 | * @return array of hashes that can be used in vts_password_hash(). |
284 | */ |
234 | */ |
285 | function vts_password_algos() { |
235 | function vts_password_algos() { |
286 | $hashes = password_algos(); |
236 | $hashes = password_algos(); |
287 | $hashes[] = PASSWORD_STD_DES; // Algorithm from crypt() |
237 | $hashes[] = PASSWORD_STD_DES; // Algorithm from crypt() |
288 | $hashes[] = PASSWORD_EXT_DES; // Algorithm from crypt() |
238 | $hashes[] = PASSWORD_EXT_DES; // Algorithm from crypt() |
289 | $hashes[] = PASSWORD_MD5; // Algorithm from crypt() |
239 | $hashes[] = PASSWORD_MD5; // Algorithm from crypt() |
290 | $hashes[] = PASSWORD_BLOWFISH; // Algorithm from crypt() |
240 | $hashes[] = PASSWORD_BLOWFISH; // Algorithm from crypt() |
291 | $hashes[] = PASSWORD_SHA256; // Algorithm from crypt() |
241 | $hashes[] = PASSWORD_SHA256; // Algorithm from crypt() |
292 | $hashes[] = PASSWORD_SHA512; // Algorithm from crypt() |
242 | $hashes[] = PASSWORD_SHA512; // Algorithm from crypt() |
293 | $hashes[] = PASSWORD_VTS_MCF1; // Algorithm by ViaThinkSoft |
243 | $hashes[] = PASSWORD_VTS_MCF1; // Algorithm by ViaThinkSoft |
294 | return $hashes; |
244 | return $hashes; |
295 | } |
245 | } |
296 | 246 | ||
297 | /** vts_password_get_info() is the same as password_get_info(), |
247 | /** vts_password_get_info() is the same as password_get_info(), |
298 | * but it adds the crypt() and ViaThinkSoft MCF 1.0 algos which can be |
248 | * but it adds the crypt() and ViaThinkSoft MCF 1.0 algos which can be |
299 | * produced by vts_password_hash() |
249 | * produced by vts_password_hash() |
300 | * @param string $hash Hash created by vts_password_hash(), password_hash(), or crypt(). |
250 | * @param string $hash Hash created by vts_password_hash(), password_hash(), or crypt(). |
301 | * @return array Same output like password_get_info(). |
251 | * @return array Same output like password_get_info(). |
302 | */ |
252 | */ |
303 | function vts_password_get_info($hash) { |
253 | function vts_password_get_info($hash) { |
304 | if (vts_crypt_version($hash) == '1') { |
254 | if (vts_crypt_version($hash) == '1') { |
305 | // OID_MCF_VTS_V1 |
255 | // OID_MCF_VTS_V1 |
306 | $mcf = crypt_modular_format_decode($hash); |
256 | $mcf = crypt_modular_format_decode($hash); |
307 | 257 | ||
308 | //$options['salt_length'] = strlen($mcf['salt']); // Note: salt_length is not a MCF option! It's just a hint for vts_password_hash() |
258 | //$options['salt_length'] = strlen($mcf['salt']); // Note: salt_length is not a MCF option! It's just a hint for vts_password_hash() |
309 | 259 | ||
310 | if (!isset($mcf['params']['a'])) throw new Exception('Param "a" (algo) missing'); |
260 | if (!isset($mcf['params']['a'])) throw new Exception('Param "a" (algo) missing'); |
311 | $options['algo'] = $mcf['params']['a']; |
261 | $options['algo'] = $mcf['params']['a']; |
312 | 262 | ||
313 | if (!isset($mcf['params']['m'])) throw new Exception('Param "m" (mode) missing'); |
263 | if (!isset($mcf['params']['m'])) throw new Exception('Param "m" (mode) missing'); |
314 | $options['mode'] = $mcf['params']['m']; |
264 | $options['mode'] = $mcf['params']['m']; |
315 | 265 | ||
316 | if ($options['mode'] == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
266 | if ($options['mode'] == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
317 | if (!isset($mcf['params']['i'])) throw new Exception('Param "i" (iterations) missing'); |
267 | if (!isset($mcf['params']['i'])) throw new Exception('Param "i" (iterations) missing'); |
318 | $options['iterations'] = (int)$mcf['params']['i']; |
268 | $options['iterations'] = (int)$mcf['params']['i']; |
319 | } |
269 | } |
320 | 270 | ||
321 | return array( |
271 | return array( |
322 | "algo" => PASSWORD_VTS_MCF1, |
272 | "algo" => PASSWORD_VTS_MCF1, |
323 | "algoName" => "vts-mcf-v1", |
273 | "algoName" => "vts-mcf-v1", |
324 | "options" => $options |
274 | "options" => $options |
325 | ); |
275 | ); |
326 | } else if (!str_starts_with($hash, '$') && (strlen($hash) == 13)) { |
276 | } else if (!str_starts_with($hash, '$') && (strlen($hash) == 13)) { |
327 | // PASSWORD_STD_DES |
277 | // PASSWORD_STD_DES |
328 | return array( |
278 | return array( |
329 | "algo" => PASSWORD_STD_DES, |
279 | "algo" => PASSWORD_STD_DES, |
330 | "algoName" => "std-des", |
280 | "algoName" => "std-des", |
331 | "options" => array( |
281 | "options" => array( |
332 | // None |
282 | // None |
333 | ) |
283 | ) |
334 | ); |
284 | ); |
335 | } else if (str_starts_with($hash, '_') && (strlen($hash) == 20)) { |
285 | } else if (str_starts_with($hash, '_') && (strlen($hash) == 20)) { |
336 | // PASSWORD_EXT_DES |
286 | // PASSWORD_EXT_DES |
337 | return array( |
287 | return array( |
338 | "algo" => PASSWORD_EXT_DES, |
288 | "algo" => PASSWORD_EXT_DES, |
339 | "algoName" => "ext-des", |
289 | "algoName" => "ext-des", |
340 | "options" => array( |
290 | "options" => array( |
341 | "iterations" => (int)base64_int_decode(substr($hash,1,4)) |
291 | "iterations" => (int)base64_int_decode(substr($hash,1,4)) |
342 | ) |
292 | ) |
343 | ); |
293 | ); |
344 | } else if (str_starts_with($hash, '$1$')) { |
294 | } else if (str_starts_with($hash, '$1$')) { |
345 | // PASSWORD_MD5 |
295 | // PASSWORD_MD5 |
346 | return array( |
296 | return array( |
347 | "algo" => PASSWORD_MD5, |
297 | "algo" => PASSWORD_MD5, |
348 | "algoName" => "md5", |
298 | "algoName" => "md5", |
349 | "options" => array( |
299 | "options" => array( |
350 | // None |
300 | // None |
351 | ) |
301 | ) |
352 | ); |
302 | ); |
353 | } else if (str_starts_with($hash, '$2$') || str_starts_with($hash, '$2a$') || |
303 | } else if (str_starts_with($hash, '$2$') || str_starts_with($hash, '$2a$') || |
354 | str_starts_with($hash, '$2x$') || str_starts_with($hash, '$2y$')) { |
304 | str_starts_with($hash, '$2x$') || str_starts_with($hash, '$2y$')) { |
355 | // PASSWORD_BLOWFISH |
305 | // PASSWORD_BLOWFISH |
356 | return array( |
306 | return array( |
357 | "algo" => PASSWORD_BLOWFISH, |
307 | "algo" => PASSWORD_BLOWFISH, |
358 | "algoName" => "blowfish", |
308 | "algoName" => "blowfish", |
359 | "options" => array( |
309 | "options" => array( |
360 | "cost" => (int)ltrim(explode('$',$hash)[2],'0') |
310 | "cost" => (int)ltrim(explode('$',$hash)[2],'0') |
361 | ) |
311 | ) |
362 | ); |
312 | ); |
363 | } else if (str_starts_with($hash, '$5$')) { |
313 | } else if (str_starts_with($hash, '$5$')) { |
364 | // PASSWORD_SHA256 |
314 | // PASSWORD_SHA256 |
365 | return array( |
315 | return array( |
366 | "algo" => PASSWORD_SHA256, |
316 | "algo" => PASSWORD_SHA256, |
367 | "algoName" => "sha256", |
317 | "algoName" => "sha256", |
368 | "options" => array( |
318 | "options" => array( |
369 | 'rounds' => (int)str_replace('rounds=','',explode('$',$hash)[2]) |
319 | 'rounds' => (int)str_replace('rounds=','',explode('$',$hash)[2]) |
370 | ) |
320 | ) |
371 | ); |
321 | ); |
372 | } else if (str_starts_with($hash, '$6$')) { |
322 | } else if (str_starts_with($hash, '$6$')) { |
373 | // PASSWORD_SHA512 |
323 | // PASSWORD_SHA512 |
374 | return array( |
324 | return array( |
375 | "algo" => PASSWORD_SHA512, |
325 | "algo" => PASSWORD_SHA512, |
376 | "algoName" => "sha512", |
326 | "algoName" => "sha512", |
377 | "options" => array( |
327 | "options" => array( |
378 | 'rounds' => (int)str_replace('rounds=','',explode('$',$hash)[2]) |
328 | 'rounds' => (int)str_replace('rounds=','',explode('$',$hash)[2]) |
379 | ) |
329 | ) |
380 | ); |
330 | ); |
381 | } else { |
331 | } else { |
382 | // PASSWORD_DEFAULT |
332 | // PASSWORD_DEFAULT |
383 | // PASSWORD_BCRYPT |
333 | // PASSWORD_BCRYPT |
384 | // PASSWORD_ARGON2I |
334 | // PASSWORD_ARGON2I |
385 | // PASSWORD_ARGON2ID |
335 | // PASSWORD_ARGON2ID |
386 | return password_get_info($hash); |
336 | return password_get_info($hash); |
387 | } |
337 | } |
388 | } |
338 | } |
389 | 339 | ||
390 | /** This function extends password_hash() with the algorithms supported by crypt(). |
340 | /** This function extends password_hash() with the algorithms supported by crypt(). |
391 | * It also adds vts_crypt_hash() which implements the ViaThinkSoft Modular Crypt Format 1.0. |
341 | * It also adds vts_crypt_hash() which implements the ViaThinkSoft Modular Crypt Format 1.0. |
392 | * The result can be verified using vts_password_verify(). |
342 | * The result can be verified using vts_password_verify(). |
393 | * @param string $password to be hashed |
343 | * @param string $password to be hashed |
394 | * @param mixed $algo algorithm |
344 | * @param mixed $algo algorithm |
395 | * @param array $options options for the hashing algorithm |
345 | * @param array $options options for the hashing algorithm |
396 | * @return string Crypt style password hash |
346 | * @return string Crypt style password hash |
397 | */ |
347 | */ |
398 | function vts_password_hash($password, $algo, $options=array()): string { |
348 | function vts_password_hash($password, $algo, $options=array()): string { |
399 | $options = vts_password_fill_default_options($algo, $options); |
349 | $options = vts_password_fill_default_options($algo, $options); |
400 | 350 | ||
401 | $crypt_salt = null; |
351 | $crypt_salt = null; |
402 | if (($algo === PASSWORD_STD_DES) && defined('CRYPT_STD_DES')) { |
352 | if (($algo === PASSWORD_STD_DES) && defined('CRYPT_STD_DES')) { |
403 | // Standard DES-based hash with a two character salt from the alphabet "./0-9A-Za-z". Using invalid characters in the salt will cause crypt() to fail. |
353 | // Standard DES-based hash with a two character salt from the alphabet "./0-9A-Za-z". Using invalid characters in the salt will cause crypt() to fail. |
404 | $crypt_salt = des_compat_salt(2); |
354 | $crypt_salt = des_compat_salt(2); |
405 | } else if (($algo === PASSWORD_EXT_DES) && defined('CRYPT_EXT_DES')) { |
355 | } else if (($algo === PASSWORD_EXT_DES) && defined('CRYPT_EXT_DES')) { |
406 | // Extended DES-based hash. The "salt" is a 9-character string consisting of an underscore followed by 4 characters of iteration count and 4 characters of salt. Each of these 4-character strings encode 24 bits, least significant character first. The values 0 to 63 are encoded as ./0-9A-Za-z. Using invalid characters in the salt will cause crypt() to fail. |
356 | // Extended DES-based hash. The "salt" is a 9-character string consisting of an underscore followed by 4 characters of iteration count and 4 characters of salt. Each of these 4-character strings encode 24 bits, least significant character first. The values 0 to 63 are encoded as ./0-9A-Za-z. Using invalid characters in the salt will cause crypt() to fail. |
407 | $iterations = $options['iterations']; |
357 | $iterations = $options['iterations']; |
408 | $crypt_salt = '_' . base64_int_encode($iterations,4) . des_compat_salt(4); |
358 | $crypt_salt = '_' . base64_int_encode($iterations,4) . des_compat_salt(4); |
409 | } else if (($algo === PASSWORD_MD5) && defined('CRYPT_MD5')) { |
359 | } else if (($algo === PASSWORD_MD5) && defined('CRYPT_MD5')) { |
410 | // MD5 hashing with a twelve character salt starting with $1$ |
360 | // MD5 hashing with a twelve character salt starting with $1$ |
411 | $crypt_salt = '$1$'.des_compat_salt(12).'$'; |
361 | $crypt_salt = '$1$'.des_compat_salt(12).'$'; |
412 | } else if (($algo === PASSWORD_BLOWFISH) && defined('CRYPT_BLOWFISH')) { |
362 | } else if (($algo === PASSWORD_BLOWFISH) && defined('CRYPT_BLOWFISH')) { |
413 | // Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z". Using characters outside of this range in the salt will cause crypt() to return a zero-length string. The two digit cost parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithm and must be in range 04-31, values outside this range will cause crypt() to fail. "$2x$" hashes are potentially weak; "$2a$" hashes are compatible and mitigate this weakness. For new hashes, "$2y$" should be used. |
363 | // Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z". Using characters outside of this range in the salt will cause crypt() to return a zero-length string. The two digit cost parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithm and must be in range 04-31, values outside this range will cause crypt() to fail. "$2x$" hashes are potentially weak; "$2a$" hashes are compatible and mitigate this weakness. For new hashes, "$2y$" should be used. |
414 | $algo = '$2y$'; // most secure |
364 | $algo = '$2y$'; // most secure |
415 | $cost = $options['cost']; |
365 | $cost = $options['cost']; |
416 | $crypt_salt = $algo.str_pad($cost,2,'0',STR_PAD_LEFT).'$'.des_compat_salt(22).'$'; |
366 | $crypt_salt = $algo.str_pad($cost,2,'0',STR_PAD_LEFT).'$'.des_compat_salt(22).'$'; |
417 | } else if (($algo === PASSWORD_SHA256) && defined('CRYPT_SHA256')) { |
367 | } else if (($algo === PASSWORD_SHA256) && defined('CRYPT_SHA256')) { |
418 | // SHA-256 hash with a sixteen character salt prefixed with $5$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit. |
368 | // SHA-256 hash with a sixteen character salt prefixed with $5$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit. |
419 | $algo = '$5$'; |
369 | $algo = '$5$'; |
420 | $rounds = $options['rounds']; |
370 | $rounds = $options['rounds']; |
421 | $crypt_salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$'; |
371 | $crypt_salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$'; |
422 | } else if (($algo === PASSWORD_SHA512) && defined('CRYPT_SHA512')) { |
372 | } else if (($algo === PASSWORD_SHA512) && defined('CRYPT_SHA512')) { |
423 | // SHA-512 hash with a sixteen character salt prefixed with $6$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit. |
373 | // SHA-512 hash with a sixteen character salt prefixed with $6$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit. |
424 | $algo = '$6$'; |
374 | $algo = '$6$'; |
425 | $rounds = $options['rounds']; |
375 | $rounds = $options['rounds']; |
426 | $crypt_salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$'; |
376 | $crypt_salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$'; |
427 | } |
377 | } |
428 | 378 | ||
429 | if (!is_null($crypt_salt)) { |
379 | if (!is_null($crypt_salt)) { |
430 | // Algorithms: PASSWORD_STD_DES |
380 | // Algorithms: PASSWORD_STD_DES |
431 | // PASSWORD_EXT_DES |
381 | // PASSWORD_EXT_DES |
432 | // PASSWORD_MD5 |
382 | // PASSWORD_MD5 |
433 | // PASSWORD_BLOWFISH |
383 | // PASSWORD_BLOWFISH |
434 | // PASSWORD_SHA256 |
384 | // PASSWORD_SHA256 |
435 | // PASSWORD_SHA512 |
385 | // PASSWORD_SHA512 |
436 | $out = crypt($password, $crypt_salt); |
386 | $out = crypt($password, $crypt_salt); |
437 | if (strlen($out) < 13) throw new Exception("crypt() failed"); |
387 | if (strlen($out) < 13) throw new Exception("crypt() failed"); |
438 | return $out; |
388 | return $out; |
439 | } else if ($algo === PASSWORD_VTS_MCF1) { |
389 | } else if ($algo === PASSWORD_VTS_MCF1) { |
440 | // Algorithms: PASSWORD_VTS_MCF1 |
390 | // Algorithms: PASSWORD_VTS_MCF1 |
441 | $ver = '1'; |
391 | $ver = '1'; |
442 | $algo = $options['algo']; |
392 | $algo = $options['algo']; |
443 | $mode = $options['mode']; |
393 | $mode = $options['mode']; |
444 | $iterations = $options['iterations']; |
394 | $iterations = $options['iterations']; |
445 | $salt_len = isset($options['salt_length']) ? $options['salt_length'] : 50; // Note: salt_length is not a MCF option! It's just a hint for vts_password_hash() |
395 | $salt_len = isset($options['salt_length']) ? $options['salt_length'] : 50; // Note: salt_length is not a MCF option! It's just a hint for vts_password_hash() |
446 | $salt = random_bytes_ex($salt_len, true, true); |
396 | $salt = random_bytes_ex($salt_len, true, true); |
447 | return vts_crypt_hash($algo, $password, $salt, $ver, $mode, $iterations); |
397 | return vts_crypt_hash($algo, $password, $salt, $ver, $mode, $iterations); |
448 | } else { |
398 | } else { |
449 | // Algorithms: PASSWORD_DEFAULT |
399 | // Algorithms: PASSWORD_DEFAULT |
450 | // PASSWORD_BCRYPT |
400 | // PASSWORD_BCRYPT |
451 | // PASSWORD_ARGON2I |
401 | // PASSWORD_ARGON2I |
452 | // PASSWORD_ARGON2ID |
402 | // PASSWORD_ARGON2ID |
453 | return password_hash($password, $algo, $options); |
403 | return password_hash($password, $algo, $options); |
454 | } |
404 | } |
455 | } |
405 | } |
456 | 406 | ||
457 | /** This function replaces password_needs_rehash() by adding additional algorithms |
407 | /** This function replaces password_needs_rehash() by adding additional algorithms |
458 | * supported by vts_password_hash(). |
408 | * supported by vts_password_hash(). |
459 | * @param string $hash The current hash |
409 | * @param string $hash The current hash |
460 | * @param string|int|null $algo Desired new default algo |
410 | * @param string|int|null $algo Desired new default algo |
461 | * @param array $options Desired new default options |
411 | * @param array $options Desired new default options |
462 | * @return bool True if algo or options of the current hash don't match the current desired values ($algo and $options), otherwise false. |
412 | * @return bool True if algo or options of the current hash don't match the current desired values ($algo and $options), otherwise false. |
463 | */ |
413 | */ |
464 | function vts_password_needs_rehash($hash, $algo, $options=array()) { |
414 | function vts_password_needs_rehash($hash, $algo, $options=array()) { |
465 | $options = vts_password_fill_default_options($algo, $options); |
415 | $options = vts_password_fill_default_options($algo, $options); |
466 | 416 | ||
467 | $info = vts_password_get_info($hash); |
417 | $info = vts_password_get_info($hash); |
468 | $algo2 = $info['algo']; |
418 | $algo2 = $info['algo']; |
469 | $options2 = $info['options']; |
419 | $options2 = $info['options']; |
470 | 420 | ||
471 | // Check if algorithm matches |
421 | // Check if algorithm matches |
472 | if ($algo !== $algo2) return true; |
422 | if ($algo !== $algo2) return true; |
473 | 423 | ||
474 | if (vts_crypt_version($hash) == '1') { |
424 | if (vts_crypt_version($hash) == '1') { |
475 | if (isset($options['salt_length'])) { |
425 | if (isset($options['salt_length'])) { |
476 | // For VTS MCF 1.0, salt_length is a valid option for vts_password_hash(), |
426 | // For VTS MCF 1.0, salt_length is a valid option for vts_password_hash(), |
477 | // but it is not a valid option inside the MCF options |
427 | // but it is not a valid option inside the MCF options |
478 | // and it is not a valid option for vts_password_get_info(). |
428 | // and it is not a valid option for vts_password_get_info(). |
479 | unset($options['salt_length']); |
429 | unset($options['salt_length']); |
480 | } |
430 | } |
481 | 431 | ||
482 | // iterations=0 means: Default, depending on the algo |
432 | // iterations=0 means: Default, depending on the algo |
483 | if (($options2['mode'] == PASSWORD_VTS_MCF1_MODE_PBKDF2) && ($options['iterations'] == 0/*default*/)) { |
433 | if (($options['iterations'] == 0/*default*/) && ($options2['mode'] == PASSWORD_VTS_MCF1_MODE_PBKDF2)) { |
484 | $algo = $options2['algo']; |
434 | $algo = $options2['algo']; |
485 | $userland = !hash_pbkdf2_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash_pbkdf2'); |
435 | $userland = !hash_pbkdf2_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash_pbkdf2'); |
486 | $options['iterations'] = _default_iterations($algo, $userland); |
436 | $options['iterations'] = _vts_password_default_iterations($algo, $userland); |
487 | } |
437 | } |
488 | } |
438 | } |
489 | 439 | ||
490 | // Check if options match |
440 | // Check if options match |
491 | if (count($options) !== count($options2)) return true; |
441 | if (count($options) !== count($options2)) return true; |
492 | foreach ($options as $name => $val) { |
442 | foreach ($options as $name => $val) { |
493 | if ($options2[$name] != $val) return true; |
443 | if ($options2[$name] != $val) return true; |
494 | } |
444 | } |
495 | return false; |
445 | return false; |
496 | } |
446 | } |
497 | 447 | ||
498 | /** This function extends password_verify() by adding ViaThinkSoft Modular Crypt Format 1.0. |
448 | /** This function extends password_verify() by adding ViaThinkSoft Modular Crypt Format 1.0. |
499 | * @param string $password to be checked |
449 | * @param string $password to be checked |
500 | * @param string $hash Hash created by crypt(), password_hash(), or vts_password_hash(). |
450 | * @param string $hash Hash created by crypt(), password_hash(), or vts_password_hash(). |
501 | * @return bool true if password is valid |
451 | * @return bool true if password is valid |
502 | */ |
452 | */ |
503 | function vts_password_verify($password, $hash): bool { |
453 | function vts_password_verify($password, $hash): bool { |
504 | if (vts_crypt_version($hash) != '0') { |
454 | if (vts_crypt_version($hash) != '0') { |
505 | // Hash created by vts_password_hash(), or vts_crypt_hash() |
455 | // Hash created by vts_password_hash(), or vts_crypt_hash() |
506 | return vts_crypt_verify($password, $hash); |
456 | return vts_crypt_verify($password, $hash); |
507 | } else { |
457 | } else { |
508 | // Hash created by vts_password_hash(), password_hash(), or crypt() |
458 | // Hash created by vts_password_hash(), password_hash(), or crypt() |
509 | return password_verify($password, $hash); |
459 | return password_verify($password, $hash); |
510 | } |
460 | } |
511 | } |
461 | } |
512 | 462 | ||
- | 463 | // --- Part 4: Functions which include a fallback to a pure-PHP sha3 implementation (requires https://github.com/danielmarschall/php-sha3 ) |
|
- | 464 | ||
- | 465 | function hash_ex($algo, $data, $binary=false, $options=array()) { |
|
- | 466 | if (!hash_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash')) { |
|
- | 467 | $bits = explode('-',$algo)[1]; |
|
- | 468 | $hash = \bb\Sha3\Sha3::hash($data, $bits, $binary); |
|
- | 469 | } else { |
|
- | 470 | $hash = hash($algo, $data, $binary); |
|
- | 471 | } |
|
- | 472 | return $hash; |
|
- | 473 | } |
|
- | 474 | ||
- | 475 | function hash_hmac_ex($algo, $data, $key, $binary=false) { |
|
- | 476 | if (!hash_hmac_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash_hmac')) { |
|
- | 477 | $bits = explode('-',$algo)[1]; |
|
- | 478 | $hash = \bb\Sha3\Sha3::hash_hmac($data, $key, $bits, $binary); |
|
- | 479 | } else { |
|
- | 480 | $hash = hash_hmac($algo, $data, $key, $binary); |
|
- | 481 | } |
|
- | 482 | return $hash; |
|
- | 483 | } |
|
- | 484 | ||
- | 485 | function hash_pbkdf2_ex($algo, $password, $salt, &$iterations=0, $length=0, $binary=false) { |
|
- | 486 | if (!hash_pbkdf2_supported_natively($algo) && str_starts_with($algo, 'sha3-') && method_exists('\bb\Sha3\Sha3', 'hash_pbkdf2')) { |
|
- | 487 | if ($iterations == 0/*default*/) { |
|
- | 488 | $iterations = _vts_password_default_iterations($algo, true); |
|
- | 489 | } |
|
- | 490 | $bits = explode('-',$algo)[1]; |
|
- | 491 | $hash = \bb\Sha3\Sha3::hash_pbkdf2($password, $salt, $iterations, $bits, $length, $binary); |
|
- | 492 | } else { |
|
- | 493 | if ($iterations == 0/*default*/) { |
|
- | 494 | $iterations = _vts_password_default_iterations($algo, false); |
|
- | 495 | } |
|
- | 496 | $hash = hash_pbkdf2($algo, $password, $salt, $iterations, $length, $binary); |
|
- | 497 | } |
|
- | 498 | return $hash; |
|
- | 499 | } |
|
- | 500 | ||
513 | // --- Part 4: Useful functions required by the crypt-functions |
501 | // --- Part 5: Useful functions required by the crypt-functions |
514 | 502 | ||
515 | define('BASE64_RFC4648_ALPHABET', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'); |
503 | define('BASE64_RFC4648_ALPHABET', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/'); |
516 | define('BASE64_CRYPT_ALPHABET', './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'); |
504 | define('BASE64_CRYPT_ALPHABET', './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'); |
517 | 505 | ||
518 | function des_compat_salt($salt_len) { |
506 | function des_compat_salt($salt_len) { |
519 | if ($salt_len <= 0) return ''; |
507 | if ($salt_len <= 0) return ''; |
520 | $characters = BASE64_CRYPT_ALPHABET; |
508 | $characters = BASE64_CRYPT_ALPHABET; |
521 | $salt = ''; |
509 | $salt = ''; |
522 | $bytes = random_bytes_ex($salt_len, true, true); |
510 | $bytes = random_bytes_ex($salt_len, true, true); |
523 | for ($i=0; $i<$salt_len; $i++) { |
511 | for ($i=0; $i<$salt_len; $i++) { |
524 | $salt .= $characters[ord($bytes[$i]) % strlen($characters)]; |
512 | $salt .= $characters[ord($bytes[$i]) % strlen($characters)]; |
525 | } |
513 | } |
526 | return $salt; |
514 | return $salt; |
527 | } |
515 | } |
528 | 516 | ||
529 | function base64_int_encode($num, $len) { |
517 | function base64_int_encode($num, $len) { |
530 | // https://stackoverflow.com/questions/15534982/which-iteration-rules-apply-on-crypt-using-crypt-ext-des |
518 | // https://stackoverflow.com/questions/15534982/which-iteration-rules-apply-on-crypt-using-crypt-ext-des |
531 | $alphabet_raw = BASE64_CRYPT_ALPHABET; |
519 | $alphabet_raw = BASE64_CRYPT_ALPHABET; |
532 | $alphabet = str_split($alphabet_raw); |
520 | $alphabet = str_split($alphabet_raw); |
533 | $arr = array(); |
521 | $arr = array(); |
534 | $base = sizeof($alphabet); |
522 | $base = sizeof($alphabet); |
535 | while ($num) { |
523 | while ($num) { |
536 | $rem = $num % $base; |
524 | $rem = $num % $base; |
537 | $num = (int)($num / $base); |
525 | $num = (int)($num / $base); |
538 | $arr[] = $alphabet[$rem]; |
526 | $arr[] = $alphabet[$rem]; |
539 | } |
527 | } |
540 | $string = implode($arr); |
528 | $string = implode($arr); |
541 | return str_pad($string, $len, '.', STR_PAD_RIGHT); |
529 | return str_pad($string, $len, '.', STR_PAD_RIGHT); |
542 | } |
530 | } |
543 | 531 | ||
544 | function base64_int_decode($base64) { |
532 | function base64_int_decode($base64) { |
545 | $num = 0; |
533 | $num = 0; |
546 | for ($i=strlen($base64)-1;$i>=0;$i--) { |
534 | for ($i=strlen($base64)-1;$i>=0;$i--) { |
547 | $num += strpos(BASE64_CRYPT_ALPHABET, $base64[$i])*pow(strlen(BASE64_CRYPT_ALPHABET),$i); |
535 | $num += strpos(BASE64_CRYPT_ALPHABET, $base64[$i])*pow(strlen(BASE64_CRYPT_ALPHABET),$i); |
548 | } |
536 | } |
549 | return $num; |
537 | return $num; |
550 | } |
538 | } |
551 | 539 | ||
552 | function crypt_radix64_encode($str) { |
540 | function crypt_radix64_encode($str) { |
553 | $x = $str; |
541 | $x = $str; |
554 | $x = base64_encode($x); |
542 | $x = base64_encode($x); |
555 | $x = rtrim($x, '='); // remove padding |
543 | $x = rtrim($x, '='); // remove padding |
556 | $x = strtr($x, BASE64_RFC4648_ALPHABET, BASE64_CRYPT_ALPHABET); |
544 | $x = strtr($x, BASE64_RFC4648_ALPHABET, BASE64_CRYPT_ALPHABET); |
557 | return $x; |
545 | return $x; |
558 | } |
546 | } |
559 | 547 | ||
560 | function crypt_radix64_decode($str) { |
548 | function crypt_radix64_decode($str) { |
561 | $x = $str; |
549 | $x = $str; |
562 | $x = strtr($x, BASE64_CRYPT_ALPHABET, BASE64_RFC4648_ALPHABET); |
550 | $x = strtr($x, BASE64_CRYPT_ALPHABET, BASE64_RFC4648_ALPHABET); |
563 | $x = base64_decode($x); |
551 | $x = base64_decode($x); |
564 | return $x; |
552 | return $x; |
565 | } |
553 | } |
566 | 554 | ||
567 | function hash_supported_natively($algo) { |
555 | function hash_supported_natively($algo) { |
568 | if (version_compare(PHP_VERSION, '5.1.2') >= 0) { |
556 | if (version_compare(PHP_VERSION, '5.1.2') >= 0) { |
569 | return in_array($algo, hash_algos()); |
557 | return in_array($algo, hash_algos()); |
570 | } else { |
558 | } else { |
571 | return false; |
559 | return false; |
572 | } |
560 | } |
573 | } |
561 | } |
574 | 562 | ||
575 | function hash_hmac_supported_natively($algo): bool { |
563 | function hash_hmac_supported_natively($algo): bool { |
576 | if (version_compare(PHP_VERSION, '7.2.0') >= 0) { |
564 | if (version_compare(PHP_VERSION, '7.2.0') >= 0) { |
577 | return in_array($algo, hash_hmac_algos()); |
565 | return in_array($algo, hash_hmac_algos()); |
578 | } else if (version_compare(PHP_VERSION, '5.1.2') >= 0) { |
566 | } else if (version_compare(PHP_VERSION, '5.1.2') >= 0) { |
579 | return in_array($algo, hash_algos()); |
567 | return in_array($algo, hash_algos()); |
580 | } else { |
568 | } else { |
581 | return false; |
569 | return false; |
582 | } |
570 | } |
583 | } |
571 | } |
584 | 572 | ||
585 | function hash_pbkdf2_supported_natively($algo) { |
573 | function hash_pbkdf2_supported_natively($algo) { |
586 | return hash_supported_natively($algo); |
574 | return hash_supported_natively($algo); |
587 | } |
575 | } |
588 | 576 | ||
589 | function vts_password_fill_default_options($algo, $options) { |
577 | function vts_password_fill_default_options($algo, $options) { |
590 | if ($algo === PASSWORD_STD_DES) { |
578 | if ($algo === PASSWORD_STD_DES) { |
591 | // No options |
579 | // No options |
592 | } else if ($algo === PASSWORD_EXT_DES) { |
580 | } else if ($algo === PASSWORD_EXT_DES) { |
593 | if (!isset($options['iterations'])) { |
581 | if (!isset($options['iterations'])) { |
594 | $options['iterations'] = PASSWORD_EXT_DES_DEFAULT_ITERATIONS; |
582 | $options['iterations'] = PASSWORD_EXT_DES_DEFAULT_ITERATIONS; |
595 | } |
583 | } |
596 | } else if ($algo === PASSWORD_MD5) { |
584 | } else if ($algo === PASSWORD_MD5) { |
597 | // No options |
585 | // No options |
598 | } else if ($algo === PASSWORD_BLOWFISH) { |
586 | } else if ($algo === PASSWORD_BLOWFISH) { |
599 | if (!isset($options['cost'])) { |
587 | if (!isset($options['cost'])) { |
600 | $options['cost'] = PASSWORD_BLOWFISH_DEFAULT_COST; |
588 | $options['cost'] = PASSWORD_BLOWFISH_DEFAULT_COST; |
601 | } |
589 | } |
602 | } else if ($algo === PASSWORD_SHA256) { |
590 | } else if ($algo === PASSWORD_SHA256) { |
603 | if (!isset($options['rounds'])) { |
591 | if (!isset($options['rounds'])) { |
604 | $options['rounds'] = PASSWORD_SHA256_DEFAULT_ROUNDS; |
592 | $options['rounds'] = PASSWORD_SHA256_DEFAULT_ROUNDS; |
605 | } |
593 | } |
606 | } else if ($algo === PASSWORD_SHA512) { |
594 | } else if ($algo === PASSWORD_SHA512) { |
607 | if (!isset($options['rounds'])) { |
595 | if (!isset($options['rounds'])) { |
608 | $options['rounds'] = PASSWORD_SHA512_DEFAULT_ROUNDS; |
596 | $options['rounds'] = PASSWORD_SHA512_DEFAULT_ROUNDS; |
609 | } |
597 | } |
610 | } else if ($algo === PASSWORD_VTS_MCF1) { |
598 | } else if ($algo === PASSWORD_VTS_MCF1) { |
611 | if (!isset($options['algo'])) { |
599 | if (!isset($options['algo'])) { |
612 | $options['algo'] = PASSWORD_VTS_MCF1_DEFAULT_ALGO; |
600 | $options['algo'] = PASSWORD_VTS_MCF1_DEFAULT_ALGO; |
613 | } |
601 | } |
614 | if (!isset($options['mode'])) { |
602 | if (!isset($options['mode'])) { |
615 | $options['mode'] = PASSWORD_VTS_MCF1_DEFAULT_MODE; |
603 | $options['mode'] = PASSWORD_VTS_MCF1_DEFAULT_MODE; |
616 | } |
604 | } |
617 | if ($options['mode'] == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
605 | if ($options['mode'] == PASSWORD_VTS_MCF1_MODE_PBKDF2) { |
618 | if (!isset($options['iterations'])) { |
606 | if (!isset($options['iterations'])) { |
619 | $options['iterations'] = PASSWORD_VTS_MCF1_DEFAULT_ITERATIONS; |
607 | $options['iterations'] = PASSWORD_VTS_MCF1_DEFAULT_ITERATIONS; |
620 | } |
608 | } |
621 | } else { |
609 | } else { |
622 | unset($options['iterations']); |
610 | unset($options['iterations']); |
623 | } |
611 | } |
624 | } |
612 | } |
625 | return $options; |
613 | return $options; |
626 | } |
614 | } |
627 | 615 | ||
- | 616 | function _vts_password_default_iterations($algo, $userland) { |
|
- | 617 | if ($userland) { |
|
- | 618 | return 100; // because the userland implementation is EXTREMELY slow, we must choose a small value, sorry... |
|
- | 619 | } else { |
|
- | 620 | // Recommendations taken from https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#pbkdf2 |
|
- | 621 | // Note that hash_pbkdf2() implements PBKDF2-HMAC-* |
|
- | 622 | if ($algo == 'sha3-512') return 100000; |
|
- | 623 | else if ($algo == 'sha3-384') return 100000; |
|
- | 624 | else if ($algo == 'sha3-256') return 100000; |
|
- | 625 | else if ($algo == 'sha3-224') return 100000; |
|
- | 626 | else if ($algo == 'sha512') return 210000; // value by owasp.org cheatcheat (28 February 2023) |
|
- | 627 | else if ($algo == 'sha512/256') return 210000; // value by owasp.org cheatcheat (28 February 2023) |
|
- | 628 | else if ($algo == 'sha512/224') return 210000; // value by owasp.org cheatcheat (28 February 2023) |
|
- | 629 | else if ($algo == 'sha384') return 600000; |
|
- | 630 | else if ($algo == 'sha256') return 600000; // value by owasp.org cheatcheat (28 February 2023) |
|
- | 631 | else if ($algo == 'sha224') return 600000; |
|
- | 632 | else if ($algo == 'sha1') return 1300000; // value by owasp.org cheatcheat (28 February 2023) |
|
- | 633 | else if ($algo == 'md5') return 5000000; |
|
- | 634 | else return 5000; |
|
- | 635 | } |
|
- | 636 | } |
|
- | 637 | ||
628 | // --- Part 5: Selftest |
638 | // --- Part 6: Selftest |
629 | 639 | ||
630 | /* |
- | |
631 | for ($i=0; $i<9999; $i++) { |
640 | for ($i=0; $i<9999; $i++) { |
632 | assert($i===base64_int_decode(base64_int_encode($i,4))); |
641 | assert($i===base64_int_decode(base64_int_encode($i,4))); |
633 | } |
642 | } |
634 | 643 | ||
635 | $rnd = random_bytes_ex(50, true, true); |
644 | $rnd = random_bytes_ex(50, true, true); |
636 | assert(crypt_radix64_decode(crypt_radix64_encode($rnd)) === $rnd); |
645 | assert(crypt_radix64_decode(crypt_radix64_encode($rnd)) === $rnd); |
637 | 646 | ||
638 | $password = random_bytes_ex(20, false, true); |
647 | $password = random_bytes_ex(20, false, true); |
639 | 648 | ||
640 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_STD_DES))); |
649 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_STD_DES))); |
641 | //echo "'$dummy' ".strlen($dummy)."\n"; |
650 | //echo "'$dummy' ".strlen($dummy)."\n"; |
642 | //var_dump(vts_password_get_info($dummy)); |
651 | //var_dump(vts_password_get_info($dummy)); |
643 | 652 | ||
644 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_EXT_DES))); |
653 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_EXT_DES))); |
645 | //echo "'$dummy' ".strlen($dummy)."\n"; |
654 | //echo "'$dummy' ".strlen($dummy)."\n"; |
646 | //var_dump(vts_password_get_info($dummy)); |
655 | //var_dump(vts_password_get_info($dummy)); |
647 | 656 | ||
648 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_MD5))); |
657 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_MD5))); |
649 | //echo "'$dummy' ".strlen($dummy)."\n"; |
658 | //echo "'$dummy' ".strlen($dummy)."\n"; |
650 | //var_dump(vts_password_get_info($dummy)); |
659 | //var_dump(vts_password_get_info($dummy)); |
651 | 660 | ||
652 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_BLOWFISH))); |
661 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_BLOWFISH))); |
653 | //echo "'$dummy' ".strlen($dummy)."\n"; |
662 | //echo "'$dummy' ".strlen($dummy)."\n"; |
654 | //var_dump(vts_password_get_info($dummy)); |
663 | //var_dump(vts_password_get_info($dummy)); |
655 | 664 | ||
656 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_SHA256))); |
665 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_SHA256))); |
657 | //echo "'$dummy' ".strlen($dummy)."\n"; |
666 | //echo "'$dummy' ".strlen($dummy)."\n"; |
658 | //var_dump(vts_password_get_info($dummy)); |
667 | //var_dump(vts_password_get_info($dummy)); |
659 | 668 | ||
660 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_SHA512))); |
669 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_SHA512))); |
661 | //echo "'$dummy' ".strlen($dummy)."\n"; |
670 | //echo "'$dummy' ".strlen($dummy)."\n"; |
662 | //var_dump(vts_password_get_info($dummy)); |
671 | //var_dump(vts_password_get_info($dummy)); |
663 | 672 | ||
664 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_VTS_MCF1, array( |
673 | assert(vts_password_verify($password,$dummy = vts_password_hash($password, PASSWORD_VTS_MCF1, array( |
665 | 'algo' => 'sha3-512', |
674 | 'algo' => 'sha3-512', |
666 | 'mode' => 'pbkdf2', |
675 | 'mode' => 'pbkdf2', |
667 | 'iterations' => 0 |
676 | 'iterations' => 0 |
668 | )))); |
677 | )))); |
669 | //echo "'$dummy' ".strlen($dummy)."\n"; |
678 | //echo "'$dummy' ".strlen($dummy)."\n"; |
670 | //var_dump(vts_password_get_info($dummy)); |
679 | //var_dump(vts_password_get_info($dummy)); |
671 | assert(false===vts_password_needs_rehash($dummy,PASSWORD_VTS_MCF1,array( |
680 | assert(false===vts_password_needs_rehash($dummy,PASSWORD_VTS_MCF1,array( |
672 | 'salt_length' => 51, |
681 | 'salt_length' => 51, |
673 | 'algo' => 'sha3-512', |
682 | 'algo' => 'sha3-512', |
674 | 'mode' => 'pbkdf2', |
683 | 'mode' => 'pbkdf2', |
675 | 'iterations' => 0 |
684 | 'iterations' => 0 |
676 | ))); |
685 | ))); |
677 | assert(true===vts_password_needs_rehash($dummy,PASSWORD_VTS_MCF1,array( |
686 | assert(true===vts_password_needs_rehash($dummy,PASSWORD_VTS_MCF1,array( |
678 | 'salt_length' => 50, |
687 | 'salt_length' => 50, |
679 | 'algo' => 'sha3-256', |
688 | 'algo' => 'sha3-256', |
680 | 'mode' => 'pbkdf2', |
689 | 'mode' => 'pbkdf2', |
681 | 'iterations' => 0 |
690 | 'iterations' => 0 |
682 | ))); |
691 | ))); |
683 | 692 | ||
684 | echo "OK, password $password\n"; |
693 | echo "OK, password $password\n"; |
685 | */ |
- | |
686 | 694 |