描述 Description

编写一个函数,接收一个字符串作为参数,判断该字符串作为密码的话其安全强度如何。如果字符串中只包含大写字母、小写字母、数字字符或标点符号中的一种则为弱密码,包含两种为中低安全密码,包含三种为中高安全密码,包含四种则为强密码。(额外要求:不可以使用正则表达式)

[http://ask.csdn.net/questions/388579\]

分析 Analysis

见代码注释

代码 Code

import string

def check(pwd):
    #密码必须至少包含6个字符
    if not isinstance(pwd, str) or len(pwd)<6:
        return 'not suitable for password'
    #密码强度等级与包含字符种类的对应关系
    d = {1:'weak', 2:'below middle', 3:'above middle', 4:'strong'}
    #分别用来标记pwd是否含有数字、小写字母、大写字母和指定的标点符号
    r = [False] * 4
    for ch in pwd:
        #是否包含数字
        if 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')

results matching ""

    No results matching ""