[英]Is there a way to check the validity of a password only if 3/4 conditions are met?
我必須編寫一個程序來檢查密碼的有效性。 條件為:至少 8 個字符、1 個大寫字母、1 個小寫字母、1 個 0-9 范圍內的數字、1 個符號。 如果滿足 3 / 4 個條件(不包括密碼長度,因為它應始終超過 8 個字符),程序應檢查密碼強度。 如果密碼長度為 8 個字符,則強度等於 1。每增加 4 個字符,強度增加 1。如果滿足所有條件,強度應加倍。
在下面的代碼中,我寫了一大行代碼,但我認為我有一個錯誤,並且有一種更快的方法可以做到這一點。 所以我問你是否有辦法更快地做到這一點。 如果是,那會是什么?
def is_valid_password():
pwd=input("Enter a password: ")
f=1
l=0
u=0
d=0
s=0
p=0
if(len(pwd)>=8):
for i in pwd:
if i.islower():
l+=1
if i.isupper():
u+=1
if i.isdigit():
d+=1
if(i=='!' or i=='@' or i=='#' or i=='$' or i=='&' or i=='_'):
s+=1
if((l+u+d+s)==len(pwd) and l==0 and u>=1 and d>=1 and s>=1) or ((l+u+d+s)==len(pwd) and l>=1 and u==0 and d>=1 and s>=1) or ((l+u+d+s)==len(pwd) and l>=1 and u>=1 and d==0 and s>=1) or ((l+u+d+s)==len(pwd) and l>=1 and u>=1 and d>=1 and s==0) or ((l+u+d+s)==len(pwd) and l>=1 and u>=1 and d>=1 and s>=1):
if((l+u+d+s)==len(pwd) and l>=1 and u>=1 and d>=1 and s>=1):
if(len(pwd)>12):
f=(f+(len(pwd)-8)//4)*2
elif(len(pwd)>=9 and len(pwd)<=12):
f==4
elif(len(pwd)==8):
f==2
print(f"Valid password with strength {f}")
else:
if(len(pwd)>12):
f=f+(len(pwd)-8)//4
elif(len(pwd)>=9 and len(pwd)<=12):
f==2
elif(len(pwd)==8):
f==1
print(f"Valid password with strength {f}")
else:
print("Invalid password!")
while True:
is_valid_password()
我可能會列出一些函數,例如testLength()
、 testUpper()
、 testDigit()
等等。 這些函數只返回 1(已完成)或 0(失敗)。 請注意,擴展測試的數量和種類非常容易。
現在你寫類似
numPassed = testLength(pwd) + \
testUpper(pwd) + \
testDigit(pwd) + ...
if (numPassed < (3 * numTests)/4)
issueError("password too weak")
這是一種偽代碼,但我認為這個想法是可以理解的。 請注意,此代碼當然不會更快,但它易於理解且易於維護或擴展。 (只要您不想一次測試十億個密碼,它也不慢)。
就個人而言,我這樣做:
import re
def is_valid_password(PassWord):
Conditions = 0
if len(PassWord) < 8:
print(f"The password ({PassWord}) is too short !")
return 0
# Or re.findall("[a-z]", PassWord):
if PassWord != PassWord.lower():
Conditions += 1
# Or re.findall("[A-Z]", PassWord):
if PassWord != PassWord.upper():
Conditions += 1
if re.findall("[0-9]", PassWord):
Conditions += 1
# Or re.findall("[!@#$&_]", PassWord):
if re.findall("[^a-zA-Z0-9]", PassWord):
Conditions += 1
if Conditions < 3:
print(f"The password ({PassWord}) not respect the conditions !")
return 0
Power = 1 + int((len(PassWord) - 8) / 4)
if Conditions == 4:
Power *= 2
print(f"Valid password ({PassWord}) with strength {Power}")
return 1
is_valid_password("Hello") # Error
is_valid_password("hello2020") # Error
is_valid_password("HELLO2020") # Error
is_valid_password("Hello2022") # 1
is_valid_password("Hello 2022") # 2
is_valid_password("HelloHello2022") # 2
is_valid_password("Hello _ 2022") # 4
is_valid_password("Hello Hello 2022") # 6
我想它更簡單,更易讀。
您可以使用正則表達式嘗試這種方式。 包括您需要在括號中和模式中或模式中找到的內容:
passwd='P%&ippo8lo'
pattern=r'(\d)|([[:upper:]])|([[:lower:]])|([\$%&])'
1位數字,2個大寫字母,3個小寫字母,4個符號
'findall' 方法將返回一個集合列表,您只需確保所有字段都已使用:
rt=regex.findall(pattern, passwd)
[
('', 'P', '', '')
('', '', '', '%')
('', '', '', '&')
('', '', 'i', '')
('', '', 'p', '')
('', '', 'p', '')
('', '', 'o', '')
('8', '', '', '')
('', '', 'l', '')
('', '', 'o', '')
]
現在密碼的硬度取決於集合的多少字段已被使用:
good = [False] * 4
for s in rt:
for i in range(0, len(s)):
if s[i] != '':
good[i] = True
print(good)
[True, True, True, True]
如果其中一個請求未被使用,則對應的字段將為 False
關於密碼長度:
ovr_len = len(password) - 8
hardness_point = int(ovr_len / 4)
total_point += hardness_point
超過8個字符每4個1分
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.