Reputation: 11
Recently, I have been building a password checker for a project I have been working on but for some reason certain inputs cause errors to occur and I cant figure out what it's from. If anyone can help that would be great as I have been searching for quite a while.
import re
exit = False
allowed_char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()-_=+"
row1 = "qwertyuiop"
row2 = "asdfghjkl"
row3 = "zxcvbnm"
class password_checker:
def __init__(self, password, score):
self.password = password
self.score = score
def validate(self):
length = len(self.password)
if (length < 8 or length > 24) and [x for x in self.password if x not in allowed_char]:
print("Invalid Length and Invalid Character")
raise ValueError
elif length < 8 or length > 24:
print("Invalid length")
raise ValueError
elif [x for x in self.password if x not in allowed_char]:
print("Invalid Character")
raise ValueError
def scoring(self):
length = len(self.password)
if re.search(r"[a-z]", self.password): self.score += 5
if re.search(r"[A-Z]", self.password): self.score += 5
if re.search(r"[0-9]", self.password): self.score += 5
if re.search(r"[!$%^&*()-_=+]",self.password): self.score += 5
if re.fullmatch(r"[A-Za-z]+", self.password): self.score -= 5
if re.fullmatch(r"[0-9]+", self.password): self.score -= 5
if re.fullmatch(r"[!$%^&*()\-_=+]+", self.password): self.score -= 5
self.password.lower()
for i in range (length - 2):
if re.search(self.password[i:i+3], row1) or re.search(self.password[i:i+3], row2) or re.search(self.password[i:i+3], row3):
self.score -= 5
print(self.score)
while exit == False:
try:
choice = int(input("""1. Check Password
2. Generate Password
3. Quit: """))
print("")
if choice == 1:
password = input("Enter Password: ")
score = len(password)
checking = password_checker(password, score)
checking.validate()
checking.scoring()
elif choice == 2:
pass
elif choice == 3:
exit = True
else:
raise ValueError
except ValueError:
print("")
I have tried multiple inputs which work with my code, for example "sdAqwe12!a^"
Later I tried the test case of "aSD7V^&*gS77+"
I expected this to work fine but for some reason an error happened.
I think this is because of the class function scoring.
This is what I run and the error received, Thank you
1. Check Password
2. Generate Password
3. Quit: 1
Enter Password: aSD7V^&*gS77+
Traceback (most recent call last):
File "main.py", line 53, in <module>
checking.scoring()
File "main.py", line 37, in scoring
if re.search(self.password[i:i+3], row1) or re.search(self.password[i:i+3], row2) or re.search(password[i:i+3], row3):
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/re.py", line 201, in search
return _compile(pattern, flags).search(string)
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/re.py", line 304, in _compile
p = sre_compile.compile(pattern, flags)
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_parse.py", line 948, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_parse.py", line 443, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/sre_parse.py", line 668, in _parse
raise source.error("nothing to repeat",
re.error: nothing to repeat at position 0
Upvotes: 0
Views: 47
Reputation: 77407
You are looking to see if 3 character extents in the password match 3 character rows from the keyboard using re.search
:
re.search(self.password[i:i+3], row1)
But that's the problem. If your password contains a regex control character, re
will try to use it. In your example "aSD7V^&*gS77+"
, you'll try the sequence "*gS"
. But that's not a valid regex. "*" tells regex to repeat the previous character zero or more times. But there is no character before it, hence re.error: nothing to repeat at position 0
Use in
instead.
self.password[i:i+3] in row1
Upvotes: 1