检测密码强度

c1 : 长度>=8

c2: 包含数字和字母

c3: 其他可见的特殊字符

强:满足c1,c2,c3

中: 只满足任一2个条件

弱:只满足任一1个或0个条件'''

import re,string

#方法1:定义每个条件的标志位

def check_password_level(s):

      flag1 = False #长度是否大于8

      flag2 = False #是否包含数字

      flag3 = False #是否包含字母

      flag4 = False #是否包含其它可见字符

      result = []

      if len(s) >= 8:

            flag1 = True

      for i in s:

            if i.isalpha():

                  flag3 = True

            elif i.isdigit():

                  flag2 = True

            elif i in string.punctuation:

                  flag4 = True

      if  flag2 and flag3:

            result.append(True)

      else:

            result.append(False)

      result += [flag1,flag4]

      #True加法运算相当于1,false相当于0

      if sum(result)==3:

            print('强')

      elif sum(result)==2:

            print('中')

      else:

            print('弱')

check_password_level('123k.')

check_password_level('123.')

check_password_level('~youlo0418')

#方法2:全部用正则

def check_password_level(s):

      num = 0

      #既有数字又有字母的正则表达式

      re_alnum = r'(\d+)([a-zA-Z]+)|([a-zA-Z]+)(\d+)'

      re_special_charactor = r"[%s]+"%string.punctuation


      if  len(s)>=8 :

            num += 1

      if re.findall(re_alnum,s) :

            num += 1

      if re.findall(re_special_charactor,s):

            num += 1


      if num == 3:

            print("强")

      elif num == 2:

            print("中")

      else:

            print('弱')

check_password_level('~youlo0918')

check_password_level('^youlo@')

check_password_level('201909youlo')

#方法3:正则+filter

def check_password_level(s):

      level_dict = {3:"强",2:"中",1:'弱',0:'弱'}

      expression = bool(len(s)>=8) +bool(list(filter(lambda x:re.findall(r'(\d+)([a-zA-Z]+)|([a-zA-Z]+)(\d+)',x), [s]))) \

                  +bool( list(filter(lambda x:re.findall(r"[%s]+"%string.punctuation,x), [s])) )

      return level_dict[expression]

check_password_level('~youlo0418')

check_password_level('^youlo@')

check_password_level('201909youlo')


#方法4:string+filter

def check_password_level(s):

      level_dict = {3:"强",2:"中",1:'弱',0:'弱'}

      expression = bool(len(s)>=8) +bool(list(filter(lambda x: x in string.digits ,s)) and list(filter(lambda x: x in string.ascii_letters ,s))) \

                  +bool( list(filter(lambda x:x in string.punctuation, s)) )

      return level_dict[expression]

check_password_level('~youlo0418')

check_password_level('^youlo@')

check_password_level('201909youlo')

#方法5:

def check_password_level(s):

      c1 = len(s)>=8

      c2 = re.findall(r'.*[A-Za-z]+.*',s) != []  and re.findall(r'.*\d+.*',s) != []

      c3 = False

      for i in s:

            if i in string.punctuation:

                  c3 = True

                  break

      if c1 and c2 and c3:

            print('强')

      elif (c1 and c2) or(c1 and c3) or(c2 and c3):

            print('中')

      else:

            print('弱')

check_password_level('~youlo0418')

check_password_level('^youlo@')

check_password_level('201909youlo')

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容