Optimized some functions

This commit is contained in:
Netkas 2021-09-16 18:01:11 -04:00
parent 82271fb9e7
commit eb89762360
3 changed files with 67 additions and 69 deletions

View File

@ -1,36 +1,17 @@
<?php
namespace Zxcvbn\Abstracts;
/** @noinspection PhpMissingFieldTypeInspection */
namespace Zxcvbn\Abstracts;
use Zxcvbn\Interfaces\MatchInterface;
abstract class BaseMatch implements MatchInterface
{
/**
* @var
*/
public $password;
/**
* @var
*/
public $begin;
/**
* @var
*/
public $end;
/**
* @var
*/
public $token;
/**
* @var
*/
public $pattern;
/**
@ -48,52 +29,29 @@
}
/**
* Get feedback to a user based on the match.
*
* @param bool $isSoleMatch
* Whether this is the only match in the password
* @return array
* Associative array with warning (string) and suggestions (array of strings)
*/
abstract public function getFeedback($isSoleMatch);
/**
* Find all occurences of regular expression in a string.
* Find all occurrences of regular expression in a string.
*
* @param string $string
* String to search.
* @param string $regex
* Regular expression with captures.
* @param int $offset
* @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')
* )
* )
* @noinspection PhpUnused
*/
public static function findAll($string, $regex, $offset = 0)
public static function findAll(string $string, string $regex, int $offset = 0): array
{
// $offset is the number of multibyte-aware number of characters to offset, but the offset parameter for
// preg_match_all counts bytes, not characters: to correct this, we need to calculate the byte offset and pass
// that in instead.
$charsBeforeOffset = mb_substr($string, 0, $offset);
$byteOffset = strlen($charsBeforeOffset);
$count = preg_match_all($regex, $string, $matches, PREG_SET_ORDER, $byteOffset);
if (!$count) {
if (!$count)
{
return [];
}
$groups = [];
foreach ($matches as $group) {
foreach ($matches as $group)
{
$captureBegin = 0;
$match = array_shift($group);
$matchBegin = mb_strpos($string, $match, $offset);
@ -104,7 +62,8 @@
'token' => $match,
],
];
foreach ($group as $capture) {
foreach ($group as $capture)
{
$captureBegin = mb_strpos($match, $capture, $captureBegin);
$captures[] = [
'begin' => $matchBegin + $captureBegin,
@ -121,13 +80,13 @@
/**
* Calculate binomial coefficient (n choose k).
*
* http://www.php.net/manual/en/ref.math.php#57895
*
* @param $n
* @param $k
* @return int
* @noinspection PhpUnused
* @noinspection SpellCheckingInspection
*/
public static function binom($n, $k)
public static function binom($n, $k): int
{
$j = $res = 1;
@ -145,26 +104,42 @@
return $res;
}
/**
* @return mixed
*/
abstract protected function getRawGuesses();
/**
* @return int|mixed
*/
public function getGuesses()
{
return max($this->getRawGuesses(), $this->getMinimumGuesses());
}
protected function getMinimumGuesses()
/**
* @return int
*/
protected function getMinimumGuesses(): int
{
if (mb_strlen($this->token) < mb_strlen($this->password)) {
if (mb_strlen($this->token) === 1) {
return Scorer::MIN_SUBMATCH_GUESSES_SINGLE_CHAR;
} else {
return Scorer::MIN_SUBMATCH_GUESSES_MULTI_CHAR;
if (mb_strlen($this->token) < mb_strlen($this->password))
{
if (mb_strlen($this->token) === 1)
{
return Score::MIN_SUBMATCH_GUESSES_SINGLE_CHAR;
}
else
{
return Score::MIN_SUBMATCH_GUESSES_MULTI_CHAR;
}
}
return 0;
}
public function getGuessesLog10()
/**
* @return float
*/
public function getGuessesLog10(): float
{
return log10($this->getGuesses());
}

View File

@ -1,6 +1,6 @@
<?php
<?php /** @noinspection PhpMissingFieldTypeInspection */
namespace Zxcvbn\Classes\Matchers;
namespace Zxcvbn\Classes\Matchers;
use Zxcvbn\Abstracts\BaseMatch;
@ -9,10 +9,6 @@
class Bruteforce extends BaseMatch
{
public const BRUTEFORCE_CARDINALITY = 10;
/**
* @var string
*/
public $pattern = 'bruteforce';
/**

View File

@ -1,5 +1,7 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace Zxcvbn\Objects;
class GuessableMatchSequence
@ -24,4 +26,29 @@
'sequence' => $this->Sequence
];
}
/**
* Constructs an object from an array representation
*
* @param array $data
* @return GuessableMatchSequence
*/
public static function fromArray(array $data): GuessableMatchSequence
{
$GuessableMatchSequence = new GuessableMatchSequence();
if(isset($data['password']))
$GuessableMatchSequence->Password = $data['password'];
if(isset($data['guesses']))
$GuessableMatchSequence->Guesses = $data['guesses'];
if(isset($data['guesses_log10']))
$GuessableMatchSequence->GuessesLog10 = $data['guesses_log10'];
if(isset($data['sequence']))
$GuessableMatchSequence->Sequence = $data['sequence'];
return $GuessableMatchSequence;
}
}