背景:
应客户爸爸要求,需要写一个判断密码复杂度的脚本。策略要求如下:
在这里插入图片描述
我的脚本主要满足1、2、5的要求,最终效果如下:
在这里插入图片描述
代码:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Time     :2020/7/14 15:43
# @Author   :Donvin.li
# @File     :password_policy.py

import re

# 输入函数
def main():
    while True:
        pwd = input('[*]请输入密码:')
        check_password_base(pwd)
        continue

# 判断是否密码是否至少8位且必须包含大小写字母和数字
def check_password_base(pwd):
    zz_str = '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$'
    re1 = re.search(zz_str, pwd)
    if not re1:
        print('[*]密码至少8位且必须包含大小写字母和数字')
    else:
        check_password_special(pwd)

def check_password_special(pwd):
    list_special = ['admin', 'root', 'crld', 'crland', 'test', 'hello', '147258', '147369', '258369']
    x=len(list_special)-1
    for pwd_special in list_special:
        if pwd_special in pwd.lower():
            print('[*]密码不能包含易猜解字符:'+str(pwd_special))
            break
        else:
            if pwd_special==list_special[x]:
                check_password_adv(pwd)

# 判断是否是连续、重复以及易猜解
def check_password_adv(pwd):
    str_all = '1234567890-=' \
              '=-0987654321' \
              '!@#$%^&*()_+' \
              '+_)(*&^%$#@!' \
              'abcdefghijklmnopqrstuvwxyz' \
              'zyxwvutsrqponmlkjihgfedcba' \
              'qwertyuiopasdfghjklzxcvbnm' \
              'mnbvcxzlkjhgfdsapoiuytrewq' \
              '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/'
    pwd_len=len(pwd)
    x = -1
    y = x+3
    while y < pwd_len:
        x+=1
        y+=1
        pwd_cut = pwd[x:y]
        #print(pwd_cut)
        if pwd_cut.lower() in str_all and len(pwd_cut) == 3:  # 无论是大写还是小写都统统转换为小写,为了匹配大写
            print('[*]密码不能包含3位以上连续字符:'+str(pwd_cut))
            break
        elif len(pwd_cut) == 3 and pwd_cut[0].lower() == pwd_cut[1].lower() == pwd_cut[2].lower():
            print('[*]密码不能包含3位以上重复字符:'+str(pwd_cut))
            break
        else:
            if y==pwd_len:
                print('[*]密码复杂度合格')

if __name__=='__main__':
    main()

结语:
仅仅为了满足客户策略,代码可能并不严谨,会有误判,比如类似“IuYngd740*%#”的强密码会被检测出来;再比如“Aa112233”这种弱密码又检测不出来。欢迎大佬扶正。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐