如何使这些代码重复性降低?随机密码生成器

时间:2017-09-17 09:09:58

标签: python performance

我在Python中制作一个随机密码生成器,它从用户那里获取一个模板和密码数量,并创建符合该模板的随机密码。

现在它的工作原理很好,但它有4个基本相同的if语句,但我不确定如何减少重复性。

这是我拥有的 -

#!/usr/bin/python

import random, sys, string

digit = string.digits
lower = string.lowercase
upper = string.uppercase
symbol = string.punctuation


if len(sys.argv) != 3:
    print 'error, not enough arguments, need a template and the number of passwords'
    print 'ex: ./passgen Template1! 100'

else:
    passlist =[]
    template = sys.argv[1]

    for j in range(0,int(sys.argv[2])): 
        password = ''
        charSet = ''
        for i in template: #runs through every character in the template provided by the user and checks what each character is and adds the appropriate string constants to the character set
            if i.islower():
                charSet += lower
                password += charSet[random.randint(0,len(charSet)-1)]
                charSet = ''
            elif i.isupper():
                charSet += upper
                password += charSet[random.randint(0,len(charSet)-1)]
                charSet = ''
            elif i.isdigit():
                charSet += digit
                password += charSet[random.randint(0,len(charSet)-1)]
                charSet = ''
            elif i in string.punctuation:
                charSet += symbol
                password += charSet[random.randint(0,len(charSet)-1)]
                charSet = ''    

        passlist.append(password)

print passlist

我不喜欢我正在做这个动作

            password += charSet[random.randint(0,len(charSet)-1)]
            charSet = ''

对于每个if语句,它似乎是多余的,但我不知道其他任何方式。

输入和输出示例 -

./passgen.py Password1! 10
['Hozxmtll1*', 'Grprwixi2,', 'Fwqarvcu5.', 'Ikfywvth6_', 'Sndcnecv3&', 'Pkeerxpm0(', 'Tjiliflo5@', 'Ibftiisa8_', "Xiljjsss8'", 'Ukfupsri6]']

2 个答案:

答案 0 :(得分:0)

我假设i始终匹配任何给定条件(如果没有,请将else:continue置于最后elif块下)。

for i in template: #runs through every character in the template provided by the user and checks what each character is and adds the appropriate string constants to the character set
    if i.islower():
        charSet += lower
    elif i.isupper():
        charSet += upper
    elif i.isdigit():
        charSet += digit
    elif i in string.punctuation:
        charSet += symbol

    password += charSet[random.randint(0,len(charSet)-1)]
    charSet = ''

这很简单。顺便说一句:用伪随机数创建密码是非常不安全的。

P.S。:对我来说它有点像魅力:

$ python2 /tmp/pytest.py "sfid!" 5
['eqeu-', 'uztv,', "qxdg'", 'mahe+', 'wvsn[']

答案 1 :(得分:0)

您可以将字符集存储在列表中,并选择匹配的项目(如果有)。

charsets = [string.digits, string.lowercase, string.uppercase, string.punctuation]
passlist =[]
template = sys.argv[1]

for j in range(0, int(sys.argv[2])): 
    password = ''
    for i in template: 
        charset = [cs for cs in charsets if i in cs]
        if charset:
            charset = charset[0] 
            password += charset[random.randint(0, len(charset)-1)]
    passlist.append(password)

如果角色不属于charsets中任何项目的一部分,则循环移动到下一个角色而不修改password,但您可以设置默认值,如果您希望它具有固定长度。

另外我认为如果将随机字符串生成器包装在一个小函数中会更好。

def pass_gen(template):
    password = ''
    charsets = [string.digits, string.lowercase, string.uppercase, string.punctuation]
    for i in template: 
        charset = [cs for cs in charsets if i in cs]
        if charset:
            charset = charset[0] 
            password += charset[random.randint(0, len(charset)-1)]
    return password