Back to UserHelper class

Method genRandomPassword

public static string
genRandomPassword
(mixed $length = 8)
Generate a random password
Parameters
  • int $length Length of the password to generate
Returns
  • string Random Password
Since
  • 1.7.0
Class: UserHelper
Project: Joomla

Method genRandomPassword - Source code

/**
 * Generate a random password
 *
 * @param   integer  $length  Length of the password to generate
 *
 * @return  string  Random Password
 *
 * @since   1.7.0
 */
public static function genRandomPassword($length = 8)
{
    $salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $base = \strlen($salt);
    $makepass = '';
    /*
     * Start with a cryptographic strength random string, then convert it to
     * a string with the numeric base of the salt.
     * Shift the base conversion on each character so the character
     * distribution is even, and randomize the start shift so it's not
     * predictable.
     */
    $random = Crypt::genRandomBytes($length + 1);
    $shift = \ord($random[0]);
    for ($i = 1; $i <= $length; ++$i) {
        $makepass .= $salt[($shift + \ord($random[$i])) % $base];
        $shift += \ord($random[$i]);
    }
    return $makepass;
}