Author Topic: Password Generator  (Read 2978 times)

0 Members and 1 Guest are viewing this topic.

Offline sarat1998

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 1
    • View Profile
Password Generator
« on: 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!
Code: [Select]

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
« Last Edit: February 09, 2013, 01:48:58 pm by sarat1998 »

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Password Generator
« Reply #1 on: February 09, 2013, 02:11:27 pm »
Nice,
How are you gonna filter the doubles?
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline sarat1998

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 1
    • View Profile
Re: Password Generator
« Reply #2 on: February 09, 2013, 07:31:23 pm »
no doubles are generated by this code

Offline Jboeggs

  • Serf
  • *
  • Posts: 23
  • Cookies: -2
    • View Profile
Re: Password Generator
« Reply #3 on: 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?

Offline DaNePaLI

  • Peasant
  • *
  • Posts: 55
  • Cookies: 12
  • Forever n00b
    • View Profile
Re: Password Generator
« Reply #4 on: 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:

Code: [Select]
#!/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()
« Last Edit: February 09, 2013, 08:49:03 pm by DaNePaLI »

Offline sarat1998

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 1
    • View Profile
Re: Password Generator
« Reply #5 on: 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!
Code: [Select]

#!/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
« Last Edit: February 10, 2013, 10:16:43 am by DeepCopy »