如何编写 python 密码验证程序

How to program a python password validator

我无法让程序检查具有 "strong password" 的所有因素,而不必制作十亿个 if 语句。

我正在尝试制作一个密码强度程序,其中密码必须包含:

  1. 至少 10 个字符
  2. 大小写混合
  3. 至少一个号码
  4. 来自“!@#$%^&*”的有效特殊字符

代码:

import re


def passwordChecker(password):
    tooShort = "Your password is too short, it must be at least 10 characters"
    noNum = "Your password does not have a number. Please add at least one number."
    notMixed = "Your password is not mixed case. Please choose a password with mixed case."
    noSpec = "Your password does not have a valid special character. Please add at least one valid special character."
    if len(password) >= 10 and re.search(r'[0-9]', password) and re.search(r'[A-Z]', password) \
            and re.search(r'[a-z]', password) and re.search(r'[$!@%^&*#]', password):
        print("Your password is valid")
    elif len(password) < 10:
        print(tooShort, "\n" +str(noNum), "\n" + str(notMixed), "\n" + str(noSpec))
    elif len(password) >= 10 and re.search(r'[A-Z]', password) and re.search(r'[a-z]', password):
        print(noNum, "\n" + str(noSpec))
    elif len(password) >= 10 and re.search(r'[$!@%^&*#]', password):
        print(notMixed, "\n" + str(noNum))


password = str(input("Enter a password: "))
passwordChecker(password)

虽然它有效,但我想我需要找出一个更好的系统,它更……稳健,我猜?使用正则表达式不是必须的,它只是我最终使用的方式。

(?=.{10,})(?=.*[A-Z].*)(?=.*[a-z].*)(?=.*\d.*)(?=.*[\!\@\#$\%\^\&\*].*)(?=^[\!\@\#$\%\^\&\*a-zA-Z0-9]+$)^.*$ 应该可以满足你的每一个需求,而且也很容易修改。

  • (?=.{10,})确保至少10个字符。
  • (?=.*[A-Z].*) 检查至少 1 个大写字母。
  • (?=.*[a-z].*) 检查至少 1 个小写字母。
  • (?=.*\d.*) 检查至少 1 个数字。
  • (?=.*[\!\@\#$\%\^\&\*].*) 从您的列表中寻找至少 1 个符号。
  • (?=^[\!\@\#$\%\^\&\*a-zA-Z0-9]+$) 确保没有出现不在您的列表中的符号。
  • ^.*$ 要匹配的实际密码。

如果您不需要这些规则中的一个或多个,只需删除包含它们的正先行。

Try it here!

编辑:这是一个实施密码检查器的示例 python 程序。

import re

#loop forever
while(1==1):
  #get a password
  pword = input("password to test: ")

  #test it against the conditions
  if(re.match(
    "(?=.{10,})" + 
    "(?=.*[A-Z].*)" +
    "(?=.*[a-z].*)" +
    "(?=.*\d.*)" +
    "(?=.*[\!\@\#$\%\^\&\*].*)(?=^[\!\@\#$\%\^\&\*a-zA-Z0-9]+$)" +
    "^.*$", pword)
  ):
    #match: good password
    print("good")
  else:
    #failed one of the conditions: bad password
    print("bad")

Demo here!

您的代码中存在一些错误。

您可以通过在自己的 password_checker 方法旁边定义五个简单的函数来解决问题。我没有完全检查以下代码中的所有内容,您可能想检查一下是否还有一些错误:

测试

import re


def password_checker(password):
    too_short = "Your password is too short, it must be at least 10 characters"
    no_num = "Your password does not have a number. Please add at least one number."
    not_mixed = "Your password is not mixed case. Please choose a password with mixed case."
    no_spec = "Your password does not have a valid special character. Please add at least one valid special character."
    unknown_problem = "Something is not right!"
    valid_pass = "Congrats! Your pass is valid!"

    if check_length(password) is True:
        print(too_short)

    if check_digit(password) is None:
        print(no_num)

    if check_lowercase(password) is None or check_uppercase(password) is None:
        print(not_mixed)

    if check_special_chars(password) is None:
        print(no_spec)

    if check_length(password) is False and check_special_chars(password) and check_digit(password) and check_lowercase(password) and check_uppercase(password):
        print(valid_pass)


def check_length(password):
    '''
    Returns True if length is smaller than 10
    '''
    return len(password) < 10


def check_digit(password):
    '''
    Returns an object if there is a digit
    '''
    return re.search(r'(?=.*[0-9])', password)


def check_lowercase(password):
    '''
    Returns an object if there is a lowercase letter
    '''
    return re.search(r'(?=.*[a-z])', password)


def check_uppercase(password):
    '''
    Returns an object if there is a uppercase letter
    '''
    return re.search(r'(?=.*[A-Z])', password)


def check_special_chars(password):
    '''
    Returns an object if there is a special char from this char class: [$!@%^&*#]
    '''
    return re.search(r'(?=.*[$!@%^&*#])', password)


password = str(input("Enter a password: "))
password_checker(password)