比较来自世界各地的卖家的域名和 IT 服务价格

检查密码的可靠性 /如何检查条件/

我正在尝试创建一个要求您输入密码的系统。 如果它都是较低的,顶部或 num, 然后,密封较弱,如果是两个条件,那么这 med, 如果每个人都得到满足,那么它很强大。 它似乎没有工作。

然而,弱者和强烈的工作,媒体没有。

我不知道我错了。


def password//:

print /'enter password'/
print //
print //
print /'the password must be at least 6, and no more than 12 characters long'/
print //

password = input /'type your password ....'/


weak = 'weak'
med = 'medium'
strong = 'strong'

if len/password/ >12:
print /'password is too long It must be between 6 and 12 characters'/

elif len/password/ <6:
print /'password is too short It must be between 6 and 12 characters'/


elif len/password/ >=6 and len/password/ <= 12:
print /'password ok'/

if password.lower//== password or password.upper//==password or password.isalnum//==password:
print /'password is', weak/

elif password.lower//== password and password.upper//==password or password.isalnum//==password:
print /'password is', med/

else:
password.lower//== password and password.upper//==password and password.isalnum//==password
print /'password is', strong/
已邀请:

莫问

赞同来自:

客厅

最好的方法是使用搜索正则表达式。

这是我目前正在使用的函数


def password_check/password/:
"""
Verify the strength of 'password'
Returns a dict indicating the wrong criteria
A password is considered strong if:
8 characters length or more
1 digit or more
1 symbol or more
1 uppercase letter or more
1 lowercase letter or more
"""

# calculating the length
length_error = len/password/ < 8

# searching for digits
digit_error = re.search/r"\d", password/ is None

# searching for uppercase
uppercase_error = re.search/r"[A-Z]", password/ is None

# searching for lowercase
lowercase_error = re.search/r"[a-z]", password/ is None

# searching for symbols
symbol_error = re.search/r"[ !#$%&'//*+,-./[\\\]^_`{|}~"+r'"]', password/ is None

# overall result
password_ok = not / length_error or digit_error or uppercase_error or lowercase_error or symbol_error /

return {
'password_ok' : password_ok,
'length_error' : length_error,
'digit_error' : digit_error,
'uppercase_error' : uppercase_error,
'lowercase_error' : lowercase_error,
'symbol_error' : symbol_error,
}


EDIT:

在Lukas的Ferry属性下,所以更新到特殊的符号条件检查


symbol_error = re.search/r"\W", password/ is None

三叔

赞同来自:

password.isalnum//

返回逻辑值,所以
password.isalnum//==password


总是


False

.

就在较低的部分
==password

:


if password.lower//== password or password.upper//==password or password.isalnum//:
# ...


接下来,它

绝不

不能同时所有的上下,或所有上部和数字,或所有较低的和所有数字,所以第二个条件 /平均/ 是不可能的。 也许你应该寻找可用性

一些

资本,线条和数字吗?

但是,您首先需要解决另一个问题。 您检查密码是否是

行业

-cifor,只包括字符和 / 或数字。 如果您想检查数字,请使用
http://docs.python.org/2/libra ... digit
.

你可能想熟悉
http://docs.python.org/2/libra ... thods
. 有方便的方法
.islower//


.isupper//

, 例如,您可以尝试:


>>> 'abc'.islower//
True
>>> 'abc123'.islower//
True
>>> 'Abc123'.islower//
False
>>> 'ABC'.isupper//
True
>>> 'ABC123'.isupper//
True
>>> 'Abc123'.isupper//
False


它们越来越快,冗长
password.upper// == password

, 以下将测试相同的问题:


if password.isupper// or password.islower// or password.isdigit//:
# very weak indeed


你想要探索的下一个伎俩 - 这是字符串上的循环,以便您可以查看单个字符:


>>> [c.isdigit// for c in 'abc123']
[False, False, False, True, True, True]


如果将它与函数相结合
any//

, 您可以检查是否存在

一些

数字的符号:


>>> any/c.isdigit// for c in 'abc123'/
True
>>> any/c.isdigit// for c in 'abc'/
False


我认为在测试密码强度时,您会发现这些技术很方便。

知食

赞同来自:

这是您写的内容的重复:


import re

def password//:
print /'Enter a password\n\nThe password must be between 6 and 12 characters.\n'/

while True:
password = input/'Password: ... '/
if 6 <= len/password/ < 12:
break
print /'The password must be between 6 and 12 characters.\n'/

password_scores = {0:'Horrible', 1:'Weak', 2:'Medium', 3:'Strong'}
password_strength = dict.fromkeys/['has_upper', 'has_lower', 'has_num'], False/
if re.search/r'[A-Z]', password/:
password_strength['has_upper'] = True
if re.search/r'[a-z]', password/:
password_strength['has_lower'] = True
if re.search/r'[0-9]', password/:
password_strength['has_num'] = True

score = len/[b for b in password_strength.values// if b]/

print /'Password is %s' % password_scores[score]/


出口 /样本/:


>>> password//
Enter a password

The password must be between 6 and 12 characters.

Password: ... ghgG234
Password is Strong

风见雨下

赞同来自:

我还搜索了单个密码可靠性检查功能,并找到了许多半工作的优惠。 我基于它们收集了自己的功能。

我们希望帮助


def get_pw_strength/ pw /:

s_lc = set/['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l', 'o', 'n', 'q', 'p', 's', 'r', 'u', 't', 'w', 'v', 'y', 'x', 'z']/
s_uc = set/['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'J', 'M', 'L', 'O', 'N', 'Q', 'P', 'S', 'R', 'U', 'T', 'W', 'V', 'Y', 'X', 'Z']/
s_dg = set/['1', '0', '3', '2', '5', '4', '7', '6', '9', '8']/
s_sp = set/['+', ',', '.', '-', '?', ':', '_', '/', '/', '*', '/', ';', '+', '!']/
pw_s = 0
pw_steps = /5, 8, 12/

pw_l = len/pw/
if / pw_l < 4 /:
return 0
for l in pw_steps :
if / pw_l > l /:
pw_s += 1
#print "length over ", l," giving point", pw_s

c_lc = c_uc = c_dg = c_sp = 0
for c in pw :
if / c in s_lc / :
c_lc += 1
if / c in s_uc / :
c_uc += 1
if / c in s_dg / :
c_dg += 1
if / c in s_sp / :
c_sp += 1
if / c_lc + c_uc + c_dg + c_sp <> pw_l /:
#print c_lc, c_uc, c_dg, c_sp, pw_l
#raise Exception "Forbidden chracter"
return -1
charset = 0
if / c_lc / :
pw_s += 1
charset = len/s_lc/
if / c_uc / :
pw_s += 1
charset = len/s_uc/
if / c_dg / :
pw_s += 1
charset = len/s_dg/
if / c_sp / :
pw_s += 2
charset = len/s_sp/
entropy = log/pow/charset,pw_l/,2/

return pw_s, entropy

要回复问题请先登录注册