| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Auth; |
|
|
| use Exception; |
| use Piwik\Config; |
|
|
| |
| |
| |
| |
| |
| class Password |
| { |
| |
| |
| |
| |
| |
| |
| private function preferredAlgorithm() |
| { |
| $passwordHashAlgorithm = Config::getInstance()->General['password_hash_algorithm']; |
| switch ($passwordHashAlgorithm) { |
| case "default": |
| return PASSWORD_DEFAULT; |
| case "bcrypt": |
| return PASSWORD_BCRYPT; |
| case "argon2i": |
| return PASSWORD_ARGON2I; |
| case "argon2id": |
| if (version_compare(PHP_VERSION, '7.3.0', '<')) { |
| throw new Exception("argon2id needs at leat PHP 7.3.0"); |
| } |
| return PASSWORD_ARGON2ID; |
| default: |
| throw new Exception("invalid password_hash_algorithm"); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| private function algorithmOptions() |
| { |
| $options = []; |
| $generalConfig = Config::getInstance()->General; |
| if ($generalConfig["password_hash_argon2_threads"] != "default") { |
| $options["threads"] = max($generalConfig["password_hash_argon2_threads"], 1); |
| } |
| if ($generalConfig["password_hash_argon2_memory_cost"] != "default") { |
| $options["memory_cost"] = max($generalConfig["password_hash_argon2_memory_cost"], 8 * $options["threads"]); |
| } |
| if ($generalConfig["password_hash_argon2_time_cost"] != "default") { |
| $options["time_cost"] = max($generalConfig["password_hash_argon2_time_cost"], 1); |
| } |
| return $options; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function hash( |
| #[\SensitiveParameter] |
| $password |
| ) { |
| return password_hash($password, $this->preferredAlgorithm(), $this->algorithmOptions()); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function info($hash) |
| { |
| return password_get_info($hash); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function needsRehash($hash) |
| { |
| return password_needs_rehash($hash, $this->preferredAlgorithm(), $this->algorithmOptions()); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function verify( |
| #[\SensitiveParameter] |
| $password, |
| #[\SensitiveParameter] |
| $hash |
| ) { |
| return password_verify($password, $hash); |
| } |
| } |
|
|