Login Register






[Python] Random password generator. filter_list
Author
Message
[Python] Random password generator. #1
Code:
import random def passgen(x): char='abcdefghijklmnopqrstuvwxzy' charup='ABCDEFGHIJKLMNOPQRSTUVWXYZ' nums='0123456789' sym='!@#$%^&*()_+-=,<.>/?;:"{[}]|' password='' while len(password)!=x: if random.randrange(0,100)/2==0: password+=char[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=charup[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=nums[random.randrange(0,9)] elif random.randrange(0,100)/2==0: password+=sym[random.randrange(0,28)] return password print(passgen(10))
[Image: fow57.jpg]

Reply

RE: [Python] Random password generator. #2
Nice snippet. Did you do it this way to get the different character types with different probabilities?
Otherwise you could just put them all in one array.

I suggest doing this instead of randrange:

Code:
password += random.choice(sym)

It is more clear and you avoid the inflexibility of the hardcoded range, i.e. if you add a character to sym, you don't have to change the max value for randrange. You could also do this by using len(sym), though.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: [Python] Random password generator. #3
(02-19-2013, 01:52 PM)Deque Wrote: Did you do it this way to get the different character types with different probabilities?

Yes.

(02-19-2013, 01:52 PM)Deque Wrote: I suggest doing this instead of randrange:

Code:
password += random.choice(sym)

It is more clear and you avoid the inflexibility of the hardcoded range, i.e. if you add a character to sym, you don't have to change the max value for randrange. You could also do this by using len(sym), though.

Thanks for this info.i was not aware that there is something like random.choice.
[Image: fow57.jpg]

Reply

RE: [Python] Random password generator. #4
Nice one mate, I'll add this to my code collection Smile
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: [Python] Random password generator. #5
thank for your help Biggrin
[Image: 4180_gif_by_d53865-d31l0il.gif]


“If they ever tell my story let them say I walked with giants, men rise and fall like the Winter wheat but these names will never die...let them say I lived in the time of Achilles...”



Reply

RE: [Python] Random password generator. #6
You could make it even more save and user-friendly by saving the password in a txt-file by doing this you could counter keyloggers in stealing you password (Just in case)

The final code would be like this (you could eventually make the program print the password too).

Code:
import random def passgen(x): char='abcdefghijklmnopqrstuvwxzy' charup='ABCDEFGHIJKLMNOPQRSTUVWXYZ' nums='0123456789' sym='!@#$%^&*()_+-=,<.>/?;:"{[}]|' password='' while len(password)!=x: if random.randrange(0,100)/2==0: password+=char[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=charup[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=nums[random.randrange(0,9)] elif random.randrange(0,100)/2==0: password+=sym[random.randrange(0,28)] return password #Added Code Goes Here: def doc(x): try: f = open('pwd.txt', 'w') f.write(x) f.close() return True except Exception, e: print e return False if doc(passgen(10)): print "Password Saved in pwd.txt." raw_input() else: raw_input()

Reply

RE: [Python] Random password generator. #7
hey can u tell me where u learn these? Tongue

Reply

RE: [Python] Random password generator. #8
hey can u tell me where u learn these? Tongue

Reply

RE: [Python] Random password generator. #9
hey can u tell me where u learn these? Tongue

Reply

RE: [Python] Random password generator. #10
(10-23-2013, 10:23 AM)NoobieXD Wrote: hey can u tell me where u learn these? Tongue

Read this: http://ictc.aeoi.org.ir/sites/default/fi...Python.pdf
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply