| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
declare( strict_types = 1 ); |
| 24 |
|
| 25 |
namespace MediaWiki\Password; |
| 26 |
|
| 27 |
use InvalidArgumentException; |
| 28 |
use UnexpectedValueException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class LayeredParameterizedPassword extends ParameterizedPassword { |
| 41 |
protected function getDelimiter(): string { |
| 42 |
return '!'; |
| 43 |
} |
| 44 |
|
| 45 |
protected function getDefaultParams(): array { |
| 46 |
$params = []; |
| 47 |
|
| 48 |
foreach ( $this->config['types'] as $type ) { |
| 49 |
$passObj = $this->factory->newFromType( $type ); |
| 50 |
|
| 51 |
if ( !$passObj instanceof ParameterizedPassword ) { |
| 52 |
throw new UnexpectedValueException( 'Underlying type must be a parameterized password.' ); |
| 53 |
} elseif ( $passObj->getDelimiter() === $this->getDelimiter() ) { |
| 54 |
throw new UnexpectedValueException( |
| 55 |
'Underlying type cannot use same delimiter as encapsulating type.' |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
$params[] = implode( $passObj->getDelimiter(), $passObj->getDefaultParams() ); |
| 60 |
} |
| 61 |
|
| 62 |
return $params; |
| 63 |
} |
| 64 |
|
| 65 |
public function crypt( string $password ): void { |
| 66 |
$lastHash = $password; |
| 67 |
foreach ( $this->config['types'] as $i => $type ) { |
| 68 |
|
| 69 |
|
| 70 |
$passObj = $this->factory->newFromType( $type ); |
| 71 |
'@phan-var ParameterizedPassword $passObj'; |
| 72 |
|
| 73 |
$params = ''; |
| 74 |
$args = ''; |
| 75 |
if ( $this->params[$i] !== '' ) { |
| 76 |
$params = $this->params[$i] . $passObj->getDelimiter(); |
| 77 |
} |
| 78 |
if ( isset( $this->args[$i] ) && $this->args[$i] !== '' ) { |
| 79 |
$args = $this->args[$i] . $passObj->getDelimiter(); |
| 80 |
} |
| 81 |
$existingHash = ":$type:" . $params . $args . $this->hash; |
| 82 |
|
| 83 |
|
| 84 |
$passObj = $this->factory->newFromCiphertext( $existingHash ); |
| 85 |
'@phan-var ParameterizedPassword $passObj'; |
| 86 |
$passObj->crypt( $lastHash ); |
| 87 |
|
| 88 |
|
| 89 |
$this->params[$i] = implode( $passObj->getDelimiter(), $passObj->params ); |
| 90 |
$this->args[$i] = implode( $passObj->getDelimiter(), $passObj->args ); |
| 91 |
$lastHash = $passObj->hash; |
| 92 |
} |
| 93 |
|
| 94 |
$this->hash = $lastHash; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function partialCrypt( ParameterizedPassword $passObj ) { |
| 107 |
$type = $passObj->config['type']; |
| 108 |
if ( $type !== $this->config['types'][0] ) { |
| 109 |
throw new InvalidArgumentException( 'Only a hash in the first layer can be finished.' ); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
$this->params[0] = implode( $passObj->getDelimiter(), $passObj->params ); |
| 114 |
$this->args[0] = implode( $passObj->getDelimiter(), $passObj->args ); |
| 115 |
$lastHash = $passObj->hash; |
| 116 |
|
| 117 |
|
| 118 |
foreach ( $this->config['types'] as $i => $type ) { |
| 119 |
if ( $i == 0 ) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
$passObj = $this->factory->newFromType( $type ); |
| 126 |
'@phan-var ParameterizedPassword $passObj'; |
| 127 |
|
| 128 |
$params = ''; |
| 129 |
$args = ''; |
| 130 |
if ( $this->params[$i] !== '' ) { |
| 131 |
$params = $this->params[$i] . $passObj->getDelimiter(); |
| 132 |
} |
| 133 |
if ( isset( $this->args[$i] ) && $this->args[$i] !== '' ) { |
| 134 |
$args = $this->args[$i] . $passObj->getDelimiter(); |
| 135 |
} |
| 136 |
$existingHash = ":$type:" . $params . $args . $this->hash; |
| 137 |
|
| 138 |
|
| 139 |
$passObj = $this->factory->newFromCiphertext( $existingHash ); |
| 140 |
'@phan-var ParameterizedPassword $passObj'; |
| 141 |
$passObj->crypt( $lastHash ); |
| 142 |
|
| 143 |
|
| 144 |
$this->params[$i] = implode( $passObj->getDelimiter(), $passObj->params ); |
| 145 |
$this->args[$i] = implode( $passObj->getDelimiter(), $passObj->args ); |
| 146 |
$lastHash = $passObj->hash; |
| 147 |
} |
| 148 |
|
| 149 |
$this->hash = $lastHash; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
class_alias( LayeredParameterizedPassword::class, 'LayeredParameterizedPassword' ); |