Skip to content

Commit f4771c2

Browse files
Merge pull request geekcomputers#741 from nikhil-reddy05/patch-1
password manager
2 parents 4b4746f + d2ad9f2 commit f4771c2

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

password_manager.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import sqlite3
2+
from getpass import getpass
3+
import os
4+
5+
# set the environment variable ADMIN_PASS to your desired string, which will be your password.
6+
ADMIN_PASSWORD = os.environ['ADMIN_PASS']
7+
connect = getpass("What is your admin password?\n")
8+
9+
while connect != ADMIN_PASSWORD:
10+
connect = getpass("What is your admin password?\n")
11+
if connect == "q":
12+
break
13+
14+
conn = sqlite3.connect('password_manager.db')
15+
cursor_ = conn.cursor()
16+
17+
18+
def get_password(service_):
19+
command = 'SELECT * from STORE WHERE SERVICE = "' + service_ + '"'
20+
cursor = conn.execute(command)
21+
for row in cursor:
22+
username_ = row[1]
23+
password_ = row[2]
24+
return [username_, password_]
25+
26+
27+
def add_password(service_, username_, password_):
28+
command = 'INSERT INTO STORE (SERVICE,USERNAME,PASSWORD) VALUES("'+service_+'","'+username_+'","'+password_+'");'
29+
conn.execute(command)
30+
conn.commit()
31+
32+
33+
def update_password(service_, password_):
34+
command = 'UPDATE STORE set PASSWORD = "' + password_ + '" where SERVICE = "' + service_ + '"'
35+
conn.execute(command)
36+
conn.commit()
37+
print(service_ + " password updated successfully.")
38+
39+
40+
def delete_service(service_):
41+
command = 'DELETE from STORE where SERVICE = "' + service_ + '"'
42+
conn.execute(command)
43+
conn.commit()
44+
print(service_ + " deleted from the database successfully.")
45+
46+
47+
def get_all():
48+
cursor_.execute("SELECT * from STORE")
49+
data = cursor_.fetchall()
50+
if len(data) == 0:
51+
print('No Data')
52+
else:
53+
for row in data:
54+
print("service = ", row[0])
55+
print("username = ", row[1])
56+
print("password = ", row[2])
57+
print()
58+
59+
60+
def is_service_present(service_):
61+
cursor_.execute("SELECT SERVICE from STORE where SERVICE = ?", (service_,))
62+
data = cursor_.fetchall()
63+
if len(data) == 0:
64+
print('There is no service named %s' % service_)
65+
return False
66+
else:
67+
return True
68+
69+
70+
if connect == ADMIN_PASSWORD:
71+
try:
72+
conn.execute('''CREATE TABLE STORE
73+
(SERVICE TEXT PRIMARY KEY NOT NULL,
74+
USERNAME TEXT NOT NULL,
75+
PASSWORD TEXT NOT NULL);
76+
''')
77+
print("Your safe has been created!\nWhat would you like to store in it today?")
78+
except:
79+
print("You have a safe, what would you like to do today?")
80+
81+
while True:
82+
print("\n" + "*" * 15)
83+
print("Commands:")
84+
print("quit = quit program")
85+
print("get = get username and password")
86+
print("getall = show all the details in the database")
87+
print("store = store username and password")
88+
print("update = update password")
89+
print("delete = delete a service details")
90+
print("*" * 15)
91+
input_ = input(":")
92+
93+
if input_ == "quit":
94+
print("\nGoodbye, have a great day.\n")
95+
conn.close()
96+
break
97+
98+
elif input_ == "store":
99+
service = input("What is the name of the service?\n")
100+
cursor_.execute("SELECT SERVICE from STORE where SERVICE = ?", (service,))
101+
data = cursor_.fetchall()
102+
if len(data) == 0:
103+
username = input("Enter username : ")
104+
password = getpass("Enter password : ")
105+
if username == '' or password == '':
106+
print("Your username or password is empty.")
107+
else:
108+
add_password(service, username, password)
109+
print("\n" + service.capitalize() + " password stored\n")
110+
else:
111+
print("Service named {} already exists.".format(service))
112+
113+
elif input_ == "get":
114+
service = input("What is the name of the service?\n")
115+
flag = is_service_present(service)
116+
if flag:
117+
username, password = get_password(service)
118+
print(service.capitalize() + " Details")
119+
print("Username : ", username)
120+
print("Password : ", password)
121+
122+
elif input_ == "update":
123+
service = input("What is the name of the service?\n")
124+
if service == '':
125+
print('Service is not entered.')
126+
else:
127+
flag = is_service_present(service)
128+
if flag:
129+
password = getpass("Enter new password : ")
130+
update_password(service, password)
131+
132+
elif input_ == "delete":
133+
service = input("What is the name of the service?\n")
134+
if service == '':
135+
print('Service is not entered.')
136+
else:
137+
flag = is_service_present(service)
138+
if flag:
139+
delete_service(service)
140+
141+
elif input_ == "getall":
142+
get_all()
143+
144+
else:
145+
print("Invalid command.")

0 commit comments

Comments
 (0)