2013-06-15 10:04:10 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
2014-05-27 20:18:06 +02:00
|
|
|
* This file is part of the phpBB Forum Software package.
|
|
|
|
*
|
|
|
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0)
|
|
|
|
*
|
|
|
|
* For full copyright and license information, please see
|
|
|
|
* the docs/CREDITS.txt file.
|
2013-06-15 10:04:10 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-09-27 11:16:47 +02:00
|
|
|
namespace phpbb\passwords;
|
|
|
|
|
|
|
|
class helper
|
2013-06-15 10:04:10 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get hash settings from combined hash
|
|
|
|
*
|
|
|
|
* @param string $hash Password hash of combined hash
|
|
|
|
*
|
|
|
|
* @return array An array containing the hash settings for the hash
|
2013-12-29 17:55:00 +01:00
|
|
|
* types in successive order as described by the combined
|
|
|
|
* password hash or an empty array if hash does not
|
|
|
|
* properly fit the combined hash format
|
2013-06-15 10:04:10 +02:00
|
|
|
*/
|
2014-02-02 14:57:43 +01:00
|
|
|
public function get_combined_hash_settings($hash)
|
2013-06-15 10:04:10 +02:00
|
|
|
{
|
2013-12-29 17:57:17 +01:00
|
|
|
$output = array();
|
|
|
|
|
2013-06-15 10:04:10 +02:00
|
|
|
preg_match('#^\$([a-zA-Z0-9\\\]*?)\$#', $hash, $match);
|
|
|
|
$hash_settings = substr($hash, strpos($hash, $match[1]) + strlen($match[1]) + 1);
|
2013-06-27 14:25:50 +02:00
|
|
|
$matches = explode('\\', $match[1]);
|
|
|
|
foreach ($matches as $cur_type)
|
2013-06-15 10:04:10 +02:00
|
|
|
{
|
|
|
|
$dollar_position = strpos($hash_settings, '$');
|
|
|
|
$output[] = substr($hash_settings, 0, ($dollar_position != false) ? $dollar_position : strlen($hash_settings));
|
|
|
|
$hash_settings = substr($hash_settings, $dollar_position + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combine hash prefixes, settings, and actual hash
|
|
|
|
*
|
|
|
|
* @param array $data Array containing the keys 'prefix' and 'settings'.
|
|
|
|
* It will hold the prefixes and settings
|
|
|
|
* @param string $type Data type of the supplied value
|
|
|
|
* @param string $value Value that should be put into the data array
|
|
|
|
*
|
2013-12-29 17:55:00 +01:00
|
|
|
* @return string|null Return complete combined hash if type is neither
|
2013-06-15 10:04:10 +02:00
|
|
|
* 'prefix' nor 'settings', nothing if it is
|
|
|
|
*/
|
2014-02-02 14:57:43 +01:00
|
|
|
public function combine_hash_output(&$data, $type, $value)
|
2013-06-15 10:04:10 +02:00
|
|
|
{
|
|
|
|
if ($type == 'prefix')
|
|
|
|
{
|
|
|
|
$data[$type] .= ($data[$type] !== '$') ? '\\' : '';
|
2013-06-27 14:25:50 +02:00
|
|
|
$data[$type] .= str_replace('$', '', $value);
|
2013-06-15 10:04:10 +02:00
|
|
|
}
|
2014-06-15 21:34:02 +02:00
|
|
|
else if ($type == 'settings')
|
2013-06-15 10:04:10 +02:00
|
|
|
{
|
|
|
|
$data[$type] .= ($data[$type] !== '$') ? '$' : '';
|
|
|
|
$data[$type] .= $value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Return full hash
|
|
|
|
return $data['prefix'] . $data['settings'] . '$' . $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rebuild hash for hashing functions
|
|
|
|
*
|
|
|
|
* @param string $prefix Hash prefix
|
|
|
|
* @param string $settings Hash settings
|
|
|
|
*
|
|
|
|
* @return string Rebuilt hash for hashing functions
|
|
|
|
*/
|
2014-02-02 14:57:43 +01:00
|
|
|
public function rebuild_hash($prefix, $settings)
|
2013-06-15 10:04:10 +02:00
|
|
|
{
|
|
|
|
$rebuilt_hash = $prefix;
|
|
|
|
if (strpos($settings, '\\') !== false)
|
|
|
|
{
|
|
|
|
$settings = str_replace('\\', '$', $settings);
|
|
|
|
}
|
|
|
|
$rebuilt_hash .= $settings;
|
|
|
|
return $rebuilt_hash;
|
|
|
|
}
|
2013-06-27 14:27:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain only the actual hash after the prefixes
|
|
|
|
*
|
|
|
|
* @param string $hash The full password hash
|
|
|
|
* @return string Actual hash (incl. settings)
|
|
|
|
*/
|
2014-02-02 14:57:43 +01:00
|
|
|
public function obtain_hash_only($hash)
|
2013-06-27 14:27:11 +02:00
|
|
|
{
|
|
|
|
return substr($hash, strripos($hash, '$') + 1);
|
|
|
|
}
|
2013-06-15 10:04:10 +02:00
|
|
|
}
|