how to make a password generator without using multiple loops?

Asked

Viewed 197 times

0

As you can see the program below "generates" several strings of unique characters. The intention here is not to create a specific algorithm for this, it is just an example. The case study is about ways to reduce the amount of loops nested in this type of occasion.
For general purposes follow the code:

char = [chr(i) for i in range(33, 123)]

for one in char: 
    for two in char: 
        for three in char: 
            for four in char: 
                for five in char: 
                    for six in char: 
                        for seven in char: 
                            for eight in char: 
                                for nine in char: 
                                    print(one, two, three, four, five, six, seven, eight, nine)

  • 1

    Search about the module itertools

  • 1

    I had to run it to understand that it’s a brute force algorithm for breaking passwords.

1 answer

0

It would not be the case to use a recursive function?

char = [chr(i) for i in range(33, 123)]

#print(char) tinha usado essa linha num teste e esqueci de tirar

def attk(senha, n):
  if (n > 0):
    for item in char: 
       attk(senha + item, n - 1)
  else:   
    print(senha)      


attk("", 9)

Code in the Repl.it

  • I’m sorry, but because I was negative?

  • 1

    It’s life Augusto. There are people here who give negative for any reason kkkkkk #Sad ;-;. Anyway, one thing you can put into your answer is the use of the itertools.product function. I believe it is the simplest and best way to generate various combinations.

  • @Jeanextreme002, if the person was negative because I didn’t use the itertools.product she did not take into account that the itertools cannot make this product for nine digits. With four houses it already gives OUT_OF_MEMORY error and ends the process. From a look https://repl.it/repls/JumboSlimFirm

  • @Jeanextreme002, thanks for the remark.

  • 1

    How strange. I use itertools.product with more than 4 houses with my interpreter and it does not give me this '-' error. Edit: I tested the list() and it worked . Do you know why ? https://repl.it/repls/LiveDelightfulSystemsanalysis

  • Consumed the memory because the fool here did list( product(char, repeat=4)), forcing itertools to consume all the RAM in a list with all the results. I hesitate my.

  • 1

    So né.... 50 I count for correcting your error kkk

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.