class MatchBase in Password Strength 8.2
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\password_strength\MatchBase
Expanded class hierarchy of MatchBase
File
- src/
MatchBase.php, line 7
Namespace
Drupal\password_strengthView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MatchBase:: |
protected | property | @var | |
MatchBase:: |
protected | function | Calculate binomial coefficient (n choose k). | |
MatchBase:: |
public static | function | Find all occurences of regular expression in a string. | |
MatchBase:: |
public | function | Get token's symbol space. | |
MatchBase:: |
public | function | ||
MatchBase:: |
protected | function | ||
MatchBase:: |
protected | function | ||
MatchBase:: |
protected | function | ||
MatchBase:: |
protected | function | ||
MatchBase:: |
protected | function | Calculate entropy. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |