简体   繁体   中英

How to write a python function to check if a user input for a password is not strong?

I need to write a code for an assignment that will take user input of a password (as a string) and lets the user know what elements of the input make the password weak.

The requirements are that the password needs to be at least 8 characters in length, include both upper and lower case letters, and include numbers. My code doesn't need to determine if the password is strong, just why it is weak if it is weak.

so far the code I have written is as follows:

    size = len(password)
    
    if size < 8:
        print('not long enough')
    
    if password.isalnum():
        pass
    else:

    for x in password:
        if x.isupper():
            pass
        else:
            print('no upper case')
    
    for y in password:
        if y.islower():
            pass
        else:
            print('no lower case')
            
    return

I made some changes and used the.isalnum operator after my test run returned multiple lines of 'no upper case' and 'no lower case'.

I would greatly appreciate if anyone could nudge me in the right direction as this has confused me for a bit now

Without regex you can use any and str.isnumeric

if not any(map(str.isnumeric, password):
    print('No numbers')

I would rather go with regex.

In [122]: def validate(password):
     ...:     return True if re.match("(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}", password) else False
     ...:

In [123]: validate("helloas")
Out[123]: False

In [124]: validate("helH12asfgvGh")
Out[124]: True

(The other answers have already given you good answers, so this isn't an full answer, it's just an explanation of what is going wrong.) The area of your code that is causing the problem is

for x in password:
        if x.isupper():
            pass
        else:
            print('no upper case')
for y in password:
        if y.islower():
            pass
        else:
            print('no lower case')

You are looping over the entire password, checking if each character is uppercase, and then printing out "no upper case" if it isn't. The problem is that if a single character of the word isn't uppercase, "that_character_that_isn't_uppercase".isupper() will return false, and print the error statement. For example, the password PaSSWORD will return one "no upper case", since "a".isupper() is False. The password passworD will return 7 "no upper case"s, since the characters p,a,s,s,w,o,r are all lowercase. The same thing is happening with the x.islower() test, you are seeing if each individual character is lowercase. I would implement something like this:

#password.islower() will return true if all the entire string is lowercase(and thus not uppercase)
if password.islower():
    print("No upper case")
elif password.isupper():
    print("No lower case")
#Again, password.isupper() sees if all letters are uppercase(which means that there is no lowercase letters).

Hope this helped!

Lots of python password checkers online, for example: https://www.geeksforgeeks.org/password-validation-in-python/

Here's a quick console program you can call like:

$ python3 password_checker.py "Testf7788790##$"
Testing password:  Testf7788790##$
Password is valid:  True
$ python3 password_checker.py "insecurePassword"
Testing password:  insecurePassword
Password should contain at least one number
Password is valid:  False

Contents of password_checker.py :

#!/usr/bin/python

import sys

def password_check(passwd):
    symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=']
    isValid = False
      
    if len(passwd) < 10:
        print('Password should be at least 10 characters')
    elif not any(char.isdigit() for char in passwd):
        print('Password should contain at least one number')
    elif not any(char.isupper() for char in passwd):
        print('Password should contain at least one uppercase character')
    elif not any(char.islower() for char in passwd):
        print('Password should contain at least one lowercase character')
    elif not any(char in symbols for char in passwd):
        print('Password should contain at least one special character from list: ', symbols)
    else:
      isValid = True

    return isValid

arguments = sys.argv
if len(arguments) < 2:
    print('No password could be parsed by argv')
    valid_password = False
else:
    password = arguments[1]
    print('Testing password: ', password)
    valid_password = password_check(password)

print('Password is valid: ', valid_password)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM