-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·298 lines (256 loc) · 10.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python3
"""
Project Name: PassMe (Copyright 2023)
Description: PassMe is Password Manager Tool use to store strong passwords for any applications.
Author: Aniket Chavan
Date: 30 Oct 2023
"""
# Modules
import os, getpass, argparse, hashlib
from string import ascii_lowercase, ascii_uppercase, digits, punctuation
from secrets import choice
from module.dbconfig import *
## alphabets, digits, meta characters(special characters)
data = ascii_lowercase + ascii_uppercase + digits + punctuation
def generate_salt() -> bytes:
"""
Generate a random 16-byte salt.
"""
salt = os.urandom(16)
return salt
def generate_random_password(length=16) -> str:
"""
Randomly Generate password with default length 16
includes uppercase, lowercase, integers, and metacharacters(special characters).
:param: length: length of password default set to 16.
"""
try:
length = input("Enter the length of Password (default is 16): ")
if not length or length == 'd' or length == 'default' or length == 'Default' or length == 'DEFAULT':
length = int(16)
elif int(length) < 4 or int(length) >= 32:
print("Please enter length between 4 - 32 \nDefault length = 16")
length = int(16)
else:
length = int(length)
password = ''.join(choice(data) for _ in range(length))
return password
except ValueError:
print("Invalid input. Please enter a valid integer for the password length.")
return None
except Exception as e:
print(f"Error: {e}")
def hashed_passwd(password, salt) -> bytes:
"""
This function use to hash and salting password.
"""
context = password.encode('utf-8')
salted_password = salt + context
sha512 = hashlib.sha512()
sha512.update(salted_password)
password = sha512.hexdigest()
return password
def create_passwd(website: str, password: str, user_specific_key: bytes):
"""
Store password with app name and encrypted password using fernet key.
"""
store_password(website, password, user_specific_key)
print("Password Stored Successfully!")
def show_passwd(website: str, user_specific_key: bytes):
"""
retrieve password from database and show password
if not then show warning password doesn't exists.
"""
data = retrieve_password(website, user_specific_key)
if data:
print("Stored Password:", data)
else:
print("Password not found.")
def handle_create_password(username: str, user_specific_key: bytes):
"""
create or update existing password or new custom password or new generated password for user.
"""
create_database()
website = input("Enter website, username, app name: ")
if check_duplicate_password(website, user_specific_key):
print(f"Password for '{website}' already exists. Do you want to update it? (y/n/c)")
print("Choose an option for the password:")
print(("1. Update with Generated Password: PRESS y"))
print(("2. NO: PRESS n"))
print(("3. Update with Custom Password: PRESS c"))
choice = input()
if choice.lower() == 'y':
password = generate_random_password()
update_password(website, password, user_specific_key)
print(f"Generated Password: {password}")
elif choice.lower() == 'c':
custom_password = input("Enter your custom password: ")
update_password(website, custom_password, user_specific_key)
print(f"Custom Password updated for '{website}'.")
else:
print("Password not updated.")
else:
print("Choose an option for the password:")
print("1. Generate a random password")
print("2. Use a custom password")
choice = input()
if choice == '1':
generated_password = generate_random_password()
store_password(website, generated_password, user_specific_key)
print(f"Generated Password: {generated_password}")
print(f"Generated Password for '{website}' stored successfully.")
elif choice == '2':
custom_password = input("Enter your custom password: ")
store_password(website, custom_password, user_specific_key)
print(f"Custom Password for '{website}' stored successfully.")
else:
print("Password not stored.")
def handle_show_password(username: str, user_specific_key: bytes):
"""
verify user, show user their passwords
"""
if username:
user_is_authenticated = login(username)
if user_is_authenticated:
website = input("Enter website, username, app name: ")
show_passwd(website, fernet_key)
else:
print("Quiting...")
def create_user(username: str, user_specific_key: bytes):
"""
Create new user account, checks user already exists, verify master password, and store salted and hashed password.
"""
create_database()
if not username:
username = input("Create a new username: ")
if check_duplicate_username(username):
print(f"User '{username}' already exists. Account creation not allowed.")
return
# Generate a unique Fernet key for each user
user_specific_key = generate_new_key(username)
master_password = getpass.getpass("Create a MASTER password: ")
verify_master_password = getpass.getpass("Confirm again: ")
if master_password != verify_master_password:
print("Password doesn't match")
print("Account creation not allowed.!!!")
else:
salt = os.urandom(16) # Generate a random salt
hashed_password = hashed_passwd(master_password, salt)
# Store the user-specific Fernet key along with other account details
store_user_in_database(username, hashed_password, salt)
print("Account created successfully.")
def verify_user(username: str, master_password: str) -> bool:
"""
Check salted and hashed password with user entered password for verification.
"""
password_hash, salt = retrieve_user_info(username)
if password_hash is None or salt is None:
# Handle the case where the user doesn't exist or the database is empty
print("User not found or database is empty. Please create a user account.")
return False
salted_password = hashed_passwd(master_password, salt)
return password_hash == salted_password
def login(username: str) -> bool:
"""
Login with username and verify master password
"""
# username = input("Enter your username: ")
master_password = getpass.getpass("Enter your MASTER password: ")
user_is_authenticated = verify_user(username, master_password)
if user_is_authenticated == False:
print("Quiting...")
return user_is_authenticated
def parse_arguments():
parser = argparse.ArgumentParser(description="Password Management Tool")
parser.add_argument("-g", "--generate", action="store_true", help="Generate a random password")
parser.add_argument("-u", "--username", metavar="username", help="Specify the username for actions")
parser.add_argument("-cs", "--create-account", action="store_true", help="Create a new account")
parser.add_argument("-c", "--create", action="store_true", help="Create a new password entry")
parser.add_argument("-s", "--show", action="store_true", help="Show a stored password")
parser.add_argument("-k", "--gen-key", "-generate-key", action="store_true", help="Generate a new Fernet key")
parser.add_argument("-i", "--import-key", metavar="filename", help="Import a Fernet key from a file")
parser.add_argument("-e", "--export-key", metavar="filename", help="Export the Fernet key to a file")
parser.add_argument("-r", "--rotate-key", action="store_true", help="Rotate the encryption key")
return parser.parse_args(), parser
def handle_arguments(args, parser):
if args.create_account:
username = args.username
user_specific_key = get_key_from_database(username)
if user_specific_key is None:
user_specific_key = key(username)
create_user(username, user_specific_key)
elif args.generate:
password = generate_random_password()
print("Generated Password:", password)
elif args.create:
username = args.username
if not username:
print("Username is required. Please provide a username.\nUse -u or --username flag to pass username.")
return
user_is_authenticated = login(username)
if user_is_authenticated:
print("User Authenticated")
user_specific_key = get_key_from_database(username)
handle_create_password(username, user_specific_key)
elif args.show:
username = args.username
if not username:
print("Username is required. Please provide a username.\nUse -u or --username flag to pass username.")
return
user_is_authenticated = login(username)
if user_is_authenticated:
website = input("Enter website, username, app name: ")
user_specific_key = key(username)
show_passwd(website, user_specific_key)
elif args.gen_key:
username = args.username
if username:
user_specific_key = generate_new_key(username)
print("Successfully Generated New Key")
print("Please Login Again")
print("Quiting...")
else:
print("Please pass Username using -u or --username.")
elif args.import_key:
username = args.username
if username:
user_is_authenticated = login(username)
if user_is_authenticated:
print("User Authenticated")
filename = args.import_key
user_specific_key = key(username)
user_specific_key = import_key_from_file(username, filename, user_specific_key)
print("Key imported successfully.")
elif args.export_key:
username = args.username
filename = args.export_key
if username:
user_is_authenticated = login(username)
if user_is_authenticated:
print("User Authenticated")
user_specific_key = key(username)
export_key_to_file(user_specific_key, filename)
print("Key exported successfully.")
else:
print("Please provide with username!")
elif args.rotate_key:
username = args.username
if username:
rotate_key(username)
else:
print("Please provide with username!")
else:
parser.print_help()
# main() function:
def main():
# Create a database file if it doesn't exist
create_database()
try:
args, parser = parse_arguments()
handle_arguments(args, parser)
except KeyboardInterrupt:
print("\nOperation was interrupted by the user.\nQuiting...")
except PasswordDecryptionError as e:
print(f"Password decryption error: {e}")
if __name__ == '__main__':
main()