944,117 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 815
  • Python RSS
Nov 6th, 2009
0

Password Generator

Expand Post »
I know Remembering secure passwords is difficult. So I made this.
Python Syntax (Toggle Plain Text)
  1. import random, os
  2. a= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','{',':','>','?','}','|','!','@','#','$','%','^','&','*','(',')','-','+']
  3.  
  4. z = int(raw_input("How many words in your password?: "))
  5. os.system("clear")
  6. List = []
  7. for x in xrange(z):
  8. List.append(random.choice(a))
  9. print "".join(List)
  10. print "1) Yes"
  11. print "2) No"
  12. b = int(raw_input("Would you like to save this password to a text file?: "))
  13. if b == 1:
  14. c = raw_input("What is this password for?: ")
  15. x = open("%s.txt" % c, "w")
  16. y = "".join(List)
  17. x.write(y)
  18. x.close
  19. x = open("%s.txt" % c, "r")
  20. x.read
  21. print "Saved!"
  22. elif b == 2:
  23. raw_input("Press Enter To Continue")
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
nevets04 is offline Offline
28 posts
since Oct 2009
Nov 7th, 2009
0
Re: Password Generator
I created a Random Password Generator awhile back as well. Mine how ever has a GUI. You can find the complete program at CoderProfile.
Python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2.  
  3. #----------------------------------------------------------------------
  4. # randompass.py
  5. # Author: Shady Tyrant
  6. # 07/20/2009
  7. #----------------------------------------------------------------------
  8.  
  9. import sys
  10. import random
  11. from GladeWindow import *
  12.  
  13. #----------------------------------------------------------------------
  14.  
  15. class randompass(GladeWindow):
  16.  
  17. #----------------------------------------------------------------------
  18.  
  19. def __init__(self):
  20.  
  21. ''' '''
  22.  
  23. self.init()
  24.  
  25. #----------------------------------------------------------------------
  26.  
  27. def init(self):
  28.  
  29. filename = 'randompass.glade'
  30.  
  31. widget_list = [
  32. 'window1',
  33. 'charNumEntry',
  34. 'genButton',
  35. 'clearButton',
  36. 'passEntry',
  37. ]
  38.  
  39. handlers = [
  40. 'on_genButton_clicked',
  41. 'on_clearButton_clicked',
  42. ]
  43.  
  44. top_window = 'window1'
  45. GladeWindow.__init__(self, filename, top_window, widget_list, handlers)
  46. #----------------------------------------------------------------------
  47.  
  48. def on_genButton_clicked(self, *args):
  49. # Creates tuple of chars used, 71 total
  50. mystring = ("A","B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", \
  51. "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", \
  52. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", \
  53. "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", \
  54. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "@", "#", \
  55. "$", "%", "^", "&", "*", "_")
  56.  
  57. try:
  58. num = int(self.widgets['charNumEntry'].get_text())
  59. if num >= 72:
  60. self.widgets['passEntry'].set_text('ERROR: length to large')
  61. except:
  62. self.widgets['passEntry'].set_text('ERROR: Failed Recieving Length Value')
  63.  
  64. # Creates random pass and prints to textbox
  65. rPass = ''.join(random.sample(mystring, num))
  66. self.widgets['passEntry'].set_text(rPass)
  67.  
  68. #----------------------------------------------------------------------
  69.  
  70. def on_clearButton_clicked(self, *args):
  71. self.widgets['passEntry'].set_text('')
  72. self.widgets['charNumEntry'].set_text('')
  73.  
  74. #----------------------------------------------------------------------
  75.  
  76. def main(argv):
  77.  
  78. w = randompass()
  79. w.show()
  80. gtk.main()
  81.  
  82. #----------------------------------------------------------------------
  83.  
  84. if __name__ == '__main__': main(sys.argv)
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 7th, 2009
0
Re: Password Generator
Click to Expand / Collapse  Quote originally posted by nevets04 ...
I know Remembering secure passwords is difficult. So I made this.
Python Syntax (Toggle Plain Text)
  1. import random, os
  2. a= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','{',':','>','?','}','|','!','@','#','$','%','^','&','*','(',')','-','+']
  3.  
  4. z = int(raw_input("How many words in your password?: "))
  5. os.system("clear")
  6. List = []
  7. for x in xrange(z):
  8. List.append(random.choice(a))
  9. print "".join(List)
  10. print "1) Yes"
  11. print "2) No"
  12. b = int(raw_input("Would you like to save this password to a text file?: "))
  13. if b == 1:
  14. c = raw_input("What is this password for?: ")
  15. x = open("%s.txt" % c, "w")
  16. y = "".join(List)
  17. x.write(y)
  18. x.close
  19. x = open("%s.txt" % c, "r")
  20. x.read
  21. print "Saved!"
  22. elif b == 2:
  23. raw_input("Press Enter To Continue")
Nice effort!

My comment, I see you are still using tabs for indentations. Avoid them and use 4 spaces as most other folks. If you use tabs, you sooner or later will mix them with spaces, then your code becomes a mess. Also, if you want to use the 2to3.py utility to convert your code for Python3 later on, it will kick you!

Also avoid os.system("clear"), it is simply not needed and is not at all portable to other systems.
Last edited by vegaseat; Nov 7th, 2009 at 11:02 am.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 7th, 2009
1
Re: Password Generator
Instead of manually writing out all the letters and digits do this:

Python Syntax (Toggle Plain Text)
  1. import string
  2. a = list(string.ascii_letters + string.digits + "!@#$%^&*()-+={}|\/?><,.'")
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Nov 7th, 2009
0
Re: Password Generator
Click to Expand / Collapse  Quote originally posted by AutoPython ...
Instead of manually writing out all the letters and digits do this:

Python Syntax (Toggle Plain Text)
  1. import string
  2. a = list(string.ascii_letters + string.digits + "!@#$%^&*()-+={}|\/?><,.'")
Oh thats a neat piece of code. I didn't know you could do that.
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 7th, 2009
1
Re: Password Generator
I actually learned that when I was making a random Password Generator myself .
BTW, That piece of code includes uppercase and lowercase letters. If you want just lower case letters you can use "string.ascii_lowercase" instead of "string.ascii_letters", or if you want all uppercase letters you can do "string.ascii_uppercase".
Last edited by AutoPython; Nov 7th, 2009 at 2:02 pm.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Nov 7th, 2009
0
Re: Password Generator
Also the OP didn't want a zero in the digits part.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 7th, 2009
0
Re: Password Generator
Okay, instead use:

Python Syntax (Toggle Plain Text)
  1. import string
  2. digits = "".join([loop for loop in range(1, 10)])
  3. a = list(string.ascii_letters + digits + "!@#$%^&*()-+={}|\/?><,.'")
Last edited by AutoPython; Nov 7th, 2009 at 3:19 pm.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Nov 7th, 2009
0
Re: Password Generator
Okay, this might even be mildly better:
Python Syntax (Toggle Plain Text)
  1. import string
  2. a = list(string.ascii_lowercase + "!@#$%^&*()-+={}|\/?><,.'") + range(1, 10)
  3.  
  4. print a
For Python3 change to list(range(1, 10)). Oh, the joys of Python!
Last edited by sneekula; Nov 7th, 2009 at 3:53 pm.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: sudoku solver problem
Next Thread in Python Forum Timeline: Cramer's Rule please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC