Jordan Iatro
Jordan Iatro

Reputation: 297

Password Strength Checking

Sorry, I am trying to write a code on python 3.2 for password strength check but it looks like I keep getting error somewhere. Whenever my input is #$%$J, is will go into the if input.upper() is true. How do I prevent this from happening? And my score value is not accurate. coding:

password_user = input("Input password : ")
score = 0
if len(password_user) <=5:
    print("Password is too short! ")
    score = 1
elif password_user == password_user.lower():
    print("Bad Password")
    score = 1
elif password_user == password_user.upper():
    print("Bad Password")
    score = 1
else:
    while score == 0:
        for x in range(33,48):
            ascii_str = chr(x)
            if password_user.find(ascii_str) >= 0:
                score = score + 3
        for y in range(97,123):
            ascii_lower = chr(y)
            if password_user.find(ascii_lower) >= 0:
                score = score + 1
        for w in range(65,91):
            ascii_upper = chr(w)
            if password_user.find(ascii_upper) >= 0:
                score = score + 2
        for z in range(48,58):
            ascii_num = chr(z)
            if password_user.find(ascii_num) >= 0:
                score = score + 2
if score >0 | score <=5:
    print("Weak Password")
    print(score)
elif score > 5 | score < 7:
    print("Medium Password")
    print(score)
elif score >= 7:
    print("Strong Password")
    print(score)

Upvotes: 0

Views: 3259

Answers (1)

antimonyarsenide
antimonyarsenide

Reputation: 21

First of all, it should be, e.g., find(ascii_str) > -1 (so that character number 0 is counted). I'm assuming you mean to give a score bonus at most once for each range of characters. Therefore, something like this is appropriate:

contain = false
for x in range(33,48):
    ascii_str = chr(x)
    if password_user.find(ascii_str) > -1:
        contain = true
if contain:
    score += 3

and similarly for the others.

EDIT: The question does not make clear whether you want each distinct character to increase the score, for each character to increase the score, or only for each type of character to increase the score. Given your metric of any score greater than 6 being strong, anything that doesn't return "Password is too short" or "Bad Password" will be strong (it'll be at least 6 characters, with at least one capital letter, already giving 7 points) unless you have many repeated letters. If you really want each new character to increase the score, you should change the metric (maybe 12 or more is a strong password?), although in my opinion it makes more sense to just increase the score for each type of character contained, and then increase the score by the length, or something like that.

Upvotes: 1

Related Questions