Commit b7a19d09 authored by Benjamin REED's avatar Benjamin REED

working build, what on earth am i doing with my life

parent b3b0635c
...@@ -14,6 +14,8 @@ USAGES = { ...@@ -14,6 +14,8 @@ USAGES = {
"new": "new <length> [ <label> <character set> ] ( 'length' = num characters in password, 'label' = optional name for the password 'charset' = characters that can be in the password (use '-<characters>' to exclude a list of characters))", "new": "new <length> [ <label> <character set> ] ( 'length' = num characters in password, 'label' = optional name for the password 'charset' = characters that can be in the password (use '-<characters>' to exclude a list of characters))",
"echo": "echo <string content> ( 'string_content' = any text to be echoed )", "echo": "echo <string content> ( 'string_content' = any text to be echoed )",
"dbg": "dbg ( Prints a debug output of the manager's state )", "dbg": "dbg ( Prints a debug output of the manager's state )",
"list": "list [ <label> ] (Prints the passwords currently stored 'label' = the password you want to decrypt)",
"save": "save - Saves the state to the file originally inputted.", # TODO: make it possible to save to a new file
} }
DEFAULT_KEYGEN_ITERATIONS = 100_000 DEFAULT_KEYGEN_ITERATIONS = 100_000
LOWERCASE_ALPHABET = set([chr(e) for e in range(ord('a'), ord('a') + 25)]) LOWERCASE_ALPHABET = set([chr(e) for e in range(ord('a'), ord('a') + 25)])
...@@ -101,11 +103,13 @@ class State: ...@@ -101,11 +103,13 @@ class State:
label = str(len(self.passwords) + 1) label = str(len(self.passwords) + 1)
pourmoi = self.key pourmoi = self.key
self.passwords[label] = Fernet(self.key).encrypt(password.encode()) self.passwords[label] = Fernet(self.key).encrypt(password.encode())
print(repr(self.passwords[label]))
#self.passwords[label] = encrypt(password.encode, self.key) #self.passwords[label] = encrypt(password.encode, self.key)
def get_password(self, label): def get_password(self, label):
if label in self.passwords:
return Fernet(self.key).decrypt(self.passwords[label]) return Fernet(self.key).decrypt(self.passwords[label])
else:
return b"[ERROR: no such label found]"
def save(self): def save(self):
content = self.head + "\n" + "\n".join(map(lambda x: f"{x[0]},{x[1].decode('utf-8')}", self.passwords.items())) content = self.head + "\n" + "\n".join(map(lambda x: f"{x[0]},{x[1].decode('utf-8')}", self.passwords.items()))
...@@ -120,7 +124,6 @@ def caesar_cipher(inp, offset): ...@@ -120,7 +124,6 @@ def caesar_cipher(inp, offset):
def generate_password(length, charset): def generate_password(length, charset):
s = [] s = []
print(charset)
for i in range(0, length): for i in range(0, length):
s.append(random.choice(list(charset))) s.append(random.choice(list(charset)))
return ''.join(s) return ''.join(s)
...@@ -147,7 +150,6 @@ def main(): ...@@ -147,7 +150,6 @@ def main():
password = sys.argv[2] password = sys.argv[2]
try: try:
print("?")
state = handle_args(filepath, password) state = handle_args(filepath, password)
except FileNotFoundError: except FileNotFoundError:
print(f"Error while opening database file, {filepath} does not exist or cannot be opened.") print(f"Error while opening database file, {filepath} does not exist or cannot be opened.")
...@@ -194,6 +196,12 @@ def main(): ...@@ -194,6 +196,12 @@ def main():
print(" ".join(args[1:])) print(" ".join(args[1:]))
elif command == "dbg": elif command == "dbg":
print(vars(state)) print(vars(state))
elif command == "save" or command == "s":
try:
state.save()
print(f"State saved to {filepath}!")
except:
print("There was an error saving, maybe back up your stuff now. Sorry")
elif command == "new": # Form: new <length> [ <label> ] elif command == "new": # Form: new <length> [ <label> ]
if len(args) < 2: if len(args) < 2:
print(f"Too few arguments for {command}, usage: {USAGES[command]}") print(f"Too few arguments for {command}, usage: {USAGES[command]}")
...@@ -204,18 +212,18 @@ def main(): ...@@ -204,18 +212,18 @@ def main():
print(f"Error parsing length as number, usage: {USAGES[command]}") print(f"Error parsing length as number, usage: {USAGES[command]}")
continue continue
if len(args) == 3: if len(args) >= 3:
label = args[2] label = args[2]
else: else:
label = "" label = ""
charset = "all" charset = "all"
chars = set()
if len(args) == 4: if len(args) == 4:
charset = args[3] charset = args[3]
chars = LOWERCASE_ALPHABET
if charset == "all": if charset == "all":
chars = LOWERCASE_ALPHABET | UPPERCASE_ALPHABET | NUMBERS | SPECIAL chars = LOWERCASE_ALPHABET | UPPERCASE_ALPHABET | NUMBERS | SPECIAL
else: else:
...@@ -231,10 +239,13 @@ def main(): ...@@ -231,10 +239,13 @@ def main():
state.add_password(label, password) state.add_password(label, password)
elif command == "list" or command == "ls": elif command == "list" or command == "ls":
if len(args) == 1: if len(args) == 1:
# if state.passwords.len() >= 10:
# TODO: make it possible to scroll thru a list of passwords
for l in state.passwords: for l in state.passwords:
print(f"{l}: {state.get_password(l).decode('utf-8')}") print(f"{l}: [...]")
print("(Type 'list <key>' to decrypt your chosen password")
elif len(args) == 2: elif len(args) == 2:
print(state.get_password(args[1])) print(state.get_password(args[1]).decode('utf-8'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment