You are here

class MatchBase in Password Strength 8.2

Hierarchy

Expanded class hierarchy of MatchBase

File

src/MatchBase.php, line 7

Namespace

Drupal\password_strength
View source
class MatchBase extends PluginBase {

  /**
   * @var
   */
  protected $zxcvbn_matcher;

  /**
   * Find all occurences of regular expression in a string.
   *
   * @param string $string
   *   String to search.
   * @param string $regex
   *   Regular expression with captures.
   * @return array
   *   Array of capture groups. Captures in a group have named indexes: 'begin', 'end', 'token'.
   *     e.g. fishfish /(fish)/
   *     array(
   *       array(
   *         array('begin' => 0, 'end' => 3, 'token' => 'fish'),
   *         array('begin' => 0, 'end' => 3, 'token' => 'fish')
   *       ),
   *       array(
   *         array('begin' => 4, 'end' => 7, 'token' => 'fish'),
   *         array('begin' => 4, 'end' => 7, 'token' => 'fish')
   *       )
   *     )
   *
   */
  public static function findAll($string, $regex) {
    $count = preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
    if (!$count) {
      return array();
    }
    $pos = 0;
    $groups = array();
    foreach ($matches as $group) {
      $captureBegin = 0;
      $match = array_shift($group);
      $matchBegin = strpos($string, $match, $pos);
      $captures = array(
        array(
          'begin' => $matchBegin,
          'end' => $matchBegin + strlen($match) - 1,
          'token' => $match,
        ),
      );
      foreach ($group as $capture) {
        $captureBegin = strpos($match, $capture, $captureBegin);
        $captures[] = array(
          'begin' => $matchBegin + $captureBegin,
          'end' => $matchBegin + $captureBegin + strlen($capture) - 1,
          'token' => $capture,
        );
      }
      $groups[] = $captures;
      $pos += strlen($match) - 1;
    }
    return $groups;
  }

  /**
   * Get token's symbol space.
   *
   * @return int
   */
  public function getCardinality() {
    if (!is_null($this->cardinality)) {
      return $this->cardinality;
    }
    $lower = $upper = $digits = $symbols = $unicode = 0;

    // Use token instead of password to support bruteforce matches on sub-string
    // of password.
    $chars = str_split($this->token);
    foreach ($chars as $char) {
      $ord = ord($char);
      if ($this
        ->isDigit($ord)) {
        $digits = 10;
      }
      elseif ($this
        ->isUpper($ord)) {
        $upper = 26;
      }
      elseif ($this
        ->isLower($ord)) {
        $lower = 26;
      }
      elseif ($this
        ->isSymbol($ord)) {
        $symbols = 33;
      }
      else {
        $unicode = 100;
      }
    }
    $this->cardinality = $lower + $digits + $upper + $symbols + $unicode;
    return $this->cardinality;
  }
  protected function isDigit($ord) {
    return $ord >= 0x30 && $ord <= 0x39;
  }
  protected function isUpper($ord) {
    return $ord >= 0x41 && $ord <= 0x5a;
  }
  protected function isLower($ord) {
    return $ord >= 0x61 && $ord <= 0x7a;
  }
  protected function isSymbol($ord) {
    return $ord <= 0x7f;
  }

  /**
   * Calculate entropy.
   *
   * @param $number
   * @return float
   */
  protected function log($number) {
    return log($number, 2);
  }

  /**
   * Calculate binomial coefficient (n choose k).
   *
   * http://www.php.net/manual/en/ref.math.php#57895
   *
   * @param $n
   * @param $k
   * @return int
   */
  protected function binom($n, $k) {
    $j = $res = 1;
    if ($k < 0 || $k > $n) {
      return 0;
    }
    if ($n - $k < $k) {
      $k = $n - $k;
    }
    while ($j <= $k) {
      $res *= $n--;
      $res /= $j++;
    }
    return $res;
  }

  /**
   * @return float
   */
  public function getEntropy() {
    return $this->zxcvbn_matcher
      ->getEntropy();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MatchBase::$zxcvbn_matcher protected property @var
MatchBase::binom protected function Calculate binomial coefficient (n choose k).
MatchBase::findAll public static function Find all occurences of regular expression in a string.
MatchBase::getCardinality public function Get token's symbol space.
MatchBase::getEntropy public function
MatchBase::isDigit protected function
MatchBase::isLower protected function
MatchBase::isSymbol protected function
MatchBase::isUpper protected function
MatchBase::log protected function Calculate entropy.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92