MediaWiki  1.34.0
LayeredParameterizedPassword.php
Go to the documentation of this file.
1 <?php
34  protected function getDelimiter() {
35  return '!';
36  }
37 
38  protected function getDefaultParams() {
39  $params = [];
40 
41  foreach ( $this->config['types'] as $type ) {
42  $passObj = $this->factory->newFromType( $type );
43 
44  if ( !$passObj instanceof ParameterizedPassword ) {
45  throw new MWException( 'Underlying type must be a parameterized password.' );
46  } elseif ( $passObj->getDelimiter() === $this->getDelimiter() ) {
47  throw new MWException( 'Underlying type cannot use same delimiter as encapsulating type.' );
48  }
49 
50  $params[] = implode( $passObj->getDelimiter(), $passObj->getDefaultParams() );
51  }
52 
53  return $params;
54  }
55 
56  public function crypt( $password ) {
57  $lastHash = $password;
58  foreach ( $this->config['types'] as $i => $type ) {
59  // Construct pseudo-hash based on params and arguments
61  $passObj = $this->factory->newFromType( $type );
62  '@phan-var ParameterizedPassword $passObj';
63 
64  $params = '';
65  $args = '';
66  if ( $this->params[$i] !== '' ) {
67  $params = $this->params[$i] . $passObj->getDelimiter();
68  }
69  if ( isset( $this->args[$i] ) && $this->args[$i] !== '' ) {
70  $args = $this->args[$i] . $passObj->getDelimiter();
71  }
72  $existingHash = ":$type:" . $params . $args . $this->hash;
73 
74  // Hash the last hash with the next type in the layer
75  $passObj = $this->factory->newFromCiphertext( $existingHash );
76  '@phan-var ParameterizedPassword $passObj';
77  $passObj->crypt( $lastHash );
78 
79  // Move over the params and args
80  $this->params[$i] = implode( $passObj->getDelimiter(), $passObj->params );
81  $this->args[$i] = implode( $passObj->getDelimiter(), $passObj->args );
82  $lastHash = $passObj->hash;
83  }
84 
85  $this->hash = $lastHash;
86  }
87 
99  public function partialCrypt( ParameterizedPassword $passObj ) {
100  $type = $passObj->config['type'];
101  if ( $type !== $this->config['types'][0] ) {
102  throw new MWException( 'Only a hash in the first layer can be finished.' );
103  }
104 
105  // Gather info from the existing hash
106  $this->params[0] = implode( $passObj->getDelimiter(), $passObj->params );
107  $this->args[0] = implode( $passObj->getDelimiter(), $passObj->args );
108  $lastHash = $passObj->hash;
109 
110  // Layer the remaining types
111  foreach ( $this->config['types'] as $i => $type ) {
112  if ( $i == 0 ) {
113  continue;
114  }
115 
116  // Construct pseudo-hash based on params and arguments
118  $passObj = $this->factory->newFromType( $type );
119  '@phan-var ParameterizedPassword $passObj';
120 
121  $params = '';
122  $args = '';
123  if ( $this->params[$i] !== '' ) {
124  $params = $this->params[$i] . $passObj->getDelimiter();
125  }
126  if ( isset( $this->args[$i] ) && $this->args[$i] !== '' ) {
127  $args = $this->args[$i] . $passObj->getDelimiter();
128  }
129  $existingHash = ":$type:" . $params . $args . $this->hash;
130 
131  // Hash the last hash with the next type in the layer
132  $passObj = $this->factory->newFromCiphertext( $existingHash );
133  '@phan-var ParameterizedPassword $passObj';
134  $passObj->crypt( $lastHash );
135 
136  // Move over the params and args
137  $this->params[$i] = implode( $passObj->getDelimiter(), $passObj->params );
138  $this->args[$i] = implode( $passObj->getDelimiter(), $passObj->args );
139  $lastHash = $passObj->hash;
140  }
141 
142  $this->hash = $lastHash;
143  }
144 }
ParameterizedPassword\getDelimiter
getDelimiter()
Returns the delimiter for the parameters inside the hash.
LayeredParameterizedPassword\crypt
crypt( $password)
Hash a password and store the result in this object.
Definition: LayeredParameterizedPassword.php:56
ParameterizedPassword\$params
array $params
Named parameters that have default values for this password type.
Definition: ParameterizedPassword.php:43
LayeredParameterizedPassword\partialCrypt
partialCrypt(ParameterizedPassword $passObj)
Finish the hashing of a partially hashed layered hash.
Definition: LayeredParameterizedPassword.php:99
MWException
MediaWiki exception.
Definition: MWException.php:26
LayeredParameterizedPassword\getDelimiter
getDelimiter()
Returns the delimiter for the parameters inside the hash.
Definition: LayeredParameterizedPassword.php:34
Password\crypt
crypt( $password)
Hash a password and store the result in this object.
Password\$hash
string $hash
String representation of the hash without the type.
Definition: Password.php:71
LayeredParameterizedPassword
This password hash type layers one or more parameterized password types on top of each other.
Definition: LayeredParameterizedPassword.php:33
ParameterizedPassword
Helper class for password hash types that have a delimited set of parameters inside of the hash.
Definition: ParameterizedPassword.php:38
ParameterizedPassword\$args
array $args
Extra arguments that were found in the hash.
Definition: ParameterizedPassword.php:50
LayeredParameterizedPassword\getDefaultParams
getDefaultParams()
Return an ordered array of default parameters for this password hash.
Definition: LayeredParameterizedPassword.php:38
$type
$type
Definition: testCompression.php:48