-
Notifications
You must be signed in to change notification settings - Fork 0
/
sv.py
195 lines (151 loc) · 7.15 KB
/
sv.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
#!/usr/bin/python3
# SecureVault 1.0
# Author: WhiteHack
from secrets import choice
from sys import argv
from os import chmod, path, mkdir, remove, listdir
from cryptography.fernet import Fernet
from hashlib import sha3_512
from getpass import getpass, getuser
class SecureVault:
def __init__(self):
self.symbols_and_numbers = ["@", "1", "/", "*", "8", "_", "6", "0", "'", "2", '"', "\\", "+", "9", "&", "3", "-", ";", "4", "!", "?", "5", "#", "$", "7"]
self.alpha = [ "m", "t", "u", "v", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "o", "p", "q", "r", "s", "w", "x", "y", "z"]
self.characters = self.symbols_and_numbers + self.alpha
self.key_length = choice([16, 17, 18, 19, 20, 21, 22, 23])
self.fernet_key = Fernet.generate_key()
self.fernet = Fernet(self.fernet_key)
self.user = getuser()
self.version_info = "SecureVault 1.0. It is a tool that allows you to generate secure keys."
def generate_key(self):
self.generated_key = ""
for _ in range(self.key_length):
char = choice(self.characters)
if char not in self.symbols_and_numbers and choice(range(10)) in [0, 3, 4, 6, 7]:
self.generated_key += char.upper()
else:
self.generated_key += char
return self.generated_key
def read_key(self):
for _ in range(2):
key_name = input("Enter the name of your password: ").strip()
key_path = f"/home/{self.user}/KeySafe/.VaultSecret/{key_name}"
with open(f"/home/{self.user}/KeySafe/.VaultSecret/.key", 'r') as key_file:
stored_hash = key_file.read()
user_password = getpass("Enter your password: ").strip()
hashed_password = sha3_512(user_password.encode()).hexdigest()
if stored_hash == hashed_password:
with open(key_path, 'rb') as key_file:
encrypted_key = key_file.read()
fernet = Fernet(user_password.encode())
user_password = ""
decrypted_key = fernet.decrypt(encrypted_key)
fernet = ""
print(f"Your password is => {decrypted_key.decode()}")
decrypted_key = ""
break
else:
print("Incorrect password!")
def store_unique_key(self):
key_path = f"/home/{self.user}/KeySafe/.VaultSecret/.key"
if not path.isfile(key_path):
with open(key_path, 'w') as key_file:
hashed_key = sha3_512(self.fernet_key).hexdigest()
key_file.write(hashed_key)
chmod(key_path, 0o600)
print(f"Your password is => {self.fernet_key.decode()}")
self.fernet_key = ""
else:
print("The password already exists!")
def save_key(self):
confirm = input("Would you like to save the password (y/n): ").strip().lower()
if confirm == "y":
for _ in range(2):
key_name = input("Enter the name of the file that will store your password: ").strip()
key_path = f"/home/{self.user}/KeySafe/.VaultSecret/{key_name}"
if not path.isfile(key_path):
with open(f"/home/{self.user}/KeySafe/.VaultSecret/.key", 'r') as key_file:
stored_hash = key_file.read()
user_password = getpass("Enter your password: ").strip()
hashed_password = sha3_512(user_password.encode()).hexdigest()
if stored_hash == hashed_password:
with open(key_path, 'wb') as key_file:
fernet = Fernet(user_password.encode())
user_password = ""
encrypted_key = fernet.encrypt(self.generated_key.encode())
self.generated_key = ""
fernet = ""
key_file.write(encrypted_key)
chmod(key_path, 0o600)
print("Your password has been saved successfully!")
break
else:
print("Incorrect password!")
else:
print("Password name already exists!")
def list_password(self):
self.listen = listdir(f"/home/{self.user}/KeySafe/.VaultSecret")
for x in self.listen:
if x != ".key":
print(x)
def delete(self):
for _ in range(2):
key_name = input("Enter the name of your password: ").strip()
key_path = f"/home/{self.user}/KeySafe/.VaultSecret/{key_name}"
with open(f"/home/{self.user}/KeySafe/.VaultSecret/.key", 'r') as key_file:
stored_hash = key_file.read()
user_password = getpass("Enter your password: ").strip()
hashed_password = sha3_512(user_password.encode()).hexdigest()
user_password = ""
if stored_hash == hashed_password:
remove(key_path)
print("Your password has been successfully deleted!")
break
else:
print("Incorrect password!")
def main(self):
try:
chmod(f"/home/{self.user}/KeySafe/sv.py", 0o700)
secret_dir = f"/home/{self.user}/KeySafe/.VaultSecret"
if not path.isdir(secret_dir):
mkdir(secret_dir)
chmod(secret_dir, 0o700)
if "-g" in argv:
print(f"Key-Safe => {self.generate_key()}")
self.save_key()
elif "-V" in argv:
print(self.version_info)
elif "-u" in argv:
self.store_unique_key()
elif "-r" in argv:
self.read_key()
elif "-d" in argv:
self.delete()
elif "-l" in argv:
self.list_password()
elif "-h" in argv or "--help" in argv:
print("""
SecureVault 1.0. It is a tool that allows you to generate secure keys.
Usage:
python3 sv.py -g generate a secure key
python3 sv.py -V print version info and exit
python3 sv.py -r read a stored password by its custom name
python3 sv.py -u generate a unique key
python3 sv.py -d delete secure key
python3 sv.py -l list your stored passwords
Help Menu:
-h --help print this help message and exit
""")
else:
print("SecureVault: invalid arguments. Use -g to generate a secure key. Try --help for more information.")
except (KeyboardInterrupt,EOFError):
print()
except FileNotFoundError as e:
print(f"Path or file does not exist => {e}")
except PermissionError as p:
print(f"Permissions error on the file or directory => {p}")
except:
print("Invalid or corrupt password!")
if __name__ == "__main__":
vault = SecureVault()
vault.main()