-4

I want to have an php check for easy passwords patterns like

123456
123456789
abcde
98764321
101010
202020

Is it possible to have a check like this that doesn't rely in maintaining an array of pre-defined strings ?

Freedo
  • 121
  • 9
  • Just iterate to each letter and check if the next one is same, one less or one more as current. So you can create a score of uniqueness. – Markus Zeller Jun 02 '20 at 07:18
  • can you give an example? i'm new to php – Freedo Jun 02 '20 at 07:21
  • Check that for full problem description : [answered one](https://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords/401684) – blandger Jun 02 '20 at 07:40

1 Answers1

1

Just iterate to each letter and check if the next one is same, one less or one more as current. So you can create a score of uniqueness.

For last case you can check if there are duplicate characters and remove from score, too.

$passwords = ['123456', '123456789', 'abcde', '98764321', '101010', '202020', 'xhdgszu'];

foreach ($passwords as $password) {
    $score = $length = strlen($password);
    $chars = str_split($password);

    for ($i = 0; $i < $length - 1; $i++) {
        $currentChar = $chars[$i];
        $nextChar = $chars[$i + 1];
        if ($currentChar === $nextChar
            || ord($currentChar) - 1 === ord($nextChar)
            || ord($currentChar) + 1 === ord($nextChar)
        ) {
            $score--;
            continue;
        }
    }

    $unique = array_unique($chars);
    $score -= $length - count($unique);

    echo "{$password}: {$score} / {$length} ", ($score < $length) ? 'Bad' : 'Good', PHP_EOL;
}
123456: 1 / 6 Bad
123456789: 1 / 9 Bad
abcde: 1 / 5 Bad
98764321: 2 / 8 Bad
101010: -3 / 6 Bad
202020: 2 / 6 Bad
xhdgszu: 7 / 7 Good

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
  • looks good. but isn't possible to do this with just preg_match? I was thinking in something simpler...but thanks – Freedo Jun 02 '20 at 07:59
  • I don't think so. How will you compare in a regex and do scoring? You could put that in a function and this can return the score or a just true for good or false as bad. – Markus Zeller Jun 02 '20 at 08:28
  • This could be because this a "low quality" question. You always should share your code, what you've tried and where you stuck. – Markus Zeller Jun 02 '20 at 08:33