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
,它接受一个密码字符串作为参数,然后根据密码的长度、大小写字母、数字和特殊字符的存在来评估密码的强度,函数返回一个包含密码强度、得分和详细评估结果的字典。
最新评论
本站CDN与莫名CDN同款、亚太CDN、速度还不错,值得推荐。
感谢推荐我们公司产品、有什么活动会第一时间公布!
我在用这类站群服务器、还可以. 用很多年了。