检查密码的强度(如何检查条件)

时间:2013-05-23 08:59:42

标签: python python-3.x

我正在尝试创建一个要求您输入密码的系统。如果它全部较低,则较高或数字则打印较弱,如果是两个条件,那么它是med,如果所有条件都满足则很强。它似乎没有用。

弱势和强势的工作,但媒体没有。

我不知道我哪里出错了。

def password():

    print ('enter password')
    print ()
    print ()
    print ('the password must be at least 6, and no more than 12 characters long')
    print ()

    password = input ('type your password    ....')


    weak = 'weak'
    med = 'medium'
    strong = 'strong'

    if len(password) >12:
        print ('password is too long It must be between 6 and 12 characters')

    elif len(password) <6:
        print ('password is too short It must be between 6 and 12 characters')


    elif len(password)    >=6 and len(password) <= 12:
        print ('password ok')

        if password.lower()== password or password.upper()==password or password.isalnum()==password:
            print ('password is', weak)

        elif password.lower()== password and password.upper()==password or password.isalnum()==password:
            print ('password is', med)

        else:
            password.lower()== password and password.upper()==password and password.isalnum()==password
            print ('password is', strong)

4 个答案:

答案 0 :(得分:4)

password.isalnum()会返回一个布尔值,因此password.isalnum()==password 总是False

只需省略==password部分:

if password.lower()== password or password.upper()==password or password.isalnum():
    # ...

接下来,它可以从不全部为高位和低位,或全部为高位和数字或全部为低位和所有数字,因此第二个条件(中等)是不可能的。也许你应该寻找一些大写,小写和数字的存在?

然而,要解决的第一个问题。您正在测试密码是否为字母数字,仅包含字符和/或数字。如果您只想测试数字,请使用.isdigit()

您可能希望熟悉string methods。您可以尝试使用方便的.islower().isupper()方法,例如:

>>> 'abc'.islower()
True
>>> 'abc123'.islower()
True
>>> 'Abc123'.islower()
False
>>> 'ABC'.isupper()
True
>>> 'ABC123'.isupper()
True
>>> 'Abc123'.isupper()
False

使用password.upper() == password这些更快,更简洁,以下将测试相同:

if password.isupper() or password.islower() or password.isdigit():
    # very weak indeed

您想学习的下一个技巧是循环一个字符串,这样您就可以测试单个字符:

>>> [c.isdigit() for c in 'abc123']
[False, False, False, True, True, True]

如果将其与any()函数结合使用,则可以测试某些字符是否为数字:

>>> any(c.isdigit() for c in 'abc123')
True
>>> any(c.isdigit() for c in 'abc')
False

我认为在测试密码强度时,你会发现这些技巧很方便。

答案 1 :(得分:2)

以下是您所写内容的翻版:

import re

def password():
    print ('Enter a password\n\nThe password must be between 6 and 12 characters.\n')

    while True:
        password = input('Password: ... ')
        if 6 <= len(password) < 12:
            break
        print ('The password must be between 6 and 12 characters.\n')

    password_scores = {0:'Horrible', 1:'Weak', 2:'Medium', 3:'Strong'}
    password_strength = dict.fromkeys(['has_upper', 'has_lower', 'has_num'], False)
    if re.search(r'[A-Z]', password):
        password_strength['has_upper'] = True
    if re.search(r'[a-z]', password):
        password_strength['has_lower'] = True
    if re.search(r'[0-9]', password):
        password_strength['has_num'] = True

    score = len([b for b in password_strength.values() if b])

    print ('Password is %s' % password_scores[score])

输出(样本):

>>> password()
Enter a password

The password must be between 6 and 12 characters.

Password: ... ghgG234
Password is Strong

答案 2 :(得分:1)

我也在寻找一些密码强度检查功能,并找到了很多半工作的建议。我基于它组合了我自己的功能。

希望能帮助

def get_pw_strength( pw ):

    s_lc = set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l', 'o', 'n', 'q', 'p', 's', 'r', 'u', 't', 'w', 'v', 'y', 'x', 'z'])
    s_uc = set(['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'J', 'M', 'L', 'O', 'N', 'Q', 'P', 'S', 'R', 'U', 'T', 'W', 'V', 'Y', 'X', 'Z'])
    s_dg = set(['1', '0', '3', '2', '5', '4', '7', '6', '9', '8'])
    s_sp = set(['+', ',', '.', '-', '?', ':', '_', '(', ')', '*', '/', ';', '+', '!'])
    pw_s = 0
    pw_steps = (5, 8, 12) 

    pw_l = len(pw)
    if ( pw_l < 4 ):
        return 0
    for l in pw_steps :
        if ( pw_l > l ):
            pw_s += 1
            #print "length over ", l," giving point", pw_s

    c_lc = c_uc = c_dg = c_sp = 0
    for c in pw :
        if ( c in s_lc ) :
            c_lc += 1
        if ( c in s_uc ) :
            c_uc += 1
        if ( c in s_dg ) :
            c_dg += 1
        if ( c in s_sp ) :
            c_sp += 1
    if ( c_lc + c_uc + c_dg + c_sp  <> pw_l ):
        #print c_lc, c_uc, c_dg, c_sp, pw_l
        #raise Exception "Forbidden chracter"
        return -1
    charset = 0
    if ( c_lc ) :
        pw_s += 1
        charset = len(s_lc)
    if ( c_uc ) :
        pw_s += 1
        charset = len(s_uc)
    if ( c_dg ) :
        pw_s += 1
        charset = len(s_dg)
    if ( c_sp ) :
        pw_s += 2
        charset = len(s_sp)
    entropy = log(pow(charset,pw_l),2)

    return pw_s, entropy

答案 3 :(得分:0)

您可以编写一个简单的“if 语句”来检查“密码的强度”,您可以在需要的地方使用它并根据您的要求进行更改:

password = "mypassword_Q@1"

if len(password) < 8 or password.lower() == password or password.upper() == password or password.isalnum()\
        or not any(i.isdigit() for i in password):
    print('your password is weak ')

else:
    print('your password is strong ')

我希望这有帮助