python实现密码强度校验


Posted in Python onMarch 18, 2020

本文实例为大家分享了python实现密码强度校验的具体代码,供大家参考,具体内容如下

一 校验规则

规则1 密码长度8位以上

规则2 密码需包含数字

规则3 密码需包含大小写字母

规则4 密码需包含特殊字符['+', '-', '*', '/', '_', '&', '%', ',']

规则5 校验5次不通过则强制退出

二 文件操作

每次输入的密码都会保存到文本文件中

以下是python的代码实现:

"""
  作者:zhengzhihui
  版本:7.0
  日期:2019/7/13
  功能:判断密码强度
  2.0功能:循环和终止
  3.0功能:将密码保存到文本中
  4.0功能:读取文件,遍历文件
  5.0功能:定义PasswordTool类
  6.0功能:定义FileTool类
  7.0功能:密码中增加大小写字母和特殊字符['+', '-', '*', '/', '_', '&', '%', ',']
"""
import time as tm
 
 
class FileTool():
  """
    文件工具类
  """
  def __init__(self, filepath):
    self.filepath = filepath
 
  def write_to_file(self, content):
    with open(self.filepath, 'a') as f:
      f.write(content)
 
  def read_from_file(self):
    with open(self.filepath, 'r') as f:
      content = f.readlines()
    return content
 
 
class PasswordTool():
  """
    密码工具类
  """
  def __init__(self, password):
    self.password = password
    self.strength_level = 0
 
  def check_number_exist(self):
    """
      判断是否含数字
    """
    has_number = False
    for c in self.password:
      if c.isnumeric():
        has_number = True
        break
    return has_number
 
  def check_letter_exist(self):
    """
      判断是否含字母
    """
    has_upper_letter = False
    has_lower_letter = False
    for c in self.password:
      if c.isupper():
        has_upper_letter = True
      elif c.islower():
        has_lower_letter = True
      has_both_letter = has_upper_letter and has_lower_letter
      if has_both_letter:
        break
    return has_both_letter
 
  def check_specialchar_exist(self):
    """
      判断是否包含特殊字符
    """
    has_specialchar = False
    specialchar_list = ['+', '-', '*', '/', '_', '&', '%', ',']
    for c in self.password:
      if c in specialchar_list:
        has_specialchar = True
        break
    return has_specialchar
 
  def process_password(self):
    """
      判断是否符合规则
    """
    # 规则1:长度至少8位
    if len(self.password) >= 8:
      self.strength_level += 1
    else:
      print('密码长度至少8位')
 
    # 规则2:必须包含数字
    if self.check_number_exist():
      self.strength_level += 1
    else:
      print('密码需要包含数字')
 
    # 规则3:必须包含大小写字母
    if self.check_letter_exist():
      self.strength_level += 1
    else:
      print('密码需要包含大小写字母')
 
    # 规则4:需要包含特殊字符
    if self.check_specialchar_exist():
      self.strength_level += 1
    else:
      print('密码需要包含至少一个特殊字符("+,-,*,/,_")')
 
 
def main():
  """
    主函数
  """
  try_times = 5
  pwd_strength_dict = {0: '弱', 1: '较弱', 2: '中', 3: '强', 4: '超强'}
  myfile = FileTool("password_7.0.txt")
 
  while try_times > 0:
    password = input('请输入密码: ')
    mypwdtool = PasswordTool(password)
    mypwdtool.process_password()
 
    now_time = tm.strftime("%Y-%m-%d %H:%M:%S", tm.localtime())
    myfile.write_to_file("日期:{} 密码:{} 强度:{}{}\n".format(now_time, password,
                          mypwdtool.strength_level, pwd_strength_dict[mypwdtool.strength_level]))
 
    if mypwdtool.strength_level >= 4:
      print('恭喜!密码合格')
      break
    else:
      print('密码不合格')
      try_times -= 1
      print()
  if try_times <= 0:
    print('尝试次数过多,密码设置失败!')
 
  content = myfile.read_from_file()
  print(content)
 
 
if __name__ == "__main__":
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现从web抓取文档的方法
Sep 26 Python
Python导入oracle数据的方法
Jul 10 Python
python简单获取数组元素个数的方法
Jul 13 Python
Python2.x与Python3.x的区别
Jan 14 Python
用Python将IP地址在整型和字符串之间轻松转换
Mar 22 Python
TensorFlow高效读取数据的方法示例
Feb 06 Python
浅谈Pandas中map, applymap and apply的区别
Apr 10 Python
python2.7实现爬虫网页数据
May 25 Python
python实现微信定时每天和女友发送消息
Apr 29 Python
Python中遍历列表的方法总结
Jun 27 Python
使用Python给头像戴上圣诞帽的图像操作过程解析
Sep 20 Python
python文件与路径操作神器 pathlib
Apr 01 Python
Python tcp传输代码实例解析
Mar 18 #Python
python实现用户名密码校验
Mar 18 #Python
Python3+selenium实现cookie免密登录的示例代码
Mar 18 #Python
Selenium启动Chrome时配置选项详解
Mar 18 #Python
python+selenium+Chrome options参数的使用
Mar 18 #Python
selenium WebDriverWait类等待机制的实现
Mar 18 #Python
Python socket处理client连接过程解析
Mar 18 #Python
You might like
PHP扩展mcrypt实现的AES加密功能示例
2019/01/29 PHP
Array.slice()与Array.splice()的返回值类型
2006/10/09 Javascript
JavaScript中获取元素索引的函数
2010/09/10 Javascript
JS实现两个大数(整数)相乘
2014/04/28 Javascript
jQuery实现网站添加高亮突出显示效果的方法
2015/06/26 Javascript
js实现兼容性好的微软官网导航下拉菜单效果
2015/09/07 Javascript
jQuery复制表单元素附源码分享效果演示
2015/09/30 Javascript
Javascript中字符串和数字的操作方法整理
2017/01/22 Javascript
Node层模拟实现multipart表单的文件上传示例
2018/01/02 Javascript
JS实现HTML页面中动态显示当前时间完整示例
2018/07/30 Javascript
当vue路由变化时,改变导航栏的样式方法
2018/08/22 Javascript
微信小程序自定义音乐进度条的实例代码
2018/08/28 Javascript
layui下拉列表select实现可输入查找的方法
2019/09/28 Javascript
vue3.0 上手体验
2020/09/21 Javascript
python使用cookielib库示例分享
2014/03/03 Python
Python实现删除Android工程中的冗余字符串
2015/01/19 Python
Python爬取三国演义的实现方法
2016/09/12 Python
基于hashlib模块--加密(详解)
2017/06/21 Python
浅析python协程相关概念
2018/01/20 Python
python实现猜数字小游戏
2020/03/24 Python
django项目简单调取百度翻译接口的方法
2019/08/06 Python
Python测试模块doctest使用解析
2019/08/10 Python
Python完全识别验证码自动登录实例详解
2019/11/24 Python
tensorflow 限制显存大小的实现
2020/02/03 Python
python logging 日志的级别调整方式
2020/02/21 Python
CSS3的颜色渐变效果的示例代码
2017/09/29 HTML / CSS
Michael Kors加拿大官网:购买设计师手袋、手表、鞋子、服装等
2019/03/16 全球购物
西班牙最大的婴儿用品网上商店:Bebitus
2019/05/30 全球购物
《雕塑之美》教学反思
2014/04/24 职场文书
咖啡厅商业计划书
2014/09/15 职场文书
接收函格式
2015/01/30 职场文书
售后前台接待岗位职责
2015/04/03 职场文书
产品调价通知函
2015/04/20 职场文书
写给女朋友的检讨书
2015/05/06 职场文书
导游词之任弼时故居
2020/01/07 职场文书
java中为什么说子类的构造方法默认访问的是父类的无参构造方法
2022/04/13 Java/Android