Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- import random
- import pickle
- import os
- print "Welcome to custom password manager by Pedro Matias!"
- DATA = "/home/pedro/password.data" #file to store passwords in
- NUMBERS = string.digits #list with numbers from 0 to 9 as strings, used to create passwords
- SIGNS = ("!", ".", ":", ";", ",", "?", "'", "@", "$", "~", "^","%", "#", "&", "/") #punctuation signs that can be used in passwords
- LOWER = string.ascii_lowercase #lowercase letters
- UPPER = string.ascii_uppercase #uppercase letters
- info = {} #dictionary to hold the email and password for a particular service. Key will be service name
- if os.path.isfile(DATA) == False: #checks if file doesn't exist, if so it creates file using open function
- f = open(DATA, "w")
- f.close()
- 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
- f = open(DATA, "wb")
- pickle.dump(info, f)
- f.close()
- def create_pswd():
- caps = raw_input("Do you want caps in your password? Y/N: ")
- length = int(raw_input("How many characters do you want in your password: "))
- if caps[0].lower() == "y":
- chars = NUMBERS + "".join(SIGNS) + LOWER + UPPER
- return "".join(random.sample(chars, length)) #needs to use join because random.sample returns list of chars, not str
- elif caps[0].lower() == "n":
- chars = NUMBERS + "".join(SIGNS) + LOWER
- return "".join(random.sample(chars, length))
- else:
- print "Sorry, that was an invalid answer. Choose Y/N."
- create_pswd()
- def iterate(): #iterates over the info dic, printing the keys
- f = open(DATA, "rb")
- info = pickle.load(f)
- f.close()
- if len(info) > 0:
- print "Here are the services for which there is stored info:"
- for key in info.keys():
- print key
- else:
- print "Sorry, the password manager is empty."
- def main():
- 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: ")
- if action == "a":
- iterate()
- f = open(DATA, "rb")
- info = pickle.load(f)
- f.close()
- if len(info) == 0:
- print "Since the password manager is empty, you can't access the information of any service."
- else:
- service = raw_input("What service's information do you want to access? Type name as it was shown previously: ")
- print info[service][0]
- main()
- elif action == "b":
- f = open(DATA, "rb")
- info = pickle.load(f)
- f.close()
- service = raw_input("What is the name of the service? Enter it here: ")
- email = raw_input("What is the email you're using for this service? If there is none, just press enter. Type it here: ")
- username = raw_input("What is the username you're using for this service? If there is none, just press enter. Type it here: ")
- wantspswd = raw_input("Do you already have a password for this service? Yes or no: ")
- if wantspswd[0].lower() == "y":
- pswd = raw_input("What is the password? Type it here: ")
- elif wantspswd[0].lower() == "n":
- print "Ok, so we'll generate a new one for you."
- pswd = create_pswd()
- if len(email) == 0 and len(username) == 0: #if there is no email or username provided
- info[service] = ["The password for " + service + " is '" + pswd + "' .", email, username]
- elif len(email) == 0 and len(username) > 0: #if there is a username but no email
- info[service] = ["The password for " + service + "is '" + pswd + "' and the username is '" + username + "' .", email, username]
- elif len(username) == 0 and len(email) > 0: #if there is an email but no username
- info[service] = ["The password for " + service + " is '" + pswd + "' and the email is '" + email + "' .", email, username]
- else:
- info[service] = ["The password for " + service + " is '" + pswd + "', the email is '" + email + "' and the username is '" + username + "' .", email, username]
- f = open(DATA, "wb")
- pickle.dump(info, f)
- f.close()
- main()
- elif action == "c":
- iterate()
- f = open(DATA, "rb")
- info = pickle.load(f)
- f.close()
- if len(info) > 0:
- service = raw_input("Which service? Enter it here: ")
- newpswd = raw_input("What is the new password: ")
- if len(info[service][1]) > 0 and len(info[service][2]) == 0: #if there is an email but no username
- info[service][0] = "The password for " + service + " is '" + newpswd + "' and the email is '" + info[service][1] + "' ."
- elif len(info[service][1]) == 0 and len(info[service][2]) == 0: #if there is no email nor username
- info[service][0] = "The password for " + service + " is '" + newpswd + "' ."
- elif len(info[service][1]) > 0 and len(info[service][2]) > 0: #if there is email and username
- info[service][0] = "The password for " + service + " is '" + newpswd + "', the email is '" + info[service][1] + "' and the username is + '" + info[service][2] + "' ."
- else: #if there is username but no email
- info[service][0] = "The password for " + service + "is '" + pswd + "' and the username is '" + info[service][2] + "' ."
- f = open(DATA, "wb")
- pickle.dump(info, f)
- f.close()
- else:
- print "Since the password manager is empty, there is no information to change."
- main()
- elif action == "d":
- iterate()
- f = open(DATA, "rb")
- info = pickle.load(f)
- f.close()
- if len(info) > 0:
- service = raw_input("Which service? Enter it here: ")
- del info[service]
- f = open(DATA, "wb")
- pickle.dump(info, f)
- f.close()
- else:
- print "Since the password manager is empty, there is nothing to delete."
- main()
- elif action == "e":
- iterate()
- main()
- else:
- print "Sorry, wrong option"
- main()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement