In the following example, a password is checked for its strength. If the password contains less than 6 characters, it is invalid. If the password contains less than 10 characters it gets the password strength medium, if it contains more than 10 characters it is strong.
Furthermore, the password must contain at least one lowercase letter, one uppercase letter, one number and one special character. The output of this program is the strength of the password, or an indication of a missing character.
def checkPassword(password):
upperChars, lowerChars, specialChars, digits, length = 0, 0, 0, 0, 0
length = len(password)
if (length < 6):
print("Password must be at least 6 characters long!\n")
else:
for i in range(0, length):
if (password[i].isupper()):
upperChars += 1
elif (password[i].islower()):
lowerChars += 1
elif (password[i].isdigit()):
digits += 1
else:
specialChars += 1
if (upperChars != 0 and lowerChars != 0 and digits != 0 and specialChars != 0):
if (length >= 10):
print("The strength of password is strong.\n")
else:
print("The strength of password is medium.\n")
else:
if (upperChars == 0):
print("Password must contain at least one uppercase character!\n")
if (lowerChars == 0):
print("Password must contain at least one lowercase character!\n")
if (specialChars == 0):
print("Password must contain at least one special character!\n")
if (digits == 0):
print("Password must contain at least one digit!\n")
password = input("Please enter password: ")
checkPassword(password)
[first_run]
Please enter password: 34rT%
Password must be at least 6 characters long!
[second_run]
Please enter password: 23456weretsDFDS
Password must contain at least one special character!
[third_run]
Please enter password: 456ztr7TZ$$
The strength of password is strong.
def checkPassword(password):
This line defines a function named checkPassword
that takes a single parameter password
.
upperChars, lowerChars, specialChars, digits, length = 0, 0, 0, 0, 0
length = len(password)
upperChars
: Counter for uppercase characters in the password.lowerChars
: Counter for lowercase characters in the password.specialChars
: Counter for special characters in the password.digits
: Counter for numeric digits in the password.length
: Stores the length of the password using the len()
function.if (length < 6):
print("Password must be at least 6 characters long!\n")
This checks if the password is shorter than 6 characters. If it is, it prints a message indicating that the password must be at least 6 characters long and exits the function.
else:
for i in range(0, length):
if (password[i].isupper()):
upperChars += 1
elif (password[i].islower()):
lowerChars += 1
elif (password[i].isdigit()):
digits += 1
else:
specialChars += 1
password[i].isupper()
: Checks if the character is an uppercase letter.password[i].islower()
: Checks if the character is a lowercase letter.password[i].isdigit()
: Checks if the character is a digit.if (upperChars != 0 and lowerChars != 0 and digits != 0 and specialChars != 0):
if (length >= 10):
print("The strength of password is strong.\n")
else:
print("The strength of password is medium.\n")
else:
if (upperChars == 0):
print("Password must contain at least one uppercase character!\n")
if (lowerChars == 0):
print("Password must contain at least one lowercase character!\n")
if (specialChars == 0):
print("Password must contain at least one special character!\n")
if (digits == 0):
print("Password must contain at least one digit!\n")
password = input("Please enter password: ")
checkPassword(password)
input("Please enter password: ")
: Prompts the user to enter a password.checkPassword(password)
: Calls the checkPassword
function with the user’s input.This code defines a function to evaluate the strength of a password based on its length and the presence of various character types. It gives detailed feedback about any missing requirements and categorizes the password’s strength as either “strong” or “medium” based on its length and complexity.