ILIAS  release_7 Revision v7.30-3-g800a261c036
ilBcryptPhpPasswordEncoder Class Reference
+ Inheritance diagram for ilBcryptPhpPasswordEncoder:
+ Collaboration diagram for ilBcryptPhpPasswordEncoder:

Public Member Functions

 __construct (array $config=[])
 
 benchmarkCost (float $time_target=0.05)
 
 getName ()
 
 isSupportedByRuntime ()
 
 getCosts ()
 
 setCosts (string $costs)
 
 encodePassword (string $raw, string $salt)
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 
 requiresReencoding (string $encoded)
 
- Public Member Functions inherited from ilBasePasswordEncoder
 isSupportedByRuntime ()
 
 requiresSalt ()
 
 requiresReencoding (string $encoded)
 

Protected Member Functions

 init ()
 
- Protected Member Functions inherited from ilBasePasswordEncoder
 comparePasswords (string $knownString, string $userString)
 Compares two passwords. More...
 
 isPasswordTooLong (string $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 = [])
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  foreach ($config as $key => $value) {
26  switch (strtolower($key)) {
27  case 'cost':
28  $this->setCosts($value);
29  break;
30  }
31  }
32  }
33 
34  if (!isset($config['cost']) && static::class == self::class) {
35  // Determine the costs only if they are not passed in constructor
36  $this->setCosts((string) $this->benchmarkCost(0.05));
37  }
38 
39  $this->init();
40  }
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
+ Here is the call graph for this function:

Member Function Documentation

◆ benchmarkCost()

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

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

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

55  : int
56  {
57  $cost = 8;
58 
59  do {
60  $cost++;
61  $start = microtime(true);
62  $encoder = new self(['cost' => (string) $cost]);
63  $encoder->encodePassword('test', '');
64  $end = microtime(true);
65  } while (($end - $start) < $time_target && $cost < 32);
66 
67  return $cost;
68  }
+ Here is the caller graph for this function:

◆ encodePassword()

ilBcryptPhpPasswordEncoder::encodePassword ( string  $raw,
string  $salt 
)

Exceptions
ilPasswordException

Implements ilPasswordEncoder.

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

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

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

113  : string
114  {
115  if ($this->isPasswordTooLong($raw)) {
116  throw new ilPasswordException('Invalid password.');
117  }
118 
119  return password_hash($raw, PASSWORD_BCRYPT, [
120  'cost' => $this->getCosts()
121  ]);
122  }
Class for user password exception handling in ILIAS.
isPasswordTooLong(string $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()

ilBcryptPhpPasswordEncoder::getCosts ( )

◆ getName()

ilBcryptPhpPasswordEncoder::getName ( )
Returns
string

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPhpPasswordEncoderTest\testNameShouldBeBcryptPhp().

73  : string
74  {
75  return 'bcryptphp';
76  }
+ Here is the caller graph for this function:

◆ init()

ilBcryptPhpPasswordEncoder::init ( )
protected

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

Referenced by __construct().

45  : void
46  {
47  }
+ Here is the caller graph for this function:

◆ isPasswordValid()

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

Implements ilPasswordEncoder.

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

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

127  : bool
128  {
129  return password_verify($raw, $encoded);
130  }
+ Here is the caller graph for this function:

◆ isSupportedByRuntime()

ilBcryptPhpPasswordEncoder::isSupportedByRuntime ( )

Implements ilPasswordEncoder.

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

81  : bool
82  {
83  return parent::isSupportedByRuntime() && version_compare(phpversion(), '5.5.0', '>=');
84  }

◆ requiresReencoding()

ilBcryptPhpPasswordEncoder::requiresReencoding ( string  $encoded)

Implements ilPasswordEncoder.

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

References getCosts().

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

135  : bool
136  {
137  return password_needs_rehash($encoded, PASSWORD_BCRYPT, [
138  'cost' => $this->getCosts()
139  ]);
140  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCosts()

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

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

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().

98  : void
99  {
100  if (!empty($costs)) {
101  $costs = (int) $costs;
102  if ($costs < 4 || $costs > 31) {
103  throw new ilPasswordException('The costs parameter of bcrypt must be in range 04-31');
104  }
105  $this->costs = sprintf('%1$02d', $costs);
106  }
107  }
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().


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