-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_functionality.py
More file actions
145 lines (126 loc) · 5.55 KB
/
Copy pathuser_functionality.py
File metadata and controls
145 lines (126 loc) · 5.55 KB
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
import json, os, sys
from encryption import encrypt_password, decrypt_password
from hash import hash_password
# Function to register you.
def register(username, master_password):
# Encrypt the master password before storing it
hashed_master_password = hash_password(master_password)
# Prepare user data for storage in dict
user_data = {'username': username, 'master_password': hashed_master_password}
# Define the path to the 'data' folder and the user data filename
data_folder = 'data'
file_name = 'user_data.json'
user_data_file_path = os.path.join(data_folder, file_name)
# Ensure the 'data' folder exists
if not os.path.exists(data_folder):
os.makedirs(data_folder)
# Check if file exists and is empty
if os.path.exists(user_data_file_path) and os.path.getsize(user_data_file_path) == 0:
# Open and write to file
with open(user_data_file_path, 'w') as file:
# Write user_data to dict in json format
json.dump(user_data, file)
# Registration Complete Message
print("\n[+] Registration complete!!\n")
# File does not exist, create a new one
else:
with open(user_data_file_path, 'w') as file: # Changed from 'x' to 'w' to overwrite if necessary
# Write user_data to dict in json format
json.dump(user_data, file)
# Registration Complete Message
print("\n[+] Registration complete!!\n")
# Function to log you in.
def login(username, entered_password):
try:
# Read user_data.json
with open('data/user_data.json', 'r') as file:
# Load json data from file into user_data dict
user_data = json.load(file)
# Retrieve stored password hash
stored_password_hash = user_data.get('master_password')
# hash entered_password
entered_password_hash = hash_password(entered_password)
# Check password hashes match and username is correct
if entered_password_hash == stored_password_hash and username == user_data.get('username'):
# Successful login message
print("\n[+] Login Successful..")
# Failed Login
else:
print("\n[-] Invalid Login credentials. Please use the credentials you used to register.\n")
sys.exit()
# Not registered (exit)
except Exception:
print("\n[-] You have not registered. Please do that.\n")
sys.exit()
# Function to view saved websites.
def view_websites():
try:
data_folder = 'data'
file_name = 'passwords.json'
password_file_path = os.path.join(data_folder, file_name)
# Open Passwords file in read mode
with open(password_file_path, 'r') as data:
# Set view to parsed json data
view = json.load(data)
print("\n-----------------------------------------------")
print("| Saved Websites |")
print("-----------------------------------------------\n")
# Interate through websites
for x in view:
print(x['website'])
print('\n')
# No passwords saved
except FileNotFoundError:
print("\n[-] You have not saved any passwords!\n")
# Function to add (save password).
def add_password(cipher, website, password):
# Define the path to the 'data' folder and the passwords file
data_folder = 'data'
passwords_file = 'passwords.json'
passwords_file_path = os.path.join(data_folder, passwords_file)
# Ensure the 'data' folder exists
if not os.path.exists(data_folder):
os.makedirs(data_folder)
# Check if passwords.json exists
if not os.path.exists(passwords_file_path):
# If passwords.json doesn't exist, initialize it with an empty list
data = []
else:
# Load existing data from passwords.json
try:
with open(passwords_file_path, 'r') as file:
data = json.load(file)
except json.JSONDecodeError:
# Handle the case where passwords.json is empty or invalid JSON
data = []
# Encrypt the password using the provided cipher
encrypted_password = encrypt_password(cipher, password)
# Create a dictionary to store the website and password
password_entry = {'website': website, 'password': encrypted_password}
data.append(password_entry)
# Save the updated list back to passwords.json in the 'data' folder
with open(passwords_file_path, 'w') as file:
json.dump(data, file, indent=4)
# Function to retrieve a saved password.
def get_password(website, cipher):
data_folder = 'data'
passwords_file = 'passwords.json'
passwords_file_path = os.path.join(data_folder, passwords_file)
# Check if passwords.json exists
if not os.path.exists(passwords_file_path):
return None
# Load existing data from passwords.json
try:
# Open in read mode and set to data
with open(passwords_file_path, 'r') as file:
data = json.load(file)
# Error
except json.JSONDecodeError:
data = []
# Loop through all the websites and check if the requested website exists.
for entry in data:
if entry['website'] == website:
# Decrypt and return the password
decrypted_password = decrypt_password(cipher, entry['password'])
return decrypted_password
return None