Reputation: 41
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
Can anyone help in generating a unique password for MM and a unique password for OO? Thank you
Upvotes: 0
Views: 477
Reputation: 718
As I commented above, you are calling password generator function just one time. You can generate different passwords at each run.
That would be what you are looking for:
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"]
# generate 2 passwords
passwords = [random_secure_string(10),random_secure_string(10)]
for (i, entity) in enumerate(entities):
print(entity + ' ' + '=' + ' ' + passwords[i])
Upvotes: 1
Reputation: 257
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))
Upvotes: 1