简体   繁体   English

如何编写 python function 来检查用户输入的密码是否不强?

[英]How to write a python function to check if a user input for a password is not strong?

I need to write a code for an assignment that will take user input of a password (as a string) and lets the user know what elements of the input make the password weak.我需要为赋值编写一个代码,该代码将接受用户输入密码(作为字符串)并让用户知道输入的哪些元素使密码变弱。

The requirements are that the password needs to be at least 8 characters in length, include both upper and lower case letters, and include numbers.要求密码长度至少为 8 个字符,包含大小写字母,并包含数字。 My code doesn't need to determine if the password is strong, just why it is weak if it is weak.我的代码不需要确定密码是否强,只要弱就为什么弱。

so far the code I have written is as follows:到目前为止,我编写的代码如下:

    size = len(password)
    
    if size < 8:
        print('not long enough')
    
    if password.isalnum():
        pass
    else:

    for x in password:
        if x.isupper():
            pass
        else:
            print('no upper case')
    
    for y in password:
        if y.islower():
            pass
        else:
            print('no lower case')
            
    return

I made some changes and used the.isalnum operator after my test run returned multiple lines of 'no upper case' and 'no lower case'.在我的测试运行返回多行“无大写”和“无小写”后,我做了一些更改并使用了 .isalnum 运算符。

I would greatly appreciate if anyone could nudge me in the right direction as this has confused me for a bit now如果有人能把我推向正确的方向,我将不胜感激,因为这让我有点困惑

Without regex you can use any and str.isnumeric没有正则表达式,您可以使用anystr.isnumeric

if not any(map(str.isnumeric, password):
    print('No numbers')

I would rather go with regex.我宁愿 go 与正则表达式。

In [122]: def validate(password):
     ...:     return True if re.match("(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}", password) else False
     ...:

In [123]: validate("helloas")
Out[123]: False

In [124]: validate("helH12asfgvGh")
Out[124]: True

(The other answers have already given you good answers, so this isn't an full answer, it's just an explanation of what is going wrong.) The area of your code that is causing the problem is (其他答案已经给了你很好的答案,所以这不是一个完整的答案,它只是对出了什么问题的解释。)导致问题的代码区域是

for x in password:
        if x.isupper():
            pass
        else:
            print('no upper case')
for y in password:
        if y.islower():
            pass
        else:
            print('no lower case')

You are looping over the entire password, checking if each character is uppercase, and then printing out "no upper case" if it isn't.您正在遍历整个密码,检查每个字符是否为大写,如果不是,则打印出“无大写”。 The problem is that if a single character of the word isn't uppercase, "that_character_that_isn't_uppercase".isupper() will return false, and print the error statement.问题是如果单词的单个字符不是大写,“that_character_that_isn't_uppercase”.isupper() 将返回 false,并打印错误语句。 For example, the password PaSSWORD will return one "no upper case", since "a".isupper() is False.例如,密码 PaSSWORD 将返回一个“非大写”,因为“a”.isupper() 为 False。 The password passworD will return 7 "no upper case"s, since the characters p,a,s,s,w,o,r are all lowercase.密码密码将返回 7 个“非大写”,因为字符 p、a、s、s、w、o、r 都是小写的。 The same thing is happening with the x.islower() test, you are seeing if each individual character is lowercase. x.islower() 测试也发生了同样的事情,您会看到每个单独的字符是否都是小写的。 I would implement something like this:我会实现这样的事情:

#password.islower() will return true if all the entire string is lowercase(and thus not uppercase)
if password.islower():
    print("No upper case")
elif password.isupper():
    print("No lower case")
#Again, password.isupper() sees if all letters are uppercase(which means that there is no lowercase letters).

Hope this helped!希望这有帮助!

Lots of python password checkers online, for example: https://www.geeksforgeeks.org/password-validation-in-python/网上有很多 python 密码检查器,例如: https://www.geeksforgeeks.org/password-validation-in-python/

Here's a quick console program you can call like:这是一个快速控制台程序,您可以这样调用:

$ python3 password_checker.py "Testf7788790##$"
Testing password:  Testf7788790##$
Password is valid:  True
$ python3 password_checker.py "insecurePassword"
Testing password:  insecurePassword
Password should contain at least one number
Password is valid:  False

Contents of password_checker.py : password_checker.py的内容:

#!/usr/bin/python

import sys

def password_check(passwd):
    symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=']
    isValid = False
      
    if len(passwd) < 10:
        print('Password should be at least 10 characters')
    elif not any(char.isdigit() for char in passwd):
        print('Password should contain at least one number')
    elif not any(char.isupper() for char in passwd):
        print('Password should contain at least one uppercase character')
    elif not any(char.islower() for char in passwd):
        print('Password should contain at least one lowercase character')
    elif not any(char in symbols for char in passwd):
        print('Password should contain at least one special character from list: ', symbols)
    else:
      isValid = True

    return isValid

arguments = sys.argv
if len(arguments) < 2:
    print('No password could be parsed by argv')
    valid_password = False
else:
    password = arguments[1]
    print('Testing password: ', password)
    valid_password = password_check(password)

print('Password is valid: ', valid_password)

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

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