config = $config; } /** * Hash a password. */ public function hash(string $password, bool $useMd5 = true): string { $salt = $this->getSalt(); if ($useMd5) { $password = md5($password); } $hash = crypt($password, $salt); return str_replace($salt, '', $hash); } /** * Get a salt from the config and normalize it. */ protected function getSalt(): string { $salt = $this->config->get('passwordSalt'); if (!isset($salt)) { throw new RuntimeException('Option "passwordSalt" does not exist in config.php'); } return $this->normalizeSalt($salt); } /** * Convert salt in format in accordance to $saltFormat. */ protected function normalizeSalt(string $salt): string { return str_replace("{0}", $salt, $this->saltFormat); } /** * Generate a new salt. */ public function generateSalt(): string { return substr(md5(uniqid()), 0, 16); } }