20190110-生成密码以及简易密码强度检查

1.生成9位字母的密码

使用random.choice函数,此函数需要一个序列,因此给定一个序列包含a-z,A-Z

#step1:生成序列
import random
s1=''
for i in range(97,123):
    s1+=chr(i)+chr(i-32)  
#step2: 生成密码
password=''
for i in range(9):
    password+=random.choice(s1)#从s1序列中随机选取一个元素
print('9位数的密码为:',password)

2:生成9位数字和字母的密码,密码可能随机出现数字和字母
此题在上一题的基础上先生成一个序列包含所有字母和数字,然后使用random.choice()函数

import random
s1=''
for i in range(97,123):
    s1+=chr(i)+chr(i-32)
s1+='0123456789'
print(s1)
#生成包含小写字母和数字的序列
#另外一个写法
import string
s1 = string.ascii_letters+string.digits
password=''
for i in range(9):
    password+=random.choice(s1)
print('随机密码为:',password)

3.检测密码强度

c1 : 长度>=8
c2: 包含数字和字母
c3: 其他可见的特殊字符
强密码条件:满足c1,c2,c3
中密码条件: 只满足任一2个条件
弱密码条件:只满足任一1个或0个条件

思路:先将c1,c2,c3三个条件写成函数,以Ture和False返回,True为满足,False为不满足

step1.写出3个条件函数

def check_len(password):
    if len(password)>=8:
        return True
#长度大于8则为True
    else:
        return False
def check_letter_type(password):
    import string
    result =False
    for i in string.ascii_letters:
#大小写字母
        if i in password:
#密码中包含字母
            for j in string.digits:
#数字
                if j in password:
#密码中包含数字
                    result =True
    return result
def check_punctuation(password):
    import string
    result = False
    for i in string.punctuation:
        if i in password:
#密码中包含特殊字符
            result =True
    return result

check_len检查密码长度,check_letter_type检查密码中是否包含字母和数字,check_punctuation检查密码中是否包含特殊字符

step2:检查密码满足几个条件

def pass_verify(password):
    count = 0 
#使用count来记录password满足几个条件
    if check_len(password):
        count +=1
    if check_letter_type(password):
        count +=1
    if check_punctuation(password):
        count +=1
    if count==3:
        print("强密码")
    elif count ==2:
        print('中密码')
    else:
        print('弱密码')
pass_verify(password)

 

转载于:https://www.cnblogs.com/hyj691001/p/10247856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值