-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththevault.py
160 lines (115 loc) · 4.88 KB
/
thevault.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
import os
from util.passgen import Vault
from helpers import validate_input
def new_vault() -> Vault:
"""
Generate a new vault.
:return:
"""
print("No existing vault detected.")
print("To create a new vault, create a password below.")
vault_key = validate_input("Password for new vault: ", lambda x: 8 <= len(x) <= 32 and x.isprintable(), err_text="It needs to be at least 8 characters, max 32. (no weird characters).\n")
# Make a new vault object.
vault = Vault(vault_key.encode("utf-8"))
# Remove vault key from memory for security.
del vault_key
return vault
def existing_vault() -> Vault:
"""
Open an existing vault.
:return:
"""
print("Found an existing vault.")
print("Please input the password for that vault.")
vault_key = validate_input(": ", lambda x: 8 <= len(x) <= 32 and x.isprintable(), err_text="Has to be at least 8 characters long, max 32. No weird characters.\n")
# Key is provided for existing vault. Try to open it.
vault = Vault(vault_key.encode("utf-8"))
try:
vault.unlock_vault()
vault.lock_vault()
except ValueError:
print("\nInvalid vault key provided.")
exit(0)
# Remove vault key from memory for security.
del vault_key
return vault
def get_vault() -> Vault:
"""
Try to retrieve an existing vault.
:return:
"""
if os.path.isfile("vault.edb"):
return existing_vault()
else:
return new_vault()
def main():
print("Welcome to the Password Vault!")
# Gather input.
vault = get_vault()
print()
print("\nLogged into the vault.")
# Run the menu.
menu = True
while menu:
print("\nPlease choose what you want to do.")
print("1: Store a new password.")
print("2: Generate a new password.")
print("3: Retrieve a password.")
print("4: Delete a password.")
print("5: Show the list of saved services.\n")
print("0: Exit and lock the vault.\n")
option = int(validate_input("> ", lambda x: 0 <= int(x) <= 5))
# Exit.
if option == 0:
print("\nLocking vault...")
vault.lock_vault()
print("Thanks for using EyeDevelop's Vault!")
menu = False
# Store a password.
elif option == 1:
service = validate_input("Please enter the service name: ", lambda x: x.isprintable() and len(x) > 0)
password = validate_input("Please enter the password: ", lambda x: x.isprintable() and len(x) > 0)
vault.store_password(service, password)
print("Saved!")
# Generate a password.
elif option == 2:
service = validate_input("Please enter the service name: ", lambda x: x.isprintable() and len(x) > 0)
length = int(validate_input("Enter the length of the password: ", lambda x: x.isnumeric()))
print("\n\nIt's time to choose complexity.")
print("Complexity is built of three numbers. The letter complexity, number complexity and symbol complexity.")
print("\nFirst up is letter complexity.")
print("0 = No letters, 1 = Only lowercase, 2 = Only uppercase, 3 = Mixed case.")
letter_complexity = validate_input("> ", lambda x: 0 <= int(x) <= 3)
print("\nNow the number complexity.")
print("0 = No digits, 1 = Use digits.")
digit_complexity = validate_input("> ", lambda x: 0 <= int(x) <= 1)
print("\nFinally, the symbol complexity.")
print("0 = No symbols, 1 = Use symbols.")
symbol_complexity = validate_input("> ", lambda x: 0 <= int(x) <= 1)
complexity = letter_complexity + digit_complexity + symbol_complexity
password = vault.generate_password(service, length, complexity)
print("Your generated password:", password)
print("\nAlso stored in vault.")
# Retrieve password for service.
elif option == 3:
service = validate_input("Please enter the service name: ", lambda x: x.isprintable() and len(x) > 0)
if service in vault.get_services():
print("Your password for {}: {}".format(service, vault.get_password(service)))
else:
print("That service is not stored in the vault.")
# Delete service.
elif option == 4:
service = validate_input("Please enter the service name: ", lambda x: x.isprintable() and len(x) > 0)
vault.delete_service(service)
print("Deleted {}.".format(service))
# Print services.
elif option == 5:
services = vault.get_services()
print("Your services ({}):\n".format(len(services)))
for service in services:
print(service)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit(0)