MediaWiki  REL1_31
BcryptPassword.php
Go to the documentation of this file.
1 <?php
32  protected function getDefaultParams() {
33  return [
34  'rounds' => $this->config['cost'],
35  ];
36  }
37 
38  protected function getDelimiter() {
39  return '$';
40  }
41 
42  protected function parseHash( $hash ) {
43  parent::parseHash( $hash );
44 
45  $this->params['rounds'] = (int)$this->params['rounds'];
46  }
47 
54  public function crypt( $password ) {
55  if ( !defined( 'CRYPT_BLOWFISH' ) ) {
56  throw new MWException( 'Bcrypt is not supported.' );
57  }
58 
59  // Either use existing hash or make a new salt
60  // Bcrypt expects 22 characters of base64-encoded salt
61  // Note: bcrypt does not use MIME base64. It uses its own base64 without any '=' padding.
62  // It expects a 128 bit salt, so it will ignore anything after the first 128 bits
63  if ( !isset( $this->args[0] ) ) {
64  $this->args[] = substr(
65  // Replace + with ., because bcrypt uses a non-MIME base64 format
66  strtr(
67  // Random base64 encoded string
68  base64_encode( MWCryptRand::generate( 16, true ) ),
69  '+', '.'
70  ),
71  0, 22
72  );
73  }
74 
75  $hash = crypt( $password,
76  sprintf( '$2y$%02d$%s', (int)$this->params['rounds'], $this->args[0] ) );
77 
78  if ( !is_string( $hash ) || strlen( $hash ) <= 13 ) {
79  throw new PasswordError( 'Error when hashing password.' );
80  }
81 
82  // Strip the $2y$
83  $parts = explode( $this->getDelimiter(), substr( $hash, 4 ) );
84  $this->params['rounds'] = (int)$parts[0];
85  $this->args[0] = substr( $parts[1], 0, 22 );
86  $this->hash = substr( $parts[1], 22 );
87  }
88 }
PasswordError
Show an error when any operation involving passwords fails to run.
Definition: PasswordError.php:26
BcryptPassword\parseHash
parseHash( $hash)
Perform any parsing necessary on the hash to see if the hash is valid and/or to perform logic for see...
Definition: BcryptPassword.php:42
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:37
MWCryptRand\generate
static generate( $bytes, $forceStrong=false)
Generate a run of (ideally) cryptographically random data and return it in raw binary form.
Definition: MWCryptRand.php:60
MWException
MediaWiki exception.
Definition: MWException.php:26
BcryptPassword
A Bcrypt-hashed password.
Definition: BcryptPassword.php:31
Password\$hash
string $hash
String representation of the hash without the type.
Definition: Password.php:76
BcryptPassword\crypt
crypt( $password)
Definition: BcryptPassword.php:54
ParameterizedPassword
Helper class for password hash types that have a delimited set of parameters inside of the hash.
Definition: ParameterizedPassword.php:38
BcryptPassword\getDefaultParams
getDefaultParams()
Return an ordered array of default parameters for this password hash.
Definition: BcryptPassword.php:32
BcryptPassword\getDelimiter
getDelimiter()
Returns the delimiter for the parameters inside the hash.
Definition: BcryptPassword.php:38