Commit 7589efe9 authored by Benjamin REED's avatar Benjamin REED

not working, just a test of a diff password system

parent 245788f3
Pipeline #3051 canceled with stages
OPNHYGpmFwa1tM17kGYFowABhqCAAAAAAGdSBM85zx4zTFeOa_FeZpa5eG_UCYuwNUVcP5JtmN4Kw20ZQe3dR1tS5kLuaFbybzMabAHkVgkuOEOsMiv86Mjqv5PSsqZx4EXGbQvqyohhZXB-LQ==
gurt,gAAAAABnUgZWqhD6KqOfi_na5PygL7BE4u9-DHarXn1sYxQkEXjYCH_kb-NJsOHWfQeQktFfljwvH2_IFUCJ-cvsjNdzp_EA7A==
schmurt,gAAAAABnUgZWqhD6KqOfi_na5PygL7BE4u9-DHarXn1sYxQkEXjYCH_kb-NJsOHWfQeQktFfljwvH2_IFUCJ-cvsjNdzp_EA7A==
\ No newline at end of file
import sys import sys
import getpass
import secrets import secrets
import os import os
import random import random
...@@ -14,12 +15,13 @@ USAGES = { ...@@ -14,12 +15,13 @@ 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)", "list": "list [ <label> <password> ](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 "save": "save - Saves the state to the file originally inputted.", # TODO: make it possible to save to a new file
"drop": "drop <label> - deletes the selected label from the list of passwords. This can be undone with the 'undo' command, however if 'drop' is used on an already deleted password, the password is removed forever from the database.", "drop": "drop <label> - deletes the selected label from the list of passwords. This can be undone with the 'undo' command, however if 'drop' is used on an already deleted password, the password is removed forever from the database.",
"undo": "undo - undoes the latest deletion/modification", "undo": "undo - undoes the latest deletion/modification",
"history": "history - prints all the deleted passwords", "history": "history - prints all the deleted passwords",
"cfg": "config [ <param=newparam> ] - configures the current program state or lists it when run without arguments. Example: 'config hints=False'", "cfg": "config [ <param=newparam> ] - configures the current program state or lists it when run without arguments. Example: 'config hints=False'",
"rename": "rename <label> <new label> - Changes a password's label",
} }
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)])
...@@ -86,16 +88,16 @@ class State: ...@@ -86,16 +88,16 @@ class State:
lines = content.split('\n') lines = content.split('\n')
head = lines[0] head = lines[0]
try: # try:
key, decrypt = password_decrypt(head, password) # key, decrypt = password_decrypt(head, password)
except: # except:
print("Password does not match for this database, and as such it cannot be decrypted.") # print("Password does not match for this database, and as such it cannot be decrypted.")
raise Exception # raise Exception
self.passwords = {} self.passwords = {}
self.to_remove = [] self.to_remove = []
self.key = bytes(0)
self.overwritten = [] self.overwritten = []
self.key = key
self.head = head self.head = head
if content == "": if content == "":
...@@ -104,7 +106,7 @@ class State: ...@@ -104,7 +106,7 @@ class State:
for i, line in enumerate(lines): for i, line in enumerate(lines):
if i == 0: if i == 0:
continue # lmao so hacky! whaddeva!! continue
if line == "": if line == "":
continue continue
parts = line.split(",") parts = line.split(",")
...@@ -124,16 +126,31 @@ class State: ...@@ -124,16 +126,31 @@ class State:
file.close() file.close()
def add_password(self, label, password): def add_password(self, label, password_in, password):
if label == "": if label == "":
label = str(len(self.passwords) + 1) label = str(len(self.passwords) + 1)
pourmoi = self.key
self.passwords[label] = Fernet(self.key).encrypt(password.encode()) try:
(key, _) = password_decrypt(self.head, password)
self.passwords[label] = password_encrypt(password_in.encode(), password)
self.key = key
except:
if self.key:
self.passwords[label] = Fernet(self.key).encrypt(password_in.encode())
return
raise Exception
#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, password):
if label in self.passwords: if label in self.passwords:
return Fernet(self.key).decrypt(self.passwords[label]) try:
dec = Fernet(self.key).decrypt(self.passwords[label])
return dec
except:
(key, decoded) = password_decrypt(self.passwords[label], password)
self.key = key
return decoded
else: else:
return b"[ERROR: no such label found]" return b"[ERROR: no such label found]"
...@@ -179,22 +196,35 @@ def main(): ...@@ -179,22 +196,35 @@ def main():
else: else:
filepath = sys.argv[1] filepath = sys.argv[1]
if len(sys.argv) < 3:
print(f"No password supplied. Usage: {sys.argv[0]} <database path> <password>");
return
password = sys.argv[2]
try: try:
state = handle_args(filepath, password) state = handle_args(filepath, "TODO: REMOVE THIS SHEISSE")
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.")
ask = input(f"Create {filepath}? (Y/N)") ask = input(f"Create {filepath}? (Y/N)")
ask = ask.strip() ask = ask.strip()
ask = ask.lower() ask = ask.lower()
if ask == "y": if ask == "y":
password = ""
while True:
# TODO: make this print asterisks or waddeva
ask = input(f"Please input a password for {filepath} (Note: password cannot contain spaces): ")
ask = ask.strip()
if ask == "q":
print("Cancelling.")
return
elif len(ask) > 100 or len(ask) < 8 or ask == "":
print("Password is too long/short, please try again.")
else:
print("Type password again to confirm:")
conf = input(f"Confirmation: ")
conf = conf.strip()
if conf != ask:
print("Error, passwords do not match.")
continue
break
password = ask
file = open(filepath, "xb") file = open(filepath, "xb")
salt = secrets.token_bytes(16)
key = password_encrypt("super secret hihi >_>".encode(), password) key = password_encrypt("super secret hihi >_>".encode(), password)
file.write(key) file.write(key)
file.write('\n'.encode()) file.write('\n'.encode())
...@@ -296,6 +326,22 @@ def main(): ...@@ -296,6 +326,22 @@ def main():
continue continue
ltst = state.to_remove.pop() ltst = state.to_remove.pop()
print(f"Undone latest deletion: {ltst}") print(f"Undone latest deletion: {ltst}")
elif command == "rename" or command == "replace" or command == "ren":
if len(args) < 3:
print(f"Too few arguments for command {command}. Usage: {USAGES['rename']}")
continue
label = args[1].strip().lower()
if label not in state.passwords:
print(f"Password {args[1]} not found in database.")
else:
newlabel = args[2].strip().lower()
if newlabel in state.passwords:
print(f"Cannot rename to {newlabel}, as there is already a password with that name")
continue
state.passwords[newlabel] = state.passwords[label]
del state.passwords[label]
print(f"Renamed {label} to {newlabel}")
elif command == "restore" or command == "res" or command == "r": elif command == "restore" or command == "res" or command == "r":
if len(args) < 2: if len(args) < 2:
print(f"Too few arguments for command {command}. Usage: {USAGES['restore']}") print(f"Too few arguments for command {command}. Usage: {USAGES['restore']}")
...@@ -379,7 +425,10 @@ def main(): ...@@ -379,7 +425,10 @@ def main():
password = generate_password(length, chars) password = generate_password(length, chars)
print(str(password)) print(str(password))
state.add_password(label, password) try:
state.add_password(label, password, input("Please input the database password: "))
except:
print("Incorrect 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: # if state.passwords.len() >= 10:
...@@ -390,7 +439,17 @@ def main(): ...@@ -390,7 +439,17 @@ def main():
if cfg['hints'] == True: if cfg['hints'] == True:
print("(Hint: type 'list <key>' to decrypt your chosen password") print("(Hint: type 'list <key>' to decrypt your chosen password")
elif len(args) == 2: elif len(args) == 2:
print(state.get_password(args[1]).decode('utf-8')) label = args[1].strip().lower()
try:
print(state.get_password(args[1], "").decode('utf-8'))
except:
print("Please input the password to decrypt this item.")
elif len(args) == 3:
label = args[1].strip().lower()
password = args[2]
print(state.get_password(args[1], password).decode('utf-8'))
else:
print(f"Too few args for {command}. Usage: {USAGES['list']}")
......
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