判断密码强度,数字、小写字母、大写字母和指定的标点符号,分别对应weak、below middle、above middle、strong。 python代码
时间: 2024-01-26 22:03:16 浏览: 100
以下是Python代码实现:
```python
import re
def check_password_strength(password):
# 判断密码是否包含数字、小写字母、大写字母和指定的标点符号
has_number = bool(re.search(r'\d', password))
has_lower = bool(re.search(r'[a-z]', password))
has_upper = bool(re.search(r'[A-Z]', password))
has_symbol = bool(re.search(r'[!@#$%^&*(),.?":{}|<>]', password))
# 根据是否包含不同类型的字符判断密码强度
if has_number and not has_lower and not has_upper and not has_symbol:
return 'weak'
elif (has_lower and not has_upper) or (has_upper and not has_lower):
return 'below middle'
elif has_lower and has_upper and not has_symbol:
return 'above middle'
elif has_number and has_lower and has_upper and has_symbol:
return 'strong'
else:
return 'weak'
```
使用方法:
```python
password = 'AbCdE123!@#'
strength = check_password_strength(password)
print(strength) # 输出 strong
```
阅读全文