Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| helper | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
11 | |
100.00% |
1 / 1 |
| get_combined_hash_settings | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| combine_hash_output | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| rebuild_hash | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| obtain_hash_only | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\passwords; |
| 15 | |
| 16 | class helper |
| 17 | { |
| 18 | /** |
| 19 | * Get hash settings from combined hash |
| 20 | * |
| 21 | * @param string $hash Password hash of combined hash |
| 22 | * |
| 23 | * @return array An array containing the hash settings for the hash |
| 24 | * types in successive order as described by the combined |
| 25 | * password hash or an empty array if hash does not |
| 26 | * properly fit the combined hash format |
| 27 | */ |
| 28 | public function get_combined_hash_settings($hash) |
| 29 | { |
| 30 | $output = array(); |
| 31 | |
| 32 | preg_match('#^\$([a-zA-Z0-9\\\]*?)\$#', $hash, $match); |
| 33 | $hash_settings = substr($hash, strpos($hash, $match[1]) + strlen($match[1]) + 1); |
| 34 | $matches = explode('\\', $match[1]); |
| 35 | foreach ($matches as $cur_type) |
| 36 | { |
| 37 | $dollar_position = strpos($hash_settings, '$'); |
| 38 | $output[] = substr($hash_settings, 0, ($dollar_position != false) ? $dollar_position : strlen($hash_settings)); |
| 39 | $hash_settings = substr($hash_settings, $dollar_position + 1); |
| 40 | } |
| 41 | |
| 42 | return $output; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Combine hash prefixes, settings, and actual hash |
| 47 | * |
| 48 | * @param array $data Array containing the keys 'prefix' and 'settings'. |
| 49 | * It will hold the prefixes and settings |
| 50 | * @param string $type Data type of the supplied value |
| 51 | * @param string $value Value that should be put into the data array |
| 52 | * |
| 53 | * @return string|false Return complete combined hash if type is neither |
| 54 | * 'prefix' nor 'settings', false if it is |
| 55 | */ |
| 56 | public function combine_hash_output(&$data, $type, $value) |
| 57 | { |
| 58 | if ($type == 'prefix') |
| 59 | { |
| 60 | $data[$type] .= ($data[$type] !== '$') ? '\\' : ''; |
| 61 | $data[$type] .= str_replace('$', '', $value); |
| 62 | } |
| 63 | else if ($type == 'settings') |
| 64 | { |
| 65 | $data[$type] .= ($data[$type] !== '$') ? '$' : ''; |
| 66 | $data[$type] .= $value; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Return full hash |
| 71 | return $data['prefix'] . $data['settings'] . '$' . $value; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Rebuild hash for hashing functions |
| 79 | * |
| 80 | * @param string $prefix Hash prefix |
| 81 | * @param string $settings Hash settings |
| 82 | * |
| 83 | * @return string Rebuilt hash for hashing functions |
| 84 | */ |
| 85 | public function rebuild_hash($prefix, $settings) |
| 86 | { |
| 87 | $rebuilt_hash = $prefix; |
| 88 | if (strpos($settings, '\\') !== false) |
| 89 | { |
| 90 | $settings = str_replace('\\', '$', $settings); |
| 91 | } |
| 92 | $rebuilt_hash .= $settings; |
| 93 | return $rebuilt_hash; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Obtain only the actual hash after the prefixes |
| 98 | * |
| 99 | * @param string $hash The full password hash |
| 100 | * @return string Actual hash (incl. settings) |
| 101 | */ |
| 102 | public function obtain_hash_only($hash) |
| 103 | { |
| 104 | return substr($hash, strripos($hash, '$') + 1); |
| 105 | } |
| 106 | } |