python 密码检查程序中某些输入出现问题

wz8daaqr  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(96)

最近,我一直在为一个项目构建一个密码检查器,我一直在工作,但由于某种原因,某些输入导致错误发生,我不能弄清楚它是从哪里来的。如果有人能帮助这将是伟大的,因为我已经搜索了相当长的一段时间。

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("")

我已经尝试了多个输入,这些输入可以与我的代码一起使用,例如“sdAqwe 12!a^”
后来我尝试了“aSD 7V ^&* gS 77+”的测试用例
我期望这能正常工作,但由于某种原因发生了错误。
我认为这是因为类函数的评分。
这是我运行和收到的错误,谢谢

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

w6lpcovy

w6lpcovy1#

您将使用re.search查看密码中的3个字符范围是否与键盘中的3个字符行匹配:

re.search(self.password[i:i+3], row1)

但这就是问题所在。如果您的密码包含一个regex控制字符,re将尝试使用它。在您的示例"aSD7V^&*gS77+"中,您将尝试序列"*gS"。但这不是一个有效的regex。“*”告诉regex重复前面的字符零次或多次。但前面没有字符,因此re.error: nothing to repeat at position 0
请改用in

self.password[i:i+3] in row1

相关问题