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