patternpythonMajor
Password Strength Detector
Viewed 0 times
detectorstrengthpassword
Problem
I'm just a beginner in Python. I wrote a simple code that detects the user's password strength, but I feel that it isn't very efficient. How can I improve it?
print('''# This is Password Strength Detector.\n
-----------------------------------------------------------------''')
password = input('Enter your password here : ')
print('\n')
from re import *
lower_match = compile(r'[a-z]').findall(password) # Finding if the password contains lowecase letters.
upper_match = compile(r'[A-Z]').findall(password) # Finding if the password contains uppercase letters.
symbol_match = compile(r'[|\"|\'|~|!|@|#|$|%|^|&|*|(|)|_|=|+|\||,|.|/|?|:|;|[|]|{\}|]').findall(
password) # Finding if the password contains special characters.
number_match = compile(r'[0-9]').findall(password) # Finding if the password contains numbers.
if len(password) = 8:
print('Your password is strong ! ')
elif len(password) >= 16:
print('Your password is very strong ! ')Solution
The strength of a password has nothing to do with the presence of special characters! Obligatory xkcd reference:
Testing
You will never indicate that a password is very strong because of this ordering:
If
Regex
The point of
Then check against
Importing
is highly frowned upon. Just do
correct horse battery staple is a very strong password, but you would classify it as weak, simply because it doesn't contain capital letters, symbols, or numbers. Rethink the idea.Testing
You will never indicate that a password is very strong because of this ordering:
elif len(password) >= 8:
print('Your password is strong ! ')
elif len(password) >= 16:
print('Your password is very strong ! ')If
len(password) is, say, 20, both branches are true. So you want to makes ure that the most restrictive one goes first:elif len(password) >= 16:
print('Your password is very strong ! ')
else:
print('Your password is strong ! ')Regex
The point of
compile is if you reuse the same regex many times, you can make it more efficient. You use each one exactly once, so it's a waste of processing time. What you want to instead use is re.search(), since you don't care about all the instances you just want to know if there is such a thing:lower_match = re.search(r'[a-z]', password)
upper_match = re.search(r'[A-Z]', password)
...Then check against
None instead of checking len(). Also your symbol check is inefficient. You have a bunch of |s, when you could simply list them all:symbol_match = re.search(r'[\"\'~!@#$%^&\\*\(\)_=\+\|,./\?:;\[\]\{\}<>]', password)Importing
from X import *is highly frowned upon. Just do
import re and use re.X everywhere.Code Snippets
elif len(password) >= 8:
print('Your password is strong ! ')
elif len(password) >= 16:
print('Your password is very strong ! ')elif len(password) >= 16:
print('Your password is very strong ! ')
else:
print('Your password is strong ! ')lower_match = re.search(r'[a-z]', password)
upper_match = re.search(r'[A-Z]', password)
...symbol_match = re.search(r'[\"\'~!@#$%^&\\*\(\)_=\+\|,./\?:;\[\]\{\}<>]', password)from X import *Context
StackExchange Code Review Q#110021, answer score: 32
Revisions (0)
No revisions yet.