Python password strength checker
The Python password strength checker is a simple program that evaluates the strength of a password based on various criteria. It checks the password for various attributes such as the presence of lowercase characters, uppercase characters, digits, special characters, and white spaces. Based on the presence of these attributes, the program assigns a strength score to the password, and the user is given feedback on the quality of their password.
The password strength checker program is written in Python and is easy to use. It asks the user for input and evaluates the input password according to the criteria mentioned. The program is equipped with error handling, so the user is prompted to re-enter the password if they provide an invalid input. The user is also given feedback on the quality of the password, providing a reason to improve the password if necessary. The Python password strength checker can be used as a starting point for creating more advanced password security applications, where the password quality can be evaluated in real-time as the user inputs the password.
The video
The interface
def check_pwd(another_pw=False):
another = 'Do you want to check another password\'s strength (y/n) : '
check_pass = 'Do you want to check your password\'s strength (y/n) : '
prompt = another if another_pw else check_pass
choice = input(prompt).lower()
if choice == 'y':
return True
elif choice == 'n':
print('Exiting...')
return False
else:
print('Invalid input. Please try again.')
return check_pwd(another_pw)
This function, check_pwd
, prompts the user to check the strength of another password or exit the program. It takes an optional boolean argument, another_pw
, which determines the prompt message displayed to the user. If another_pw
is False
, the prompt message asks the user if they want to check the strength of their password, and if True
, the prompt message asks if they want to check the strength of another password.
The function takes user input, converts it to lowercase, and checks if it is either ‘y’ or ‘n’. If it is ‘y’, the function returns True
, indicating that the password strength check should continue. If it is ‘n’, the function returns False
, indicating that the password strength check should end and the program should exit. If the user input is invalid, the prompt message is displayed again.
The strength check
def check_password_strength():
password = getpass.getpass('Enter the password: ')
strength = 0
remarks = ''
lower_count = upper_count = num_count = wspace_count = special_count = 0
for char in password:
if char in string.ascii_lowercase:
lower_count += 1
elif char in string.ascii_uppercase:
upper_count += 1
elif char in string.digits:
num_count += 1
elif char == ' ':
wspace_count += 1
else:
special_count += 1
strength += sum([lower_count >= 1,
upper_count >= 1,
num_count >= 1,
wspace_count >= 1,
special_count >= 1])
if strength == 1:
remarks = ('That\'s a very bad password.'
' Change it as soon as possible.')
elif strength == 2:
remarks = ('That\'s a weak password.'
' You should consider using a tougher password.')
elif strength == 3:
remarks = 'Your password is okay, but it can be improved.'
elif strength == 4:
remarks = ('Your password is hard to guess.'
' But you could make it even more secure.')
elif strength == 5:
remarks = ('Now that\'s a strong password!!!'
' Hackers don\'t have a chance guessing that password!')
print('Your password has:-')
print(f'{lower_count} lowercase letters')
print(f'{upper_count} uppercase letters')
print(f'{num_count} digits')
print(f'{wspace_count} whitespaces')
print(f'{special_count} special characters')
print(f'Password Score: {(strength / 5)*100}/100')
print(f'Remarks: {remarks}')
This code checks the strength of a password entered by the user using getpass.getpass()
to mask the password input. The password strength is determined by counting the number of different types of characters in the password, such as lowercase letters, uppercase letters, digits, whitespaces, and special characters. A score is calculated based on the number of different types of characters, and remarks are given based on the score. The results of the check, including the character count and score, are printed out to the user.
strength += sum([lower_count >= 1,
upper_count >= 1,
num_count >= 1,
wspace_count >= 1,
special_count >= 1])
The code adds up the count of certain types of characters in the password string and increments the strength
variable by the number of character types that are present.
The sum
function takes a list of booleans as an argument, with each boolean representing the presence (True
) or absence (False
) of a certain character type in the password string. The sum
function returns the count of True
values in the list, i.e., the number of character types present in the password string. This value is then added to the strength
variable.
The code
import string
import getpass
def check_password_strength():
password = getpass.getpass('Enter the password: ')
strength = 0
remarks = ''
lower_count = upper_count = num_count = wspace_count = special_count = 0
for char in password:
if char in string.ascii_lowercase:
lower_count += 1
elif char in string.ascii_uppercase:
upper_count += 1
elif char in string.digits:
num_count += 1
elif char == ' ':
wspace_count += 1
else:
special_count += 1
strength += sum([lower_count >= 1,
upper_count >= 1,
num_count >= 1,
wspace_count >= 1,
special_count >= 1])
if strength == 1:
remarks = ('That\'s a very bad password.'
' Change it as soon as possible.')
elif strength == 2:
remarks = ('That\'s a weak password.'
' You should consider using a tougher password.')
elif strength == 3:
remarks = 'Your password is okay, but it can be improved.'
elif strength == 4:
remarks = ('Your password is hard to guess.'
' But you could make it even more secure.')
elif strength == 5:
remarks = ('Now that\'s a strong password!!!'
' Hackers don\'t have a chance guessing that password!')
print('Your password has:-')
print(f'{lower_count} lowercase letters')
print(f'{upper_count} uppercase letters')
print(f'{num_count} digits')
print(f'{wspace_count} whitespaces')
print(f'{special_count} special characters')
print(f'Password Score: {(strength / 5)*100}/100')
print(f'Remarks: {remarks}')
def check_pwd(another_pw=False):
another = 'Do you want to check another password\'s strength (y/n) : '
check_pass = 'Do you want to check your password\'s strength (y/n) : '
prompt = another if another_pw else check_pass
choice = input(prompt).lower()
if choice == 'y':
return True
elif choice == 'n':
print('Exiting...')
return False
else:
print('Invalid input. Please try again.')
return check_pwd(another_pw)
if __name__ == '__main__':
print('===== Welcome to Password Strength Checker =====')
check_pw = check_pwd()
while check_pw:
check_password_strength()
check_pw = check_pwd(True)
This is a password strength checker written in Python using the getpass
and string
libraries. When run, it prompts the user to enter their password, which is then evaluated for strength. The password strength is determined by counting the number of lowercase letters, uppercase letters, digits, whitespaces, and special characters it contains. A score is calculated based on the number of these character types present, with a score of 5 meaning a very strong password. The results are displayed along with some remarks about the password’s strength. The program then prompts the user to check the strength of another password, and repeats the process until the user decides to exit.