MediaWiki  1.33.0
Argon2Password.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\Assert\Assert;
4 
30 class Argon2Password extends Password {
34  private static $knownOptions = [
35  'memory_cost' => null,
36  'time_cost' => null,
37  'threads' => null,
38  ];
39 
43  protected function isSupported() {
44  // It is actually possible to have a PHP build with Argon2i but not Argon2id
45  return defined( 'PASSWORD_ARGON2I' ) || defined( 'PASSWORD_ARGON2ID' );
46  }
47 
51  private function prepareParams() {
52  switch ( $this->config['algo'] ) {
53  case 'argon2i':
54  $algo = PASSWORD_ARGON2I;
55  break;
56  case 'argon2id':
57  $algo = PASSWORD_ARGON2ID;
58  break;
59  case 'auto':
60  $algo = defined( 'PASSWORD_ARGON2ID' ) ? PASSWORD_ARGON2ID : PASSWORD_ARGON2I;
61  break;
62  default:
63  throw new LogicException( "Unexpected algo: {$this->config['algo']}" );
64 
65  }
66 
67  $params = array_intersect_key( $this->config, self::$knownOptions );
68 
69  return [ $algo, $params ];
70  }
71 
75  public function crypt( $password ) {
76  list( $algo, $params ) = $this->prepareParams();
77  $this->hash = password_hash( $password, $algo, $params );
78  }
79 
83  public function equals( $other ) {
84  if ( is_string( $other ) ) {
85  return $this->verify( $other );
86  }
87 
88  // Argon2 key derivation is not deterministic, can't pass objects to equals()
89  return false;
90  }
91 
95  public function verify( $password ) {
96  Assert::parameterType( 'string', $password, '$password' );
97 
98  return password_verify( $password, $this->hash );
99  }
100 
104  public function toString() {
105  $res = ":argon2:{$this->hash}";
106  $this->assertIsSafeSize( $res );
107  return $res;
108  }
109 
113  public function needsUpdate() {
114  list( $algo, $params ) = $this->prepareParams();
115  return password_needs_rehash( $this->hash, $algo, $params );
116  }
117 }
Argon2Password
Implements Argon2, a modern key derivation algorithm designed to resist GPU cracking and side-channel...
Definition: Argon2Password.php:30
Argon2Password\equals
equals( $other)
@inheritDoc
Definition: Argon2Password.php:83
$params
$params
Definition: styleTest.css.php:44
$res
$res
Definition: database.txt:21
Argon2Password\isSupported
isSupported()
@inheritDoc
Definition: Argon2Password.php:43
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Password\assertIsSafeSize
assertIsSafeSize( $hash)
Assert that hash will fit in a tinyblob field.
Definition: Password.php:215
Argon2Password\toString
toString()
@inheritDoc
Definition: Argon2Password.php:104
Argon2Password\prepareParams
prepareParams()
Definition: Argon2Password.php:51
Argon2Password\verify
verify( $password)
@inheritDoc
Definition: Argon2Password.php:95
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
Argon2Password\needsUpdate
needsUpdate()
@inheritDoc
Definition: Argon2Password.php:113
Argon2Password\$knownOptions
static null[] $knownOptions
Array with known password_hash() option names as keys.
Definition: Argon2Password.php:34
Password
Represents a password hash for use in authentication.
Definition: Password.php:61
Argon2Password\crypt
crypt( $password)
@inheritDoc
Definition: Argon2Password.php:75