为了账号安全,请及时绑定邮箱和手机立即绑定

使用 php 检查密码是否包含简单序列

使用 php 检查密码是否包含简单序列

PHP
长风秋雁 2023-04-28 14:51:29
我想用 php 检查简单的密码模式,比如123456123456789abcde98764321101010202020是否有可能进行这样的检查而不依赖于维护预定义字符串数组?
查看完整描述

1 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

只需遍历每个字母并检查下一个字母是否与当前字母相同,少一个或多一个。所以你可以创造一个独特的分数。


对于最后一种情况,您可以检查是否有重复字符并从乐谱中删除。


$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


查看完整回答
反对 回复 2023-04-28
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信