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