如何编写高效判断检测密码强度代码的有效性及安全性的疑问句标题?

如何编写高效判断检测密码强度代码的有效性及安全性的疑问句标题?

import re
def check_password_strength(password):
    """
    Check the strength of a given password.
    Parameters:
    password (str): The password to be checked.
    Returns:
    dict: A dictionary containing the strength of the password in different categories.
    """
    # Initialize the score
    score = 0
    # Check for length
    if len(password) >= 8:
        score += 1
    else:
        print("Password is too short. It should be at least 8 characters long.")
    # Check for uppercase letters
    if re.search(r'[AZ]', password):
        score += 1
    else:
        print("Password should contain at least one uppercase letter.")
    # Check for lowercase letters
    if re.search(r'[az]', password):
        score += 1
    else:
        print("Password should contain at least one lowercase letter.")
    # Check for digits
    if re.search(r'[09]', password):
        score += 1
    else:
        print("Password should contain at least one digit.")
    # Check for special characters
    if re.search(r'[W_]', password):
        score += 1
    else:
        print("Password should contain at least one special character (e.g., !@#$%^&*)")
    # Determine the strength based on the score
    if score == 5:
        strength = "Strong"
    elif score >= 3:
        strength = "Moderate"
    else:
        strength = "Weak"
    # Return the results
    return {
        "Strength": strength,
        "Score": score,
        "Details": {
            "Length": len(password) >= 8,
            "Uppercase": bool(re.search(r'[AZ]', password)),
            "Lowercase": bool(re.search(r'[az]', password)),
            "Digits": bool(re.search(r'[09]', password)),
            "Special Characters": bool(re.search(r'[W_]', password))
        }
    }
Example usage:
password = "Example@123"
result = check_password_strength(password)
print(f"Password: {password}")
print(f"Strength: {result['Strength']}")
print(f"Score: {result['Score']}")
print("Details:")
for detail, value in result['Details'].items():
    print(f"  {detail}: {'Passed' if value else 'Failed'}")

这段代码定义了一个函数check_password_strength,它接受一个密码字符串作为参数,然后根据密码的长度、大小写字母、数字和特殊字符的存在来评估密码的强度,函数返回一个包含密码强度、得分和详细评估结果的字典。

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1134886.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
未希新媒体运营
上一篇 2024-10-03 05:39
下一篇 2024-10-03 05:40

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入