ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 ()
 @inheritDoc More...
 
 getCosts ()
 
 setCosts (string $costs)
 
 encodePassword (string $raw, string $salt)
 @inheritDoc More...
 
 isPasswordValid (string $encoded, string $raw, string $salt)
 @inheritDoc More...
 
 requiresReencoding (string $encoded)
 @inheritDoc More...
 
- Public Member Functions inherited from ilBasePasswordEncoder
 isSupportedByRuntime ()
 @inheritDoc More...
 
 requiresSalt ()
 @inheritDoc More...
 
 requiresReencoding (string $encoded)
 @inheritDoc More...
 
 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...
 
 getName ()
 Returns a unique name/id of the concrete password encoder. More...
 
 requiresSalt ()
 Returns whether or not the encoder requires a salt. More...
 
 requiresReencoding (string $encoded)
 Returns whether or not the a encoded password needs to be re-encoded. More...
 
 isSupportedByRuntime ()
 Returns whether or not the encoder is supported by the runtime (PHP, HHVM, ...) More...
 

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

Reimplemented in ilBcryptPasswordEncoder.

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

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

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

+ 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.

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 }

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

+ Here is the caller graph for this function:

◆ encodePassword()

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

@inheritDoc

Exceptions
ilPasswordException

Implements ilPasswordEncoder.

Reimplemented in ilBcryptPasswordEncoder.

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

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 }
isPasswordTooLong(string $password)
Checks if the password is too long.
Class for user password exception handling in ILIAS.

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

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

+ 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.

Reimplemented in ilBcryptPasswordEncoder.

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

73 : string
74 {
75 return 'bcryptphp';
76 }

Referenced by ilBcryptPhpPasswordEncoderTest\testNameShouldBeBcryptPhp().

+ Here is the caller graph for this function:

◆ init()

ilBcryptPhpPasswordEncoder::init ( )
protected

Reimplemented in ilBcryptPasswordEncoder.

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

45 : void
46 {
47 }

Referenced by __construct().

+ Here is the caller graph for this function:

◆ isPasswordValid()

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

@inheritDoc

Implements ilPasswordEncoder.

Reimplemented in ilBcryptPasswordEncoder.

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

127 : bool
128 {
129 return password_verify($raw, $encoded);
130 }

◆ isSupportedByRuntime()

ilBcryptPhpPasswordEncoder::isSupportedByRuntime ( )

@inheritDoc

Reimplemented from ilBasePasswordEncoder.

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)

@inheritDoc

Reimplemented from ilBasePasswordEncoder.

Reimplemented in ilBcryptPasswordEncoder.

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

135 : bool
136 {
137 return password_needs_rehash($encoded, PASSWORD_BCRYPT, [
138 'cost' => $this->getCosts()
139 ]);
140 }

References getCosts().

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCosts()

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: