Python实现的密码强度检测器示例

yipeiwu_com7年前Python基础

本文实例讲述了Python实现的密码强度检测器。分享给大家供大家参考,具体如下:

密码强度

密码强度如何量化呢?

一个密码可以有以下几种类型:长度、大写字母、小写字母、数字以及特殊符号。

显然,密码包含的特征越多、长度越长,其强度也就越高。

我们设置几个等级来评测密码强度,分别是:terrible, simple,
medium, strong
。

不同的应用可能对密码强度的要求不一样,我们引入最小程度(min_length)和最小特征数(min_types),作为可配置选项。

这样我们就可以检测密码包含的特征,特征与密码之间的关系可以简单定义为:

特征数 强度
小于最小长度 terrible
常用密码或规则的密码 simple
小于最小特征数 medium
大于或等于最小特征数 strong

另:常用的1万个密码点击此处本站下载。

代码实现

check.py

# coding: utf-8
"""
check
Check if your password safe
"""
import re
# 特征
NUMBER = re.compile(r'[0-9]')
LOWER_CASE = re.compile(r'[a-z]')
UPPER_CASE = re.compile(r'[A-Z]')
OTHERS = re.compile(r'[^0-9A-Za-z]')
def load_common_password():
 words = []
 with open("10k_most_common.txt", "r") as f:
  for word in f:
   words.append(word.strip())
 return words
COMMON_WORDS = load_common_password()
# 管理密码强度的类
class Strength(object):
 """
 密码强度三个属性:是否有效valid, 强度strength, 提示信息message
 """
 def __init__(self, valid, strength, message):
  self.valid = valid
  self.strength = strength
  self.message = message
 def __repr__(self):
  return self.strength
 def __str__(self):
  return self.message
 def __bool__(self):
  return self.valid
class Password(object):
 TERRIBLE = 0
 SIMPLE = 1
 MEDIUM = 2
 STRONG = 3
 @staticmethod
 def is_regular(input):
  regular = ''.join(['qwertyuiop', 'asdfghjkl', 'zxcvbnm'])
  return input in regular or input[::-1] in regular
 @staticmethod
 def is_by_step(input):
  delta = ord(input[1]) - ord(input[0])
  for i in range(2, len(input)):
   if ord(input[i]) - ord(input[i - 1]) != delta:
    return False
  return True
 @staticmethod
 def is_common(input):
  return input in COMMON_WORDS
 def __call__(self, input, min_length=6, min_type=3, level=STRONG):
  if len(input) < min_length:
   return Strength(False, "terrible", "密码太短了")
  if self.is_regular(input) or self.is_by_step(input):
   return Strength(False, "simple", "密码有规则")
  if self.is_common(input):
   return Strength(False, "simple", "密码很常见")
  types = 0
  if NUMBER.search(input):
   types += 1
  if LOWER_CASE.search(input):
   types += 1
  if UPPER_CASE.search(input):
   types += 1
  if OTHERS.search(input):
   types += 1
  if types < 2:
   return Strength(level <= self.SIMPLE, "simple", "密码太简单了")
  if types < min_type:
   return Strength(level <= self.MEDIUM, "medium", "密码还不够强")
  return Strength(True, "strong", "密码很强")
class Email(object):
 def __init__(self, email):
  self.email = email
 def is_valid_email(self):
  if re.match("^.+@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", self.email):
   return True
  return False
 def get_email_type(self):
  types = ['qq', '163', 'gmail', '126', 'sina']
  email_type = re.search('@\w+', self.email).group()[1:]
  if email_type in types:
   return email_type
  return 'wrong email'
password = Password()

test_check.py: 用于单元测试

# coding: utf-8
"""
test for check
"""
import unittest
import check
class TestCheck(unittest.TestCase):
 def test_regular(self):
  rv = check.password("qwerty")
  self.assertTrue(repr(rv) == "simple")
  self.assertTrue('规则' in rv.message)
 def test_by_step(self):
  rv = check.password("abcdefg")
  self.assertTrue(repr(rv) == "simple")
  self.assertTrue('规则' in rv.message)
 def test_common(self):
  rv = check.password("password")
  self.assertTrue(repr(rv) == "simple")
  self.assertTrue('常见' in rv.message)
 def test_medium(self):
  rv = check.password("ahj01a")
  self.assertTrue(repr(rv) == 'medium')
  self.assertTrue('不够强' in rv.message)
 def test_strong(self):
  rv = check.password("asjka9AD")
  self.assertTrue(repr(rv) == 'strong')
  self.assertTrue('很强' in rv.message)
 # 测试邮箱
 def test_email(self):
  rv = check.Email("123@gmail.com")
  self.assertEqual(rv.is_valid_email(), True)
 def test_email_type(self):
  rv = check.Email("123@gmail.com")
  types = ['qq', '163', 'gmail', '126', 'sina']
  self.assertIn(rv.get_email_type(), types)
if __name__ == '__main__':
 unittest.main()

PS:这里再为大家提供两款相关在线工具供大家参考使用:

在线随机数字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu

高强度密码生成器:
http://tools.jb51.net/password/CreateStrongPassword

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python编写打字训练小程序

Python编写打字训练小程序

你眼中的程序猿 别人眼中的程序猿,是什么样子?打字如飞,各种炫酷的页面切换,一个个好似黑客般的网站破解。可现实呢? 二指禅的敲键盘,写一行代码,查半天百度…那么如何能让我们从外表上变得...

简析Python的闭包和装饰器

简析Python的闭包和装饰器

什么是装饰器? 装饰器(Decorator)相对简单,咱们先介绍它:“装饰器的功能是将被装饰的函数当作参数传递给与装饰器对应的函数(名称相同的函数),并返回包装后的被装饰的函数”,听起来...

Django 数据库同步操作技巧详解

Django 数据库同步操作技巧详解

同步数据库: 使用上述两条命令同步数据库 1.认识migrations目录: migrations目录作用:用来存放通过makemigrations命令生成的数据库脚本,里面的生成...

mac下pycharm设置python版本的图文教程

mac下pycharm设置python版本的图文教程

安装了pycharm 5.0.1,创建了测试项目,发现python的版本是2.6.9的,系统自带的版本好像是2.7的,为什么这样,怎么切换到2.7 看了一下系统到底装了些什么版本 居...

用Pelican搭建一个极简静态博客系统过程解析

用Pelican搭建一个极简静态博客系统过程解析

我一直建议每个开发者都要有写博客记笔记的习惯,一来可以沉淀知识,二来可以帮助别人,我使用过很多博客平台,也用Python开发过博客系统,就这么个东西折腾好几年,一直找不到理想的产品,直到...