-
Notifications
You must be signed in to change notification settings - Fork 0
/
save-password.py
186 lines (161 loc) · 6.25 KB
/
save-password.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
import json
import os
import random
from cryptography.fernet import Fernet
import argparse, sys
import string
fpath = __file__
absolute_path = os.path.abspath(fpath)
base_dir = os.path.dirname(absolute_path)
epicPath = base_dir + '/passwords/'
def write_key():
keyPath = base_dir + '/special/'
file_name = keyPath + 'spec.key'
key = Fernet.generate_key()
if not os.path.exists(keyPath):
os.mkdir(keyPath)
with open(file_name, "wb") as key_file:
key_file.write(key)
keyPath = base_dir + '/special/'
keyname = keyPath + 'spec.key'
passlockname = keyPath + 'lock.pws'
def name():
if os.path.exists(passlockname):
print("Pass lock found :)")
else:
print("Pass lock not found :(")
plockinput = input("Enter decryption code (example: o27n) : ")
if not os.path.exists(keyPath):
os.mkdir(keyPath)
with open(passlockname, "w") as key_file:
key_file.write(plockinput)
key_file.close()
if not os.path.exists(keyname):
write_key()
key = load_key()
encrypt(passlockname, key)
tname = input("Enter name of password: ")
if os.path.exists(epicPath + tname + '.pwsaver'):
print("Name is taken!")
name()
else:
cont(tname)
def cont(name):
email = input("Enter username/email: ")
passphrase = input("Enter password: ")
x = {"name": name, "email": email, "passphrase": passphrase}
y = json.dumps(x)
generate(y, name)
def encrypt(filename, key):
f = Fernet(key)
with open(filename, "rb") as file:
# read all file data
file_data = file.read()
file.close()
# encrypt data
encrypted_data = f.encrypt(file_data)
# write the encrypted file
with open(filename, "wb") as file:
file.write(encrypted_data)
file.close()
print("Saved and encrypted.")
def load_key():
fpath = __file__
absolute_path = os.path.abspath(fpath)
base_dir = os.path.dirname(absolute_path)
epicPath = base_dir + '/special/'
file_name = epicPath + 'spec.key'
return open(file_name, "rb").read()
def save(load, full_path):
folder = base_dir + '/passwords/'
if not os.path.exists(folder):
os.mkdir(folder)
with open(full_path, "w") as key_file:
key_file.write(load)
key_file.close()
key = load_key()
encrypt(full_path, key)
def generate(y, name):
full_path = epicPath + name + '.pwsaver'
save(y, full_path)
parser = argparse.ArgumentParser()
parser.add_argument("-S", "--save", help="save a password", action="store_true")
parser.add_argument("-E", "--email", help="email/user to save")
parser.add_argument("-N", "--name", help="name of password to save")
parser.add_argument("-P", "--password", help="password to save")
parser.add_argument("-R", "--random", help="generates a random password")
args = parser.parse_args()
password_characters = string.ascii_letters + string.digits + string.punctuation
password = []
if args.save:
if args.name:
if args.email:
if args.password:
if os.path.exists(passlockname):
print("Pass lock found :)")
else:
print("Pass lock not found :(")
plockinput = input("Enter decryption code (example: o27n) : ")
if not os.path.exists(keyPath):
os.mkdir(keyPath)
with open(passlockname, "w") as key_file:
key_file.write(plockinput)
key_file.close()
if not os.path.exists(keyname):
write_key()
key = load_key()
encrypt(passlockname, key)
if not os.path.exists(keyname):
write_key()
print("=" * 30, "Key Generator", "=" * 30)
print(
"I have generated your SPECIAL key needed to DECRYPT your keys. Do NOT delete this, or your passwords will be lost!")
print("=" * 75)
if os.path.exists(epicPath + args.name + '.pwsaver'):
print("Name is taken!")
sys.exit()
else:
full_path = epicPath + args.name + '.pwsaver'
x = {"name": args.name, "email": args.email, "passphrase": args.password}
j = json.dumps(x)
save(j, full_path)
else:
if args.random:
if os.path.exists(passlockname):
print("Pass lock found :)")
else:
print("Pass lock not found :(")
plockinput = input("Enter decryption code (example: o27n) : ")
if not os.path.exists(keyPath):
os.mkdir(keyPath)
with open(passlockname, "w") as key_file:
key_file.write(plockinput)
key_file.close()
if not os.path.exists(keyname):
write_key()
key = load_key()
encrypt(passlockname, key)
if not os.path.exists(keyname):
write_key()
print("=" * 30, "Key Generator", "=" * 30)
print(
"I have generated your SPECIAL key needed to DECRYPT your keys. Do NOT delete this, or your passwords will be lost!")
print("=" * 75)
if os.path.exists(epicPath + args.name + '.pwsaver'):
print("Name is taken!")
sys.exit()
else:
full_path = epicPath + args.name + '.pwsaver'
for x in range(args.random):
password.append(random.choice(password_characters))
x = {"name": args.name, "email": args.email, "passphrase": ''.join(password)}
j = json.dumps(x)
save(j, full_path)
else:
name()
else:
name()
else:
name()
else:
name()