ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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, $key, 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($this->benchmarkCost(0.05));
37  }
38 
39  $this->init();
40  }
$config
Definition: bootstrap.php:15
$key
Definition: croninfo.php:18
+ 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 54 of file class.ilBcryptPhpPasswordEncoder.php.

References $end, and $start.

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

55  {
56  $cost = 8;
57 
58  do {
59  $cost++;
60  $start = microtime(true);
61  $encoder = new self(array('cost' => $cost));
62  $encoder->encodePassword('test', '');
63  $end = microtime(true);
64  } while (($end - $start) < $time_target && $cost < 32);
65 
66  return $cost;
67  }
$start
Definition: bench.php:8
+ 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 113 of file class.ilBcryptPhpPasswordEncoder.php.

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

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

114  {
115  if ($this->isPasswordTooLong($raw)) {
116  require_once 'Services/Password/exceptions/class.ilPasswordException.php';
117  throw new ilPasswordException('Invalid password.');
118  }
119 
120  return password_hash($raw, PASSWORD_BCRYPT, array(
121  'cost' => $this->getCosts()
122  ));
123  }
Class for user password exception handling in ILIAS.
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 72 of file class.ilBcryptPhpPasswordEncoder.php.

Referenced by ilBcryptPhpPasswordEncoderTest\testNameShouldBeBcryptPhp().

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

◆ init()

ilBcryptPhpPasswordEncoder::init ( )
protected

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

Referenced by __construct().

46  {
47  }
+ 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 128 of file class.ilBcryptPhpPasswordEncoder.php.

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

129  {
130  return password_verify($raw, $encoded);
131  }
+ 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 80 of file class.ilBcryptPhpPasswordEncoder.php.

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

◆ 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 136 of file class.ilBcryptPhpPasswordEncoder.php.

References getCosts().

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

137  {
138  return password_needs_rehash($encoded, PASSWORD_BCRYPT, array(
139  'cost' => $this->getCosts()
140  ));
141  }
+ 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 97 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().

98  {
99  if (!empty($costs)) {
100  $costs = (int) $costs;
101  if ($costs < 4 || $costs > 31) {
102  require_once 'Services/Password/exceptions/class.ilPasswordException.php';
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(), and setCosts().


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