python 字符串实例:检查并判断密码字符串的安全强度
检查并判断密码字符串的安全强度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import string def check(pwd): #密码必须至少包含六个字符 if not isinstance (pwd, str ) or len (pwd)< 6 : return 'noot suitable for password' #密码强度等级与包含字符种类的对应关系 d = { 1 : 'weak' , 2 : 'below middle' , 3 : 'above middle' , 4 : 'strong' } #分别用来标记pwd是否含有数字、小写字母、大写字母、指定的标点符号 r = [ False ] * 4 pwd_range = string.ascii_uppercase + string.ascii_lowercase + string.digits + ',.!;><?' for ch in pwd: if ch not in pwd_range: return 'error' elif not r[ 0 ] and ch in string.digits: r[ 0 ] = True elif not r[ 1 ] and ch in string.ascii_lowercase: r[ 1 ] = True elif not r[ 2 ] and ch in string.ascii_uppercase: r[ 2 ] = True elif not r[ 3 ] and ch in ',.!;?<>' : r[ 3 ] = True #统计包含的字符种类,返回密码强度 return d.get(r.count( True ), 'error' ) def program(): while True : pwd = input ( "请输入您的密码:" ) print (check(pwd)) program() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步