ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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...
 
 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 the encoder requires a salt. More...
 
 requiresReencoding (string $encoded)
 Returns whether the encoded password needs to be re-encoded. More...
 
 isSupportedByRuntime ()
 Returns whether the encoder is supported by the runtime (PHP, HHVM, ...) More...
 

Private Attributes

const string 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

Reimplemented in ilBcryptPasswordEncoder.

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

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 }

References benchmarkCost(), 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
Exceptions
ilPasswordException

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

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 }

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

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

Reimplemented in ilBcryptPasswordEncoder.

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

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 }
Class for user password exception handling in ILIAS.

References 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 a unique name/id of the concrete password encoder.

Implements ilPasswordEncoder.

Reimplemented in ilBcryptPasswordEncoder.

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

69 : string
70 {
71 return 'bcryptphp';
72 }

Referenced by ilBcryptPhpPasswordEncoderTest\testNameShouldBeBcryptPhp().

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

Reimplemented in ilBcryptPasswordEncoder.

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

101 : bool
102 {
103 return password_verify($raw, $encoded);
104 }

◆ requiresReencoding()

ilBcryptPhpPasswordEncoder::requiresReencoding ( string  $encoded)

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

Reimplemented from ilBasePasswordEncoder.

Reimplemented in ilBcryptPasswordEncoder.

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

106 : bool
107 {
108 return password_needs_rehash($encoded, PASSWORD_BCRYPT, [
109 self::COST => $this->costs
110 ]);
111 }

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

+ Here is the caller graph for this function:

◆ setCosts()

Field Documentation

◆ $costs

string ilBcryptPhpPasswordEncoder::$costs = '08'
private

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

Referenced by getCosts(), and setCosts().

◆ COST

const string ilBcryptPhpPasswordEncoder::COST = 'cost'
private

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


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