You are here

class PasswordStrength in Password Strength 8.2

Same name in this branch
  1. 8.2 src/PasswordStrength.php \Drupal\password_strength\PasswordStrength
  2. 8.2 src/Plugin/PasswordConstraint/PasswordStrength.php \Drupal\password_strength\Plugin\PasswordConstraint\PasswordStrength

Hierarchy

Expanded class hierarchy of PasswordStrength

File

src/PasswordStrength.php, line 8

Namespace

Drupal\password_strength
View source
class PasswordStrength {

  /**
   * @var
   */
  protected $scorer;

  /**
   * @var
   */
  protected $searcher;

  /**
   * @var
   */
  protected $matcher;
  public function __construct() {
    $this->searcher = new Searcher();
    $this->scorer = new Scorer();
    $this->matcher = new Matcher();
  }

  /**
   * Calculate password strength via non-overlapping minimum entropy patterns.
   *
   * @param string $password
   *   Password to measure.
   * @param array $userInputs
   *   Optional user inputs.
   *
   * @return array
   *   Strength result array with keys:
   *     password
   *     entropy
   *     match_sequence
   *     score
   */
  public function passwordStrength($password, array $userInputs = array()) {
    $timeStart = microtime(TRUE);
    if (strlen($password) === 0) {
      $timeStop = microtime(TRUE) - $timeStart;
      return $this
        ->result($password, 0, array(), 0, array(
        'calc_time' => $timeStop,
      ));
    }

    // Get matches for $password.
    $matches = $this->matcher
      ->getMatches($password, $userInputs);

    // Calcuate minimum entropy and get best match sequence.
    $entropy = $this->searcher
      ->getMinimumEntropy($password, $matches);
    $bestMatches = $this->searcher->matchSequence;

    // Calculate score and get crack time.
    $score = $this->scorer
      ->score($entropy);
    $metrics = $this->scorer
      ->getMetrics();
    $timeStop = microtime(TRUE) - $timeStart;

    // Include metrics and calculation time.
    $params = array_merge($metrics, array(
      'calc_time' => $timeStop,
    ));
    return $this
      ->result($password, $entropy, $bestMatches, $score, $params);
  }

  /**
   * Result array.
   *
   * @param string $password
   * @param float $entropy
   * @param array $matches
   * @param int $score
   * @param array $params
   *
   * @return array
   */
  protected function result($password, $entropy, $matches, $score, $params = array()) {
    $r = array(
      'password' => $password,
      'entropy' => $entropy,
      'match_sequence' => $matches,
      'score' => $score,
    );
    return array_merge($params, $r);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PasswordStrength::$matcher protected property @var
PasswordStrength::$scorer protected property @var
PasswordStrength::$searcher protected property @var
PasswordStrength::passwordStrength public function Calculate password strength via non-overlapping minimum entropy patterns.
PasswordStrength::result protected function Result array.
PasswordStrength::__construct public function