如何使用Python加快密码生成脚本的速度?

时间:2018-08-08 16:52:59

标签: python-3.x random passwords

所以我有一个脚本,可以使用choice模块生成安全密码,但是我发现它的生成时间和磁盘写入速度确实很慢(〜0.4 Mo / s;对于生成的1,000,000密码; 16至32字符)

您有什么提示可以使此脚本更快?

密码生成也位于for mult in range(num_passwords)循环中

在这里:

import string, os, sys, random, time
from secrets import choice

#Requesting informations
#must be int
num_files = int(input("[INT] Number of files: "))
num_passwords = int(input("[INT] Number of codes: "))
num_bits_from = int(input("[INT] Number of bits (from): "))
num_bits_to = int(input("[INT] Number of bits (to): "))

#must be str
add_infos = str(input("[INT] Add additional infos in file? [1/0]: "))
folder_name = str(input("[STR] Folder name (create new): "))
file_prefix = str(input("[STR] File prefix: "))
file_extension = str(input("[STR] File extension: "))

#creating folder
os.mkdir(folder_name)

#User selects charlist or create a new one
#Default: chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
while True:
    print("Select a charlist:")
    print("1/ ASCII w/o specials")
    print("2/ ASCII w/ specials")
    print("3/ New charlist")
    charlistChoice = str(input("\n[INT] (1/2/3): "))
    if charlistChoice == "1":
        chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
        break
    elif charlistChoice == "2":
        chars = string.punctuation + string.ascii_uppercase + string.ascii_lowercase + string.digits
        break
    elif charlistChoice == "3":
        print("========")
        print("Please enter charlist on one line")
        print("Example: abcABC123?;!")
        chars = str(input("[CHARLIST]> "))
        break
    else:
        print("Invalid choice!")
        continue #Goes to the top of the while loop

input("press [ENTER] to generate")

#Charlist has been set, starting generation
print("Please wait, this can take a while")
print("========")

for mult in range(num_files): #For given number of files
    file_name = file_prefix + str(mult) + file_extension
    print("Generating", str(num_passwords), "passwords in", file_name, "...")

    if add_infos == "1": #if user requested to write add. infos in file
        currentFile = open(file_name, "w")
        currentFile.write("====Generation====\n")
        currentFile.write("bits per line (from): "+str(num_bits_from)+"\n")
        currentFile.write("bits per line (to): "+str(num_bits_to)+"\n")
        currentFile.write("total codes: "+str(num_passwords)+"\n")
        currentFile.write("====Start====\n")

        for x in range(num_passwords): #Generate WITH additionnal infos
            currentFile.write(str(x)+": "+''.join([choice(chars) for _ in range(random.randint(num_bits_from, num_bits_to))])+"\n")

    else: #Does not write additionnal infos in file
        currentFile = open(file_name, "w")

        for x in range(num_passwords): #Generate passwords with bits from int and to int
            currentFile.write(''.join([choice(chars) for _ in range(random.randint(num_bits_from, num_bits_to))])+"\n")

    currentFile.close()
    os.system("move "+file_name+" "+folder_name) #Closes and move file to folder, iterating again

#Generation completed

os.system("pause")
sys.exit(0) #Exits the program with code 0

提前谢谢!

0 个答案:

没有答案