密码安全性检查(包含数字、小写字母、大写字母)

Stephan and Sophia forget about security and use simple passwords for everything. Help Nikola develop a password security check module. The password will be considered strong enough if its length is greater than or equal to 10 symbols, it has at least one digit, as well as containing one uppercase letter and one lowercase letter in it. The password contains only ASCII latin letters or digits.

Input: A password as a string.

Output: Is the password safe or not as a boolean or any data type that can be converted and processed as a boolean. In the results you will see the converted results.

斯蒂芬和索菲亚对于一切都使用简单的密码,忘记了安全性。请你帮助尼古拉开发一个密码安全检查模块。如果密码的长度大于或等于10个字符,且其中至少有一个数字、一个大写字母和一个小写字母,该密码将被视为足够强大。密码只包含ASCII拉丁字母或数字。

输入: 密码。

输出: 密码的安全与否,作为布尔值(bool),或者任何可以转换和处理为布尔值的数据类型。你会在结果看到转换后的结果(True 或 False)。


def checkio(data: str) -> bool:

    if data.__len__() < 10:
        return False
    flag1 = False
    flag2 = False
    flag3 = False
    # if re.match('[^A-Z]*[A-Z]+[^a-z]*[a-z]+[^0-9]*[0-9]+.*',str):    别人的正则方法,有问题
    for i in data:
        if i.isdigit():
            flag1 = True
        if i.islower():
            flag2 = True
        if i.isupper():
            flag3 = True
    if flag1 == False or flag2 == False or flag3 == False:
        return False
    #replace this for solution
    return True or False
#Some hints
#Just check all conditions
if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio('A1213pokl') == False, "1st example"
    assert checkio('bAse730onE4') == True, "2nd example"
    assert checkio('asasasasasasasaas') == False, "3rd example"
    assert checkio('QWERTYqwerty') == False, "4th example"
    assert checkio('123456123456') == False, "5th example"
    assert checkio('QwErTy911poqqqq') == True, "6th example"

    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

他人写法1:(正则的)

import re

DIGIT_RE = re.compile('\d')
UPPER_CASE_RE = re.compile('[A-Z]')
LOWER_CASE_RE = re.compile('[a-z]')

def checkio(data):
    """
    Return True if password strong and False if not
    
    A password is strong if it contains at least 10 symbols,
    and one digit, one upper case and one lower case letter.
    """
    if len(data) < 10:
        return False
    
    if not DIGIT_RE.search(data):
        return False

    if not UPPER_CASE_RE.search(data):
        return False

    if not LOWER_CASE_RE.search(data):
        return False
        
    return True

他人写法2:

checkio = lambda s: not(
        len(s) < 10
        or s.isdigit()
        or s.isalpha()
        or s.islower()
        or s.isupper()
    ) 

他人写法3:

def checkio(s):
    fs = ''.join(filter(str.isalnum, s)) # keep only letters and digits
    return (
            len(fs) >= 1        # There is at least one letter or digit
        and len(s)  >= 10       # ... and there are at least 10 characters
        and not fs.isalpha()    # ... and there is at least one digit
        and not fs.isdigit()    # ... and there is at least one letter
        and not fs.islower()    # ... and not all letters are lowercase
        and not fs.isupper()    # ... and not all letters are uppercase
    )

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以采用如下的方法来生成包含大写字母小写字母数字和特殊符号的密码: 1. 创建一个包含所有可能字符的字符串,例如: ``` char all_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+={}[]|\:;\"'<>,.?/"; ``` 2. 使用随机数生成器来从该字符串中选择字符并将其添加到密码中,直到密码的长度达到所需长度。可以使用 `rand()` 函数来生成随机数。 3. 每次从字符串中选择字符时,可以使用 `rand()` 函数生成一个随机索引,然后将该索引对应的字符添加到密码中。 4. 确保密码包含至少一个大写字母一个小写字母一个数字一个特殊符号。可以在生成密码的过程中,添加一些特定的字符,例如: ``` char special_chars[] = "!@#$%^&*()_-+={}[]|\:;\"'<>,.?/"; ``` 然后,在生成密码的过程中,确保该字符串中至少包含一个字符。 下面是一个生成包含大写字母小写字母数字和特殊符号的密码的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); // 初始化随机数生成器 char all_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+={}[]|\\:;\"'<>,.?/"; char special_chars[] = "!@#$%^&*()_-+={}[]|\\:;\"'<>,.?/"; char password[16]; // 密码长度为 16 int i; // 确保密码包含至少一个大写字母一个小写字母一个数字一个特殊符号 password[0] = 'A' + rand() % 26; password[1] = 'a' + rand() % 26; password[2] = '0' + rand() % 10; password[3] = special_chars[rand() % strlen(special_chars)]; // 生成其余的密码字符 for (i = 4; i < 16; i++) { password[i] = all_chars[rand() % strlen(all_chars)]; } // 打印生成的密码 printf("Generated password: %s\n", password); return 0; } ``` 注意,这只是一种简单的方法来生成密码。实际上,为了确保密码安全性,应该使用更加复杂和专业的方法来生成密码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值