Back to UserHelper class

Method hashPassword

public static string
hashPassword
(mixed $password, mixed $algorithm = self::HASH_BCRYPT, array $options = array())
Hashes a password using the current encryption.
Parameters
  • string $password The plaintext password to encrypt.
  • string|int $algorithm The hashing algorithm to use, represented by `HASH_*` class constants, or a container service ID.
  • array $options The options for the algorithm to use.
Returns
  • string The encrypted password.
Since
  • 3.2.1
-
  • \InvalidArgumentException when the algorithm is not supported
Class: UserHelper
Project: Joomla

Method hashPassword - Source code

/**
 * Hashes a password using the current encryption.
 *
 * @param   string          $password   The plaintext password to encrypt.
 * @param   string|integer  $algorithm  The hashing algorithm to use, represented by `HASH_*` class constants, or a container service ID.
 * @param   array           $options    The options for the algorithm to use.
 *
 * @return  string  The encrypted password.
 *
 * @since   3.2.1
 * @throws  \InvalidArgumentException when the algorithm is not supported
 */
public static function hashPassword($password, $algorithm = self::HASH_BCRYPT, array $options = array())
{
    $container = Factory::getContainer();
    // If the algorithm is a valid service ID, use that service to generate the hash
    if ($container->has($algorithm)) {
        return $container->get($algorithm)->hashPassword($password, $options);
    }
    // Try to load handler
    if (isset(self::HASH_ALGORITHMS[$algorithm])) {
        return $container->get(self::HASH_ALGORITHMS[$algorithm])->hashPassword($password, $options);
    }
    // Unsupported algorithm, sorry!
    throw new \InvalidArgumentException(sprintf('The %s algorithm is not supported for hashing passwords.', $algorithm));
}