ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilBcryptPhpPasswordEncoder Class Reference
+ Inheritance diagram for ilBcryptPhpPasswordEncoder:
+ Collaboration diagram for ilBcryptPhpPasswordEncoder:

Public Member Functions

 __construct (array $config=array())
 
 benchmarkCost ($time_target=0.05)
 
 getName ()
 
 isSupportedByRuntime ()
 {Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...)
Returns
boolean
} More...
 
 getCosts ()
 
 setCosts ($costs)
 
 encodePassword ($raw, $salt)
 {Encodes the raw password.
Parameters
string$rawThe password to encode
string$saltThe salt
Returns
string The encoded password
} More...
 
 isPasswordValid ($encoded, $raw, $salt)
 {Checks a raw password against an encoded password.The raw password has to be injected into the encoder instance before.

Parameters
string$encodedAn encoded password
string$rawA raw password
string$saltThe salt
Returns
Boolean true if the password is valid, false otherwise
} More...
 
 requiresReencoding ($encoded)
 {Returns whether or not the a encoded password needs to be re-encoded.
Parameters
$encodedstring
Returns
boolean
} More...
 
- Public Member Functions inherited from ilBasePasswordEncoder
 isSupportedByRuntime ()
 {Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...)
Returns
boolean
} More...
 
 requiresSalt ()
 {Returns whether or not the encoder requires a salt.
Returns
boolean
} More...
 
 requiresReencoding ($encoded)
 {Returns whether or not the a encoded password needs to be re-encoded.
Parameters
$encodedstring
Returns
boolean
} More...
 

Protected Member Functions

 init ()
 
- Protected Member Functions inherited from ilBasePasswordEncoder
 comparePasswords ($known_string, $user_string)
 Compares two passwords. More...
 
 isPasswordTooLong ($password)
 Checks if the password is too long. More...
 

Protected Attributes

 $costs = '08'
 

Additional Inherited Members

- Data Fields inherited from ilBasePasswordEncoder
const MAX_PASSWORD_LENGTH = 4096
 

Detailed Description

Definition at line 11 of file class.ilBcryptPhpPasswordEncoder.php.

Constructor & Destructor Documentation

◆ __construct()

ilBcryptPhpPasswordEncoder::__construct ( array  $config = array())
Parameters
array$config
Exceptions
ilPasswordException

Definition at line 22 of file class.ilBcryptPhpPasswordEncoder.php.

References $config, benchmarkCost(), init(), and setCosts().

23  {
24  if(!empty($config))
25  {
26  foreach($config as $key => $value)
27  {
28  switch(strtolower($key))
29  {
30  case 'cost':
31  $this->setCosts($value);
32  break;
33  }
34  }
35  }
36 
37  if(!isset($config['cost']) && static::class == self::class)
38  {
39  // Determine the costs only if they are not passed in constructor
40  $this->setCosts($this->benchmarkCost(0.05));
41  }
42 
43  $this->init();
44  }
+ Here is the call graph for this function:

Member Function Documentation

◆ benchmarkCost()

ilBcryptPhpPasswordEncoder::benchmarkCost (   $time_target = 0.05)
See also
http://php.net/manual/en/function.password-hash.php#example-984
Parameters
float$time_target
Returns
int

Definition at line 58 of file class.ilBcryptPhpPasswordEncoder.php.

References $start, and array.

Referenced by __construct(), and ilBcryptPhpPasswordEncoderTest\testCostsCanBeDeterminedDynamically().

59  {
60  $cost = 8;
61 
62  do
63  {
64  $cost++;
65  $start = microtime(true);
66  $encoder = new self(array('cost' => $cost));
67  $encoder->encodePassword('test', '');
68  $end = microtime(true);
69  }
70  while(($end - $start) < $time_target && $cost < 32);
71 
72  return $cost;
73  }
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ encodePassword()

ilBcryptPhpPasswordEncoder::encodePassword (   $raw,
  $salt 
)

{Encodes the raw password.

Parameters
string$rawThe password to encode
string$saltThe salt
Returns
string The encoded password
}

Exceptions
ilPasswordException

Implements ilPasswordEncoder.

Definition at line 121 of file class.ilBcryptPhpPasswordEncoder.php.

References array, getCosts(), and ilBasePasswordEncoder\isPasswordTooLong().

Referenced by ilBcryptPhpPasswordEncoderTest\testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(), ilBcryptPhpPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified(), and ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

122  {
123  if($this->isPasswordTooLong($raw))
124  {
125  require_once 'Services/Password/exceptions/class.ilPasswordException.php';
126  throw new ilPasswordException('Invalid password.');
127  }
128 
129  return password_hash($raw, PASSWORD_BCRYPT, array(
130  'cost' => $this->getCosts()
131  ));
132  }
Class for user password exception handling in ILIAS.
Create styles array
The data for the language used.
isPasswordTooLong($password)
Checks if the password is too long.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCosts()

◆ getName()

ilBcryptPhpPasswordEncoder::getName ( )
Returns
string

Implements ilPasswordEncoder.

Definition at line 78 of file class.ilBcryptPhpPasswordEncoder.php.

Referenced by ilBcryptPhpPasswordEncoderTest\testNameShouldBeBcryptPhp().

79  {
80  return 'bcryptphp';
81  }
+ Here is the caller graph for this function:

◆ init()

ilBcryptPhpPasswordEncoder::init ( )
protected

Definition at line 49 of file class.ilBcryptPhpPasswordEncoder.php.

Referenced by __construct().

50  {
51  }
+ Here is the caller graph for this function:

◆ isPasswordValid()

ilBcryptPhpPasswordEncoder::isPasswordValid (   $encoded,
  $raw,
  $salt 
)

{Checks a raw password against an encoded password.The raw password has to be injected into the encoder instance before.

Parameters
string$encodedAn encoded password
string$rawA raw password
string$saltThe salt
Returns
Boolean true if the password is valid, false otherwise
}

Implements ilPasswordEncoder.

Definition at line 137 of file class.ilBcryptPhpPasswordEncoder.php.

Referenced by ilBcryptPhpPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified(), and ilBcryptPhpPasswordEncoderTest\testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength().

138  {
139  return password_verify($raw, $encoded);
140  }
+ Here is the caller graph for this function:

◆ isSupportedByRuntime()

ilBcryptPhpPasswordEncoder::isSupportedByRuntime ( )

{Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...)

Returns
boolean
}

Implements ilPasswordEncoder.

Definition at line 86 of file class.ilBcryptPhpPasswordEncoder.php.

87  {
88  return parent::isSupportedByRuntime() && version_compare(phpversion(), '5.5.0', '>=');
89  }

◆ requiresReencoding()

ilBcryptPhpPasswordEncoder::requiresReencoding (   $encoded)

{Returns whether or not the a encoded password needs to be re-encoded.

Parameters
$encodedstring
Returns
boolean
}

Implements ilPasswordEncoder.

Definition at line 145 of file class.ilBcryptPhpPasswordEncoder.php.

References array, and getCosts().

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

146  {
147  return password_needs_rehash($encoded, PASSWORD_BCRYPT, array(
148  'cost' => $this->getCosts()
149  ));
150  }
Create styles array
The data for the language used.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCosts()

ilBcryptPhpPasswordEncoder::setCosts (   $costs)
Parameters
string$costs
Exceptions
ilPasswordException

Definition at line 103 of file class.ilBcryptPhpPasswordEncoder.php.

References $costs.

Referenced by __construct(), ilBcryptPhpPasswordEncoderTest\testCostsCanBeRetrievedWhenCostsAreSet(), ilBcryptPasswordEncoderTest\testCostsCanBeRetrievedWhenCostsAreSet(), ilBcryptPhpPasswordEncoderTest\testCostsCanBeSetInRange(), ilBcryptPasswordEncoderTest\testCostsCanBeSetInRange(), ilBcryptPhpPasswordEncoderTest\testCostsCannotBeSetAboveRange(), ilBcryptPasswordEncoderTest\testCostsCannotBeSetAboveRange(), ilBcryptPhpPasswordEncoderTest\testCostsCannotBeSetBelowRange(), ilBcryptPasswordEncoderTest\testCostsCannotBeSetBelowRange(), ilBcryptPhpPasswordEncoderTest\testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(), ilBcryptPasswordEncoderTest\testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(), ilBcryptPhpPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified(), ilBcryptPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified(), ilBcryptPhpPasswordEncoderTest\testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(), ilBcryptPasswordEncoderTest\testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(), and ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

104  {
105  if(!empty($costs))
106  {
107  $costs = (int)$costs;
108  if($costs < 4 || $costs > 31)
109  {
110  require_once 'Services/Password/exceptions/class.ilPasswordException.php';
111  throw new ilPasswordException('The costs parameter of bcrypt must be in range 04-31');
112  }
113  $this->costs = sprintf('%1$02d', $costs);
114  }
115  }
Class for user password exception handling in ILIAS.
+ Here is the caller graph for this function:

Field Documentation

◆ $costs

ilBcryptPhpPasswordEncoder::$costs = '08'
protected

Definition at line 16 of file class.ilBcryptPhpPasswordEncoder.php.

Referenced by getCosts(), and setCosts().


The documentation for this class was generated from the following file: