EvilZone

Hacking and Security => Hacking and Security => : sarat1998 February 09, 2013, 01:36:18 PM

: Password Generator
: sarat1998 February 09, 2013, 01:36:18 PM
This python code below can generate all possible strings of a specified length with all the characters present on a U.S. keyboard (94 characters) and store them in a text file. Hope this will be helpful!
:

from itertools import product
import random
def pwdgen():
    location = input("""Where do you want to store the words generated? (File Location) (Please specify the directory with / rather than \)""")
    maxlen = int(input('How long should the words generated be?'))
    plist, variable = [], ()
    while len(variable) <= maxlen:
        variable += (str(random.random()), )
    for variable in product(range(33, 127), repeat = maxlen):
        tstring = ''
        for i in range(0, maxlen):
            tstring += chr(variable[i])
        plist.append(tstring)
    file = open(location, 'w')
    for i in plist:
        file.write(i + '\n')
    file.close()
    return 'Done!'


P.S. : Sorry for the confusing variable names
: Re: Password Generator
: proxx February 09, 2013, 02:11:27 PM
Nice,
How are you gonna filter the doubles?
: Re: Password Generator
: sarat1998 February 09, 2013, 07:31:23 PM
no doubles are generated by this code
: Re: Password Generator
: Jboeggs February 09, 2013, 07:36:43 PM
This is actually very useful, is it fast or does it take a while to generator big passwords?
: Re: Password Generator
: DaNePaLI February 09, 2013, 08:38:02 PM
Well I am not a good python coder so I might be wrong. But, I am curious why are you first appending the generated strings in the list and then writing each string from the list in the file. Why not just directly write the generated strings to the file, that would be probably be better optimization to this code.

And, appending very large number of data in the list is gonna result MemoryError sooner or later based on the memory available in the system. So you might want to avoid it as even the 4 character passwords are gonna be 94 * 94 * 94 * 94 strings (Btw, I forgot to mention I'm weak in Mathematics) in number which is still huge to be appended in the list when it is not absolutely necessary. If you code is gonna cause MemoryError for 4 letters strings, you will not be able to do anything much with this generator since most of the systems do not allow passwords shorter than 6 these days.

Btw, why are you missing the shebang line (I guess its the right term) at the top of your script. (#!/usr/bin/python3)

Below is the slight modification I made:

:
#!/usr/bin/python3
from itertools import product
import random
def pwdgen():
    location = input("""Where do you want to store the words generated? (File Location) (Please specify the directory with / rather than \)""")
    maxlen = int(input('How long should the words generated be?'))
    file = open(location, 'w')
    variable = ()
    while len(variable) <= maxlen:
        variable += (str(random.random()), )
    for variable in product(range(33, 127), repeat = maxlen):
        tstring = ''
        for i in range(0, maxlen):
            tstring += chr(variable[i])
        file.write(tstring + '\n')
        #plist.append(tstring)
    #file = open(location, 'w')
    file.close()
    return 'Done!'

pwdgen()
: Re: Password Generator
: sarat1998 February 10, 2013, 07:21:41 AM
@Jboeggs Let's assume we want all possible 7 character strings. That is 64,847,759,419,264 strings. So it takes quite some time to generate these strings. BTW thanks for the reply.


@DaNePaLI You are absolutely correct! If you don't mind I would like to point out a minor mistake in your code, you forgot to assign plist to a new list. So, here is the working code for the password generator which is much faster! Thank you very much again! If anyone is using the previous code please use this!
:

#!/usr/bin/python3
from itertools import product
import random
def pwdgen():
    location = input("""Where do you want to store the words generated? (File Location) (Please specify the directory with / rather than \)""")
    maxlen = int(input('How long should the words generated be?'))
    file = open(location, 'w')
    plist, variable = [], ()
    while len(variable) <= maxlen:
        variable += (str(random.random()), )
    for variable in product(range(33, 127), repeat = maxlen):
        tstring = ''
        for i in range(0, maxlen):
            tstring += chr(variable[i])
        file.write(tstring + '\n')
        plist.append(tstring)
    file = open(location, 'w')
    file.close()
    return 'Done!'

Staff Edit
Do not double post