ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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...
 
 encodePassword ($raw, $salt)
 Encodes the raw password. More...
 
 isPasswordValid ($encoded, $raw, $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 ($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 ($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

Reimplemented in ilBcryptPasswordEncoder.

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

23 {
24 if(!empty($config))
25 {
26 foreach($config as $key => $value)
27 {
28 switch(strtolower($key))
29 {
30 case 'cost':
31 $this->setCosts($value);
32 break;
33 }
34 }
35 }
36
37 if(!isset($config['cost']) && static::class == self::class)
38 {
39 // Determine the costs only if they are not passed in constructor
40 $this->setCosts($this->benchmarkCost(0.05));
41 }
42
43 $this->init();
44 }

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

+ 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 58 of file class.ilBcryptPhpPasswordEncoder.php.

59 {
60 $cost = 8;
61
62 do
63 {
64 $cost++;
65 $start = microtime(true);
66 $encoder = new self(array('cost' => $cost));
67 $encoder->encodePassword('test', '');
68 $end = microtime(true);
69 }
70 while(($end - $start) < $time_target && $cost < 32);
71
72 return $cost;
73 }

References $start.

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

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

Reimplemented in ilBcryptPasswordEncoder.

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

122 {
123 if($this->isPasswordTooLong($raw))
124 {
125 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
126 throw new ilPasswordException('Invalid password.');
127 }
128
129 return password_hash($raw, PASSWORD_BCRYPT, array(
130 'cost' => $this->getCosts()
131 ));
132 }
isPasswordTooLong($password)
Checks if the password is too long.
Class for user password exception handling in ILIAS.

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

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

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

◆ getCosts()

◆ getName()

ilBcryptPhpPasswordEncoder::getName ( )
Returns
string

Implements ilPasswordEncoder.

Reimplemented in ilBcryptPasswordEncoder.

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

79 {
80 return 'bcryptphp';
81 }

Referenced by ilBcryptPhpPasswordEncoderTest\testNameShouldBeBcryptPhp().

+ Here is the caller graph for this function:

◆ init()

ilBcryptPhpPasswordEncoder::init ( )
protected

Reimplemented in ilBcryptPasswordEncoder.

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

50 {
51 }

Referenced by __construct().

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

Reimplemented in ilBcryptPasswordEncoder.

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

138 {
139 return password_verify($raw, $encoded);
140 }

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

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

Reimplemented from ilBasePasswordEncoder.

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

87 {
88 return parent::isSupportedByRuntime() && version_compare(phpversion(), '5.5.0', '>=');
89 }

◆ requiresReencoding()

ilBcryptPhpPasswordEncoder::requiresReencoding (   $encoded)

{{Returns whether or not the a encoded password needs to be re-encoded.

Parameters
$encodedstring
Returns
boolean
}}

Reimplemented from ilBasePasswordEncoder.

Reimplemented in ilBcryptPasswordEncoder.

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

146 {
147 return password_needs_rehash($encoded, PASSWORD_BCRYPT, array(
148 'cost' => $this->getCosts()
149 ));
150 }

References getCosts().

Referenced by ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

+ 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 103 of file class.ilBcryptPhpPasswordEncoder.php.

104 {
105 if(!empty($costs))
106 {
107 $costs = (int)$costs;
108 if($costs < 4 || $costs > 31)
109 {
110 require_once 'Services/Password/exceptions/class.ilPasswordException.php';
111 throw new ilPasswordException('The costs parameter of bcrypt must be in range 04-31');
112 }
113 $this->costs = sprintf('%1$02d', $costs);
114 }
115 }
sprintf('%.4f', $callTime)

References $costs, and sprintf.

Referenced by __construct(), ilBcryptPasswordEncoderTest\testCostsCanBeRetrievedWhenCostsAreSet(), ilBcryptPhpPasswordEncoderTest\testCostsCanBeRetrievedWhenCostsAreSet(), ilBcryptPasswordEncoderTest\testCostsCanBeSetInRange(), ilBcryptPhpPasswordEncoderTest\testCostsCanBeSetInRange(), ilBcryptPasswordEncoderTest\testCostsCannotBeSetAboveRange(), ilBcryptPhpPasswordEncoderTest\testCostsCannotBeSetAboveRange(), ilBcryptPasswordEncoderTest\testCostsCannotBeSetBelowRange(), ilBcryptPhpPasswordEncoderTest\testCostsCannotBeSetBelowRange(), ilBcryptPasswordEncoderTest\testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(), ilBcryptPhpPasswordEncoderTest\testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(), ilBcryptPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified(), ilBcryptPhpPasswordEncoderTest\testPasswordShouldBeCorrectlyEncodedAndVerified(), ilBcryptPasswordEncoderTest\testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(), ilBcryptPhpPasswordEncoderTest\testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(), and ilBcryptPhpPasswordEncoderTest\testReencodingIsDetectedWhenNecessary().

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