You are here

class DefaultPasswordGenerator in Drupal 9

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Password/DefaultPasswordGenerator.php \Drupal\Core\Password\DefaultPasswordGenerator

Provides a default password generator.

Hierarchy

Expanded class hierarchy of DefaultPasswordGenerator

1 file declares its use of DefaultPasswordGenerator
DefaultPasswordGeneratorTest.php in core/tests/Drupal/Tests/Core/Password/DefaultPasswordGeneratorTest.php
1 string reference to 'DefaultPasswordGenerator'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses DefaultPasswordGenerator
password_generator in core/core.services.yml
Drupal\Core\Password\DefaultPasswordGenerator

File

core/lib/Drupal/Core/Password/DefaultPasswordGenerator.php, line 8

Namespace

Drupal\Core\Password
View source
class DefaultPasswordGenerator implements PasswordGeneratorInterface {

  /**
   * The allowed characters for the password.
   *
   * Note that the number 0 and the letter 'O' have been removed to avoid
   * confusion between the two. The same is true of 'I', 1, and 'l'.
   *
   * @var string
   */
  protected $allowedChars = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';

  /**
   * Generates a password.
   *
   * @param int $length
   *   (optional) The length of the password.
   *
   * @return string
   *   The password.
   */
  public function generate(int $length = 10) : string {

    // The maximum integer we want from random_int().
    $max = strlen($this->allowedChars) - 1;
    $pass = '';
    for ($i = 0; $i < $length; $i++) {
      $pass .= $this->allowedChars[random_int(0, $max)];
    }
    return $pass;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultPasswordGenerator::$allowedChars protected property The allowed characters for the password.
DefaultPasswordGenerator::generate public function Generates a password. Overrides PasswordGeneratorInterface::generate