zoukankan      html  css  js  c++  java
  • 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)

     

  • 相关阅读:
    C# GetHashCode 部分冲突列表 数字字符串版本
    Amazon Dynamo DB
    SCTP 一句话介绍
    SystemTap 使用以及安装
    Windows Azure Service Disruption on Feb 29th
    发布ASP.NET MVC3网站
    SQLServer数据集合的交、并、差集运算
    DataTable的几个函数
    oracle创建表空间以及用户的语句
    asp.net mvc3及odp.net资料下载地址
  • 原文地址:https://www.cnblogs.com/hyj691001/p/10247856.html
Copyright © 2011-2022 走看看