#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import time
import string
import random
import difflib
import pypinyin
word_map = {
'~': 0, '`': 0, '~': 0,
'!': 1, '@': 2, '#': 3, '$': 4, '%': 5, '^': 6, '&': 7, '*': 8, '(': 9, ')': 10, '_': 11, '+': 12,
'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 10, '-': 11, '=': 12,
'q': 13, 'w': 14, 'e': 15, 'r': 16, 't': 17, 'y': 18, 'u': 19, 'i': 20, 'o': 21, 'p': 22,
'{': 23, '}': 24, '|': 25,
'[': 23, ']': 24, '\\': 25,
'·': 23, '「': 24, '」': 24, '、': 25,
'a': 26, 's': 27, 'd': 28, 'f': 29, 'g': 30, 'h': 31, 'j': 32, 'k': 33, 'l': 34,
':': 35, ':': 35, '"': 36, '“': 36, '”': 36,
';': 35, ';': 35, '\'': 36, '’': 36,
'z': 37, 'x': 38, 'c': 39, 'v': 40, 'b': 41, 'n': 42, 'm': 43,
'<': 44, '《': 44, '>': 45, '》': 45, '?': 46, '?': 46,
',': 44, ',': 44, '.': 45, '。': 45, '/': 46
}
def new_password(min_pass_len, max_pass_len, min_char_kind):
pass_choice = [string.ascii_lowercase, string.ascii_uppercase, string.digits, '!@#$%^&*-_=+']
pass_len = random.randint(min_pass_len, max_pass_len) # 随机密码长度
# 随机字符种类,如果为1表示密码包含该种字符
char_kind_count = random.randint(min_char_kind, 4)
char_kind_map = [0, 0, 0, 0]
for index in range(char_kind_count):
char_kind_map[index] = 1
random.shuffle(char_kind_map)
password, char_kind_num = [], 0
for index, contain_the_kind in enumerate(char_kind_map):
if contain_the_kind:
char_kind_num += 1 # 已完成随机添加的字符种类+1
# 最大可随机添加字符数=剩余可用密码位数-减预留密码位数
max_random_count = pass_len - len(password) - (char_kind_count - char_kind_num)
if char_kind_num == char_kind_count: # 最后一类字符的最小可随机添加字符数
min_random_count = min_pass_len - len(password)
min_random_count = min_random_count > 1 and min_random_count or 1
else:
min_random_count = 1
max_random_count = max_random_count >= min_random_count and max_random_count or min_random_count
random_char_count = random.randint(min_random_count, max_random_count) # 该类字符要随机添加的字符数
# 生成可选字符列表
choice_char_list = pass_choice[index]
while len(choice_char_list) < random_char_count:
choice_char_list += choice_char_list
password.extend(random.sample(choice_char_list, random_char_count)) # 随机添加字符
# print(char_kind_count, char_kind_num, min_random_count, max_random_count, random_char_count, index, password)
if len(password) >= pass_len:
break
random.shuffle(password) # 打乱字符列表
return ''.join(password)
def verify_password(password, min_pass_len, max_pass_len, min_char_kind, diff_str=None, diff_name=''):
# diff_name: 原密码/用户名, diff_str: old_password/username
if len(password) < min_pass_len:
return '密码不能小于%s个字符' % min_pass_len
if len(password) > max_pass_len:
return '密码不能大于%s个字符' % max_pass_len
lower_password = password.lower().strip()
for i in range(0, len(lower_password)):
sub_str = [word_map.get(char) for char in lower_password[i: i + 3] if char in word_map]
if len(sub_str) == 3 and (sub_str[0] + sub_str[2]) / 2 == sub_str[1] and -2 <= sub_str[0] - sub_str[2] <= 2:
return '密码不能包含键盘排序字符“%s”' % password[i: i + 3]
if diff_str:
lower_diff_str = ''.join([''.join(word) for word in pypinyin.pinyin(diff_str, style=pypinyin.NORMAL)]).lower().strip()
if lower_password in lower_diff_str or lower_diff_str in lower_password:
return '密码与%s相似度过高' % diff_name
if difflib.SequenceMatcher(None, lower_password, lower_diff_str).quick_ratio() >= 0.65:
return '密码与%s相似度过高' % diff_name
char_types = 0
for regex in [r'[0-9]', r'[a-z]', r'[A-Z]', r'[^0-9a-zA-Z]']:
if re.search(regex, password, re.S):
char_types += 1
if char_types >= min_char_kind:
return '校验通过'
return '密码至少包含数字、大小写字母、特殊字符中的%s种' % min_char_kind
if __name__ == '__main__':
min_pass_len, max_pass_len = 8, 12 # 密码长度范围,min_pass_len必须大于等于min_char_kind
min_char_kind = 3 # 最少字符种类,不能大于4种
while True:
password = new_password(min_pass_len, max_pass_len, min_char_kind)
check_result = verify_password(password, min_pass_len, max_pass_len, min_char_kind, '大', '用户名')
print(password, check_result)
if check_result != '校验通过':
break
time.sleep(0.01)
Python生成指定长度和字符类型的随机密码并校验安全性
于 2020-07-19 22:05:59 首次发布