Search code examples
pythonloopspasswordsunique

Python: Creating multiple unique passwords


I am trying to create a script that generates X amount of random passwords. I have got the password generation part sorted for 1 case, but when I try and add 2 or more, it generates the same password for all cases. The code and result are below:

import string
import secrets

def random_secure_string(stringLength):
    secureStr = ''.join((secrets.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation) for i in range(stringLength)))
    return secureStr

Entities = ["MM","OO"]

Password = [random_secure_string(10)]

for x in Entities:
    for y in Password:
      print(x + ' ' + '=' + ' ' + y)

This returns the following

enter image description here

Can anyone help in generating a unique password for MM and a unique password for OO? Thank you


Solution

  • separate call of random_secure_string should help.

    Password = [random_secure_string(10), random_secure_string(10)]
    

    I would prefer to generate passwords explicitly inside the loop

    Entities = ["MM","OO"]
    
    for x in Entities:
    
        print(x + ' ' + '=' + ' ' + random_secure_string(10))