ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilBcryptPhpPasswordEncoder Class Reference
+ Inheritance diagram for ilBcryptPhpPasswordEncoder:
+ Collaboration diagram for ilBcryptPhpPasswordEncoder:

Public Member Functions

 __construct (array $config=[])
 
 benchmarkCost (float $time_target=0.05)
 
 getName ()
 Returns a unique name/id of the concrete password encoder. More...
 
 getCosts ()
 
 setCosts (string $costs)
 
 encodePassword (string $raw, string $salt)
 Encodes the raw password. More...
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 Checks a raw password against an encoded password. More...
 
 requiresReencoding (string $encoded)
 Returns whether the encoded password needs to be re-encoded. More...
 
- Public Member Functions inherited from ilBasePasswordEncoder
 isSupportedByRuntime ()
 Returns whether the encoder is supported by the runtime (PHP, HHVM, ...) More...
 
 requiresSalt ()
 Returns whether the encoder requires a salt. More...
 
 requiresReencoding (string $encoded)
 Returns whether the encoded password needs to be re-encoded. More...
 

Private Attributes

const COST = 'cost'
 
string $costs = '08'
 

Additional Inherited Members

- Protected Member Functions inherited from ilBasePasswordEncoder
 comparePasswords (string $knownString, string $userString)
 Compares two passwords. More...
 
 isPasswordTooLong (string $password)
 

Detailed Description

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

Constructor & Destructor Documentation

◆ __construct()

ilBcryptPhpPasswordEncoder::__construct ( array  $config = [])
Parameters
array<string,mixed>$config
Exceptions
ilPasswordException

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

References benchmarkCost(), and setCosts().

37  {
38  foreach ($config as $key => $value) {
39  if (strtolower($key) === self::COST) {
40  $this->setCosts($value);
41  }
42  }
43 
44  if (!isset($config[self::COST]) && static::class === self::class) {
45  // Determine the costs only if they are not passed in constructor
46  $this->setCosts((string) $this->benchmarkCost());
47  }
48  }
+ 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
Exceptions
ilPasswordException

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

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

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

◆ encodePassword()

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

Encodes the raw password.

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

Implements ilPasswordEncoder.

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

References ilBasePasswordEncoder\isPasswordTooLong().

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

90  : string
91  {
92  if ($this->isPasswordTooLong($raw)) {
93  throw new ilPasswordException('Invalid password.');
94  }
95 
96  return password_hash($raw, PASSWORD_BCRYPT, [
97  self::COST => $this->costs
98  ]);
99  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCosts()

ilBcryptPhpPasswordEncoder::getCosts ( )

◆ getName()

ilBcryptPhpPasswordEncoder::getName ( )

Returns a unique name/id of the concrete password encoder.

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPhpPasswordEncoderTest\testNameShouldBeBcryptPhp().

69  : string
70  {
71  return 'bcryptphp';
72  }
+ Here is the caller graph for this function:

◆ isPasswordValid()

ilBcryptPhpPasswordEncoder::isPasswordValid ( string  $encoded,
string  $raw,
string  $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, may be empty
Returns
Boolean true if the password is valid, false otherwise

Implements ilPasswordEncoder.

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

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

101  : bool
102  {
103  return password_verify($raw, $encoded);
104  }
+ Here is the caller graph for this function:

◆ requiresReencoding()

ilBcryptPhpPasswordEncoder::requiresReencoding ( string  $encoded)

Returns whether the encoded password needs to be re-encoded.

Implements ilPasswordEncoder.

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

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

106  : bool
107  {
108  return password_needs_rehash($encoded, PASSWORD_BCRYPT, [
109  self::COST => $this->costs
110  ]);
111  }
+ Here is the caller graph for this function:

◆ setCosts()

ilBcryptPhpPasswordEncoder::setCosts ( string  $costs)

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

References ILIAS\Repository\int().

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

79  : void
80  {
81  if ($costs !== '') {
82  $numeric_costs = (int) $costs;
83  if ($numeric_costs < 4 || $numeric_costs > 31) {
84  throw new ilPasswordException('The costs parameter of bcrypt must be in range 04-31');
85  }
86  $this->costs = sprintf('%1$02d', $numeric_costs);
87  }
88  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $costs

string ilBcryptPhpPasswordEncoder::$costs = '08'
private

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

Referenced by getCosts().

◆ COST

const ilBcryptPhpPasswordEncoder::COST = 'cost'
private

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


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