【问题标题】:How to program a python password validator [closed]如何编写python密码验证器[关闭]
【发布时间】:2020-02-06 02:46:48
【问题描述】:

我无法让程序检查具有“强密码”的所有因素,而不必进行十亿个 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)

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

【问题讨论】:

标签: regex python-3.x loops if-statement passwords


【解决方案1】:

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

  • (?=.{10,}) 确保至少 10 个字符。
  • (?=.*[A-Z].*) 至少检查 1 个大写字母。
  • (?=.*[a-z].*) 至少检查 1 个小写字母。
  • (?=.*\d.*) 至少检查 1 位数字。
  • (?=.*[\!\@\#\$\%\^\&amp;\*].*) 从您的列表中查找至少 1 个符号。
  • (?=^[\!\@\#\$\%\^\&amp;\*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!

【讨论】:

  • 对不起,我不懂那个语法。我不太清楚将该代码放在哪里或如何放置。我还是个新手。
  • @KevinJ。不用担心!用一些 python 示例代码查看更新的答案,以及测试它的演示。
  • 哦!这是一个非常好的方法!我很确定我可以使用现有代码找到一种方法,根据它们失败的条件来打印语句。谢谢,伙计!
  • @KevinJ。乐意效劳!如果它解决了您的问题,请考虑接受答案,以便未来的访问者知道什么有效。
【解决方案2】:

您的代码中有一些错误。

你可以通过在你自己的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)

【讨论】:

  • 谢谢!在试图让一切都靠我自己工作时,我遇到了一个问题,即使密码正确,一切都会打印出来。但这为我解决了!如果我愿意,我什至可以把它放到一个函数中!
猜你喜欢
  • 2019-04-28
  • 2016-07-18
  • 2014-03-08
  • 1970-01-01
  • 2010-12-18
  • 2018-02-03
  • 1970-01-01
  • 2019-02-21
  • 2020-05-09
相关资源
最近更新 更多