| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
declare( strict_types = 1 ); |
| 22 |
|
| 23 |
namespace MediaWiki\Password; |
| 24 |
|
| 25 |
use LogicException; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class Argon2Password extends Password { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private const KNOWN_OPTIONS = [ |
| 38 |
'memory_cost' => null, |
| 39 |
'time_cost' => null, |
| 40 |
'threads' => null, |
| 41 |
]; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected function isSupported(): bool { |
| 47 |
return defined( 'PASSWORD_ARGON2ID' ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private function prepareParams(): array { |
| 54 |
switch ( $this->config['algo'] ) { |
| 55 |
case 'argon2i': |
| 56 |
$algo = PASSWORD_ARGON2I; |
| 57 |
break; |
| 58 |
case 'argon2id': |
| 59 |
case 'auto': |
| 60 |
$algo = PASSWORD_ARGON2ID; |
| 61 |
break; |
| 62 |
default: |
| 63 |
throw new LogicException( "Unexpected algo: {$this->config['algo']}" ); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
$params = array_intersect_key( $this->config, self::KNOWN_OPTIONS ); |
| 68 |
|
| 69 |
return [ $algo, $params ]; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function crypt( string $password ): void { |
| 76 |
[ $algo, $params ] = $this->prepareParams(); |
| 77 |
$this->hash = password_hash( $password, $algo, $params ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function verify( string $password ): bool { |
| 84 |
return password_verify( $password, $this->hash ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function toString(): string { |
| 91 |
$res = ":argon2:{$this->hash}"; |
| 92 |
$this->assertIsSafeSize( $res ); |
| 93 |
return $res; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function needsUpdate(): bool { |
| 100 |
[ $algo, $params ] = $this->prepareParams(); |
| 101 |
return password_needs_rehash( $this->hash, $algo, $params ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
class_alias( Argon2Password::class, 'Argon2Password' ); |