本帖最后由 阿奇_o 于 2021-7-6 01:30 编辑 import re
def passwd_test(pw):
# 低强度
if re.fullmatch(r"(\d{1,8})|(\w{1,8})", pw):
# print(re.fullmatch(r"(\d{1,8})|(\w{1,8})", pw))
return "弱密码"
# 中强度
# https://gist.github.com/leadfast/1932460490b637260b92ad9a200d4eac
pt0 = re.compile(r"(?=^.{8,15}$)(?=(?:.*?\d){1})(?=(?:.*?[a-zA-Z]){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")
pt1 = re.compile(r"(?=^.{8,}$)(?=(?:.*?[a-zA-Z]){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$") # 字母和特殊字符的组合,无数字
pt2 = re.compile(r"(?=^.{8,}$)(?=(?:.*?\d){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$") # 数字和特殊字符组合,无字母
pt3 = re.compile(r"\d\w{8,}") # 数字和字母组合,无特殊字符
for p in [pt0, pt1, pt2, pt3]:
if re.search(p, pw):
return "中强度"
# 高强度
pt = re.compile(r"(?=^[a-zA-Z]{1,}.{15,}$)(?=(?:.*?\d){1})(?=(?:.*?[a-zA-Z]){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")
if re.search(pt, pw):
return "强密码"
print(passwd_test('1234678'))
print(passwd_test('123z678@'))
print(passwd_test('password1234!@#')) # 中强度,位数不够
print(passwd_test('password1234!@#$')) # 强密码,刚够16位
print(passwd_test('password1234!@#$.')) # 强密码
print(passwd_test('100password1234!@#$.')) # 中强度,不是字母开头
正则。。you make me crazy! |