3 * Implements the ParameterizedPassword class for the MediaWiki software.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 declare( strict_types
= 1 );
25 namespace MediaWiki\Password
;
28 * Helper class for password hash types that have a delimited set of parameters
31 * All passwords are in the form of :<TYPE>:... as explained in the main Password
32 * class. This class is for hashes in the form of :<TYPE>:<PARAM1>:<PARAM2>:... where
33 * <PARAM1>, <PARAM2>, etc. are parameters that determine how the password was hashed.
34 * Of course, the internal delimiter (which is : by convention and default), can be
35 * changed by overriding the ParameterizedPassword::getDelimiter() function.
37 * This class requires overriding an additional function: ParameterizedPassword::getDefaultParams().
38 * See the function description for more details on the implementation.
42 abstract class ParameterizedPassword
extends Password
{
44 * Named parameters that have default values for this password type
47 protected $params = [];
50 * Extra arguments that were found in the hash. This may or may not make
59 protected function parseHash( ?
string $hash ): void
{
60 parent
::parseHash( $hash );
62 if ( $hash === null ) {
63 $this->params
= $this->getDefaultParams();
67 $parts = explode( $this->getDelimiter(), $hash );
68 $paramKeys = array_keys( $this->getDefaultParams() );
70 if ( count( $parts ) < count( $paramKeys ) ) {
71 throw new PasswordError( 'Hash is missing required parameters.' );
75 $this->args
= array_splice( $parts, count( $paramKeys ) );
76 $this->params
= array_combine( $paramKeys, $parts );
82 $this->hash
= array_pop( $this->args
);
88 public function needsUpdate(): bool {
89 return $this->params
!== $this->getDefaultParams();
92 public function toString(): string {
93 $str = ':' . $this->config
['type'] . ':';
95 if ( count( $this->params
) ||
count( $this->args
) ) {
96 $str .= implode( $this->getDelimiter(), array_merge( $this->params
, $this->args
) );
97 $str .= $this->getDelimiter();
100 $res = $str . $this->hash
;
101 $this->assertIsSafeSize( $res );
106 * Returns the delimiter for the parameters inside the hash
110 abstract protected function getDelimiter(): string;
113 * Return an ordered array of default parameters for this password hash
115 * The keys should be the parameter names and the values should be the default
116 * values. Additionally, the order of the array should be the order in which they
117 * appear in the hash.
119 * When parsing a password hash, the constructor will split the hash based on
120 * the delimiter, and consume as many parts as it can, matching each to a parameter
121 * in this list. Once all the parameters have been filled, all remaining parts will
122 * be considered extra arguments, except, of course, for the very last part, which
123 * is the hash itself.
127 abstract protected function getDefaultParams(): array;
130 /** @deprecated since 1.43 use MediaWiki\\Password\\ParameterizedPassword */
131 class_alias( ParameterizedPassword
::class, 'ParameterizedPassword' );