Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.67 KB | None | 0 0
  1. import string
  2. import random
  3. import pickle
  4. import os
  5.  
  6. print "Welcome to custom password manager by Pedro Matias!"
  7. DATA = "/home/pedro/password.data" #file to store passwords in
  8. NUMBERS = string.digits #list with numbers from 0 to 9 as strings, used to create passwords
  9. SIGNS = ("!", ".", ":", ";", ",", "?", "'", "@", "$", "~", "^","%", "#", "&", "/") #punctuation signs that can be used in passwords
  10. LOWER = string.ascii_lowercase #lowercase letters
  11. UPPER = string.ascii_uppercase #uppercase letters
  12. info = {} #dictionary to hold the email and password for a particular service. Key will be service name
  13.  
  14. if os.path.isfile(DATA) == False: #checks if file doesn't exist, if so it creates file using open function
  15.     f = open(DATA, "w")
  16.     f.close()
  17.    
  18. if os.stat(DATA).st_size == False: #checks if file is completely empty, if so drops empty info dic in it because pickle goes EOF with empty file
  19.     f = open(DATA, "wb")
  20.     pickle.dump(info, f)
  21.     f.close()
  22.        
  23. def create_pswd():
  24.     caps = raw_input("Do you want caps in your password? Y/N: ")
  25.     length = int(raw_input("How many characters do you want in your password: "))
  26.     if caps[0].lower() == "y":
  27.         chars = NUMBERS + "".join(SIGNS) + LOWER + UPPER
  28.         return "".join(random.sample(chars, length)) #needs to use join because random.sample returns list of chars, not str
  29.     elif caps[0].lower() == "n":
  30.         chars = NUMBERS + "".join(SIGNS) + LOWER
  31.         return "".join(random.sample(chars, length))
  32.     else:
  33.         print "Sorry, that was an invalid answer. Choose Y/N."
  34.         create_pswd()
  35.  
  36. def iterate(): #iterates over the info dic, printing the keys
  37.     f = open(DATA, "rb")
  38.     info = pickle.load(f)
  39.     f.close()
  40.     if len(info) > 0:
  41.         print "Here are the services for which there is stored info:"
  42.         for key in info.keys():
  43.             print key
  44.     else:
  45.         print "Sorry, the password manager is empty."            
  46.        
  47. def main():
  48.     action = raw_input("What do you want to do? Type 'a' to read an existing service password and email, type 'b' to generate information for a service, type 'c' to change a password, type 'd' to delete the information for a service and type 'e' to show all the saved services. Type: ")
  49.    
  50.     if action == "a":
  51.         iterate()
  52.         f = open(DATA, "rb")
  53.         info = pickle.load(f)
  54.         f.close()
  55.         if len(info) == 0:
  56.             print "Since the password manager is empty, you can't access the information of any service."
  57.         else:
  58.             service = raw_input("What service's information do you want to access? Type name as it was shown previously: ")
  59.             print info[service][0]    
  60.         main()    
  61.    
  62.     elif action == "b":
  63.         f = open(DATA, "rb")
  64.         info = pickle.load(f)
  65.         f.close()
  66.         service = raw_input("What is the name of the service? Enter it here: ")
  67.         email = raw_input("What is the email you're using for this service? If there is none, just press enter. Type it here: ")
  68.         username = raw_input("What is the username you're using for this service? If there is none, just press enter. Type it here: ")
  69.         wantspswd = raw_input("Do you already have a password for this service? Yes or no: ")
  70.        
  71.        
  72.         if wantspswd[0].lower() == "y":
  73.             pswd = raw_input("What is the password? Type it here: ")
  74.        
  75.         elif wantspswd[0].lower() == "n":
  76.             print "Ok, so we'll generate a new one for you."
  77.             pswd = create_pswd()
  78.        
  79.        
  80.         if len(email) == 0 and len(username) == 0: #if there is no email or username provided
  81.             info[service] = ["The password for " + service + " is '" + pswd + "' .", email, username]          
  82.        
  83.         elif len(email) == 0 and len(username) > 0: #if there is a username but no email
  84.             info[service] = ["The password for " + service + "is '" + pswd + "' and the username is '" + username + "' .", email, username]
  85.        
  86.         elif len(username) == 0 and len(email) > 0: #if there is an email but no username
  87.             info[service] = ["The password for " + service + " is '" + pswd + "' and the email is '" + email + "' .", email, username]
  88.        
  89.         else:
  90.             info[service] = ["The password for " + service + " is '" + pswd + "', the email is '" + email + "' and the username is '" + username + "' .", email, username]
  91.        
  92.         f = open(DATA, "wb")
  93.         pickle.dump(info, f)
  94.         f.close()
  95.         main()
  96.        
  97.     elif action == "c":
  98.         iterate()
  99.         f = open(DATA, "rb")
  100.         info = pickle.load(f)
  101.         f.close()
  102.         if len(info) > 0:
  103.             service = raw_input("Which service? Enter it here: ")
  104.             newpswd = raw_input("What is the new password: ")
  105.            
  106.             if len(info[service][1]) > 0 and len(info[service][2]) == 0: #if there is an email but no username
  107.                 info[service][0] = "The password for " + service + " is '" + newpswd + "' and the email is '" + info[service][1] + "' ."
  108.            
  109.             elif len(info[service][1]) == 0 and len(info[service][2]) == 0: #if there is no email nor username
  110.                 info[service][0] = "The password for " + service + " is '" + newpswd + "' ."
  111.            
  112.             elif len(info[service][1]) > 0 and len(info[service][2]) > 0: #if there is email and username
  113.                 info[service][0] = "The password for " + service + " is '" + newpswd + "', the email is '" + info[service][1] + "' and the username is + '" + info[service][2] + "' ."
  114.            
  115.             else: #if there is username but no email
  116.                 info[service][0] = "The password for " + service + "is '" + pswd + "' and the username is '" + info[service][2] + "' ."
  117.            
  118.            
  119.             f = open(DATA, "wb")
  120.             pickle.dump(info, f)
  121.             f.close()
  122.        
  123.         else:
  124.             print "Since the password manager is empty, there is no information to change."
  125.        
  126.         main()
  127.    
  128.     elif action == "d":
  129.         iterate()
  130.         f = open(DATA, "rb")
  131.         info = pickle.load(f)
  132.         f.close()
  133.         if len(info) > 0:
  134.             service = raw_input("Which service? Enter it here: ")
  135.             del info[service]
  136.             f = open(DATA, "wb")
  137.             pickle.dump(info, f)
  138.             f.close()
  139.         else:
  140.             print "Since the password manager is empty, there is nothing to delete."
  141.         main()
  142.    
  143.     elif action == "e":
  144.         iterate()
  145.         main()
  146.    
  147.     else:
  148.         print "Sorry, wrong option"
  149.         main()        
  150.            
  151. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement