|
| 1 | +''' |
| 2 | +Password Strength Checker |
| 3 | +------------------------------------------------------------- |
| 4 | +''' |
| 5 | + |
| 6 | + |
| 7 | +import string |
| 8 | +import getpass |
| 9 | + |
| 10 | + |
| 11 | +def check_password_strength(): |
| 12 | + password = getpass.getpass('Enter the password: ') |
| 13 | + strength = 0 |
| 14 | + remarks = '' |
| 15 | + lower_count = upper_count = num_count = wspace_count = special_count = 0 |
| 16 | + |
| 17 | + for char in list(password): |
| 18 | + if char in string.ascii_lowercase: |
| 19 | + lower_count += 1 |
| 20 | + elif char in string.ascii_uppercase: |
| 21 | + upper_count += 1 |
| 22 | + elif char in string.digits: |
| 23 | + num_count += 1 |
| 24 | + elif char == ' ': |
| 25 | + wspace_count += 1 |
| 26 | + else: |
| 27 | + special_count += 1 |
| 28 | + |
| 29 | + if lower_count >= 1: |
| 30 | + strength += 1 |
| 31 | + if upper_count >= 1: |
| 32 | + strength += 1 |
| 33 | + if num_count >= 1: |
| 34 | + strength += 1 |
| 35 | + if wspace_count >= 1: |
| 36 | + strength += 1 |
| 37 | + if special_count >= 1: |
| 38 | + strength += 1 |
| 39 | + |
| 40 | + if strength == 1: |
| 41 | + remarks = ('That\'s a very bad password.' |
| 42 | + ' Change it as soon as possible.') |
| 43 | + elif strength == 2: |
| 44 | + remarks = ('That\'s a weak password.' |
| 45 | + ' You should consider using a tougher password.') |
| 46 | + elif strength == 3: |
| 47 | + remarks = 'Your password is okay, but it can be improved.' |
| 48 | + elif strength == 4: |
| 49 | + remarks = ('Your password is hard to guess.' |
| 50 | + ' But you could make it even more secure.') |
| 51 | + elif strength == 5: |
| 52 | + remarks = ('Now that\'s one hell of a strong password!!!' |
| 53 | + ' Hackers don\'t have a chance guessing that password!') |
| 54 | + |
| 55 | + print('Your password has:-') |
| 56 | + print(f'{lower_count} lowercase letters') |
| 57 | + print(f'{upper_count} uppercase letters') |
| 58 | + print(f'{num_count} digits') |
| 59 | + print(f'{wspace_count} whitespaces') |
| 60 | + print(f'{special_count} special characters') |
| 61 | + print(f'Password Score: {strength / 5}') |
| 62 | + print(f'Remarks: {remarks}') |
| 63 | + |
| 64 | + |
| 65 | +def check_pwd(another_pw=False): |
| 66 | + valid = False |
| 67 | + if another_pw: |
| 68 | + choice = input( |
| 69 | + 'Do you want to check another password\'s strength (y/n) : ') |
| 70 | + else: |
| 71 | + choice = input( |
| 72 | + 'Do you want to check your password\'s strength (y/n) : ') |
| 73 | + |
| 74 | + while not valid: |
| 75 | + if choice.lower() == 'y': |
| 76 | + return True |
| 77 | + elif choice.lower() == 'n': |
| 78 | + print('Exiting...') |
| 79 | + return False |
| 80 | + else: |
| 81 | + print('Invalid input...please try again. \n') |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + print('===== Welcome to Password Strength Checker =====') |
| 86 | + check_pw = check_pwd() |
| 87 | + while check_pw: |
| 88 | + check_password_strength() |
| 89 | + check_pw = check_pwd(True) |
0 commit comments