-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassi.py
197 lines (166 loc) · 5.85 KB
/
passi.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
from polyalphabetics import viginere
import polyalphabetics as p
import random
from Tkinter import Tk
import os
def addToClipBoard(text):
command = 'echo ' + text.strip() + '| clip'
os.system(command)
class Core:
alphabet = "".join([chr(i) for i in range(0, 255)])
def __init__(self, db_file, master_password):
self.master_password = master_password
self.db_file = db_file
def load_db(self):
try:
inp = open(self.db_file, "r")
data = inp.read()
inp.close()
except:
self.rows = {}
return None
rows = {}
data = self.decrypt(data, self.master_password)
for line in data.split("\n"):
if len(line)>0:
key, value = line.split("|")
rows[key] = value
self.rows = rows
return rows
@classmethod
def decrypt(self, data, password):
return viginere(data, password, alphabet=self.alphabet, enc=False)
@classmethod
def encrypt(self, data, password):
return viginere(data, password, alphabet=self.alphabet, enc=True)
def generate_password(self):
n = 100
s = ""
while n>0:
s += str(random.randint(0, 9))
n -= 1
return s
def get_all_keys(self):
return self.rows.keys()
def get_password(self, key):
return self.rows[key]
def copy_password_to_clipboard(self, key):
addToClipBoard(self.rows[key])
def create_password(self, name):
pwd = self.generate_password()
assert name not in self.rows.keys()
assert name.count("|") == 0
self.rows[name] = pwd
self.save()
return name, pwd
def remove_password(self, name):
assert name in self.rows.keys()
del self.rows[name]
self.save()
def save(self):
text = ""
for key, value in self.rows.items():
text += "%s|%s\n" % (key, value)
text = self.encrypt(text, self.master_password)
with open(self.db_file, "w") as inp:
inp.write(text)
class Terminal:
version = "0.1.0"
name = "Passi - Password Manager"
current_algorithm = "Vigenere"
commands = {
"UNLOCK": ["open db"],
"LOCK": ["close db"],
"INIT": ["start new db"],
"GEN": ["add pwd"],
"GET": ["get pwd"],
"ALL": ["get all names"],
"REM": ["remove pwd"],
"COPY": ["copy pwd to clipboard"],
"HELP": ["display all commands"],
"QUIT": ["exit with saving"]
}
state = "NOTHING LOADED"
core = None
def __init__(self):
pass
def print_hello(self):
print("Welcome to %s @ version %s\nCurrent algorithm: %s\n"
% (self.name, self.version, self.current_algorithm) + "-"*80)
def print_commands(self):
print("LIST OF COMMANDS:\n"+"\n".join(["-%s\t(%s)" % (key, value[0]) for key, value in self.commands.items()]))
def print_crypto_test(self):
s = "".join([chr(random.randint(0, 255)) for i in range(100)])
print("CRYPTOTEST")
print("RAW=" + s)
print("CIP=" + Core.encrypt(s, "123456789"))
print("REC=" + Core.decrypt(Core.encrypt(s, "123456789"), "123456789"))
print("~"*80)
def prompt(self):
print("-"*80)
print(self.name)
print(self.state)
inp = raw_input("==>")
if inp not in self.commands.keys():
self.print_commands()
return
if inp == "HELP":
self.print_commands()
return
if inp == "QUIT":
print("BYE")
quit()
if self.state == "NOTHING LOADED":
if inp in ("LOCK", "GEN", "GET", "ALL", "REM", "COPY"):
print("INVALID COMMAND. You cannot manipulate a not opened db.")
return
else:
if inp in ("UNLOCK", "INIT"):
self.core = Core(raw_input("db_file:"), raw_input("master_password:"))
print("try to load db...")
self.core.load_db()
print("Core is ready!")
self.state = "DB (%s) LOADED" % (self.core.db_file)
return
else:
print("MISSING IMPLEMENTATION (->REPORT BUG")
return
else:
if inp == "LOCK":
print("Locking and leaving the current db...")
del self.core
self.core = None
self.state = "NOTHING LOADED"
return
elif inp == "GEN":
print("Enter the account's name, like Facebook or github")
name = raw_input("Name:")
print(self.core.create_password(name))
print("DB saved...")
return
elif inp == "GET":
name = raw_input("Name:")
print(self.core.get_password(name))
return
elif inp == "ALL":
print("\n".join(self.core.get_all_keys()))
return
elif inp == "REM":
name = raw_input("Name:")
print(self.core.remove_password(name))
print("DB saved...")
return
elif inp == "COPY":
name = raw_input("Name:")
self.core.copy_password_to_clipboard(name)
print("Password is in clipboard")
return
else:
print("Missing Implementation or INVALID COMMAND (like INIT) at this stage")
def mainloop(self):
self.print_crypto_test()
self.print_hello()
while True:
self.prompt()
if __name__ == '__main__':
Terminal().mainloop()