简体   繁体   English

python 3.3密码检查器

[英]python 3.3 Password checker

I am creating a program that checks passwords to see if they are strong enough: 我正在创建一个程序来检查密码,以查看它们是否足够牢固:

  • they need caps, 他们需要上限,
  • lower case, 小写,
  • numbers, 号,
  • symbols and 符号和
  • cannot contain easy passwords. 不能包含简单的密码。

As you can see below this is only some of my code and I have a menu, and a password checker, the only problem is that the checker doesn't seem to be working as when I enter a password, its always saying its a strong password even though it does not contain any caps or numbers or symbols. 正如您在下面看到的那样,这只是我的一些代码,并且我有一个菜单和一个密码检查器,唯一的问题是该检查器似乎不像我输入密码时那样工作,它总是说它很安全。密码,即使其中不包含任何大写字母,数字或符号。 Below is my code so far. 下面是到目前为止的代码。

import random #importing random
import time #importing time
global Menu

def Menu():
    global optionchoice #globalising variable optionchoice so that it can be used inside of another def of the code
    print("Hey, Welcome to PassWordChecker.")
    time.sleep(2) #delaying the next line for 2 seconds
    print("Please choose one of the options below." )
    time.sleep(1)
    print("MENU:")
    print("***")
    print("    press 1 once for              :           password checker  ")
    print("    press 2 twice for             :           password generator")
    print("    press 3 two or three times to :           Quit              ")
    print("                                                                 ***") #creating a basic menu
    optionchoice = (input("which option would you like to choose?"))

def checkpasswordandinput():
    global optionchoice
    optionchoice = optionchoice
    if optionchoice == '1':
        print(" please enter a password. ")
        UserPassword = input("")
        if len(UserPassword) <= 8 or len(UserPassword) >= 24 or UserPassword == UserPassword.isupper() or UserPassword == UserPassword.isdigit() or UserPassword == UserPassword.islower() or UserPassword == UserPassword.isalpha():
            print("make sure your password includes numbers and upper and lower case letters  ")
            UserPassword = input('please enter a new password')
        else:
            print('your password is a very good one that is difficult to crack')
            return Menu()

Notes to future readers: 给未来读者的注意事项:

Please do not write code in the form above: 请不要以上述形式编写代码:

  1. Do not import modules you don't need 不要导入不需要的模块
  2. Do not use global variables 不要使用全局变量
  3. Do use loops: eg while True: 请使用循环:例如, while True:
  4. Do call functions from other functions 从其他功能调用功能
  5. Do return values from one function back to the caller: optionchoice = menu() 不要将一个函数的值返回给调用者: optionchoice = menu()

The above code would have been better in this form: 上面的代码在这种形式下会更好:

import time

def menu():
    print("    press 1 for password checker  ")
    print("    press 2 for password generator")
    print("    press 3 to Quit")
    return input("which option would you like to choose?")

def checkpasswordandinput():
    while True:
        optionchoice = menu()
        if optionchoice == '1':
            #Enter password and validate
            pass
        # detect other options
        if optionchoice == '3':
            break


checkpasswordandinput()

Those checks in the form UserPassword == UserPassword.isupper() will not work. 那些形式为UserPassword == UserPassword.isupper()将不起作用。 Here, you are comparing a string, the password, to a boolean, the result of isX() . 在这里,您将一个字符串(密码)与一个布尔值( isX()的结果isX() Hence, all those checks are False and you get to the else branch (if the length is acceptable). 因此,所有这些检查都是False并且您到达else分支(如果长度可以接受)。

In the case of upper and lowercase chars, you could use UserPassword == UserPassword.upper() instead (just upper , not isupper , and analogously with lower() ), ie compare the password to it's upper/lowercased version, but for digits this does not work. 在使用大写和小写字符的情况下,可以改用UserPassword == UserPassword.upper() (只是upper ,不是isupper ,并与lower()类似),即将密码与其大写/小写版本进行比较,但是要输入数字这行不通。 Instead, you can use any to check if any char is a digit: any(c.isdigit() for c in UserPassword) 相反,您可以使用any来检查任何char是否为数字: any(c.isdigit() for c in UserPassword)

Edit: You can use UserPassword == UserPassword.upper() to check if the password does not contains any lowercase letters, which is kind of unintuitive. 编辑:您可以使用UserPassword == UserPassword.upper()来检查密码是否包含任何小写字母,这有点不直观。 Instead, I'd suggest using any for all the checks and also inversing the condition so the "positive" case is in the if s body and the "negative" in the else. 相反,我建议对所有检查都使用any ,并且还要反转条件,以便if主体中包含“正”情况,else中包含“负”情况。 Something like this: 像这样:

up = UserPassword
if 8 <= len(up) <= 24 and any(c.isupper() for c in up) and any(c.islower() for c in up) and any(c.isdigit() for c in up) and any(c.isalpha() for c in up):

Or a bit shorter, using a list of functions: 或更短一些,使用功能列表:

if 8 <= len(up) <= 24 and all(any(f(c) for c in up) for f in (str.islower, str.isupper, str.isdigit, str.isalpha)):

So you need to do something like the following: 因此,您需要执行以下操作:

hasUpper = False
hasLower = False
hasDigit = False

etc etc

Then, go through the 'password' one character at a time: ie. 然后,一次通过一个字符输入“密码”:即。 string[1], string[2], string[3] 字符串[1],字符串[2],字符串[3]

Run your boolean result on it (isupper, islower, etc) when true, change your hasUpper, hasLower, hasDigit to True 当为true时,对它运行布尔结果(isupper,islower等),将hasUpper,hasLower,hasDigit更改为True

Then, once gone through the whole string, check your boolean results with your requirements. 然后,遍历整个字符串后,根据需要检查布尔结果。 ie: 即:

if requirements are upper, lower, and digit. 如果要求是上,下和数字。

if hasUpper = True and hasLower = True and hasDigit = True:

then it's a good password, etc. 那是一个很好的密码,等等。

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM