From f5e290df65cba814143f004c0487a7a020caeeab Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 9 Dec 2022 14:52:57 +0100 Subject: [PATCH] implemented json file safe and dynamic master password --- scripts/classes/AbstractSplittedSecret.py | 3 ++- scripts/classes/Cleanup.py | 3 +++ scripts/classes/Generate.py | 31 ++++++++++++++++++----- scripts/main.py | 11 ++++++-- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/scripts/classes/AbstractSplittedSecret.py b/scripts/classes/AbstractSplittedSecret.py index f53473a..2dbc1b0 100644 --- a/scripts/classes/AbstractSplittedSecret.py +++ b/scripts/classes/AbstractSplittedSecret.py @@ -3,4 +3,5 @@ from .Cli import Cli class AbstractSplittedSecret(Cli): def __init__(self): super(Cli, self).__init__() - self.encrypted_splitted_password_files_folder = "data/encrypted/splitted_password_files/" \ No newline at end of file + self.encrypted_splitted_password_files_folder = "data/encrypted/splitted_password_files/" + self.decrypted_password_files_folder="data/decrypted/password_files/" \ No newline at end of file diff --git a/scripts/classes/Cleanup.py b/scripts/classes/Cleanup.py index e0efc7e..1a9bb54 100644 --- a/scripts/classes/Cleanup.py +++ b/scripts/classes/Cleanup.py @@ -3,6 +3,9 @@ class Cleanup(AbstractSplittedSecret): def __init__(self): super(Cleanup, self).__init__() def deleteAllEncryptedFiles(self): + self.executeCommand('rm -v ' + self.decrypted_password_files_folder + '*') + print(self.getCommandString()) + print(self.getOutputString()) self.executeCommand('rm -v ' + self.encrypted_splitted_password_files_folder + '*') print(self.getCommandString()) print(self.getOutputString()) diff --git a/scripts/classes/Generate.py b/scripts/classes/Generate.py index e272e35..e389b1b 100644 --- a/scripts/classes/Generate.py +++ b/scripts/classes/Generate.py @@ -3,15 +3,16 @@ import string import math import numpy import re +import json from .AbstractSplittedSecret import AbstractSplittedSecret class Generate(AbstractSplittedSecret): - def __init__(self, amount_of_secret_holders, decryption_quota): + def __init__(self, amount_of_secret_holders, decryption_quota,master_password): super(Generate, self).__init__() self.amount_of_secret_holders = amount_of_secret_holders self.decryption_quota = decryption_quota - self.decrypted_master_password_file_path="data/decrypted/password_files/master-password.txt" + self.master_password = master_password self.quota_factor=self.decryption_quota/100 self.group_members_amount=math.ceil(self.amount_of_secret_holders * self.quota_factor) @@ -81,15 +82,33 @@ class Generate(AbstractSplittedSecret): self.group_mapped_data[password_group_index_int]['password'] += password index += 1 - def generateGroupFiles(self): + def generateEncryptedGroupFiles(self): for password_group_index_int in self.group_mapped_data: encrypted_splitted_password_file = AbstractSplittedSecret().encrypted_splitted_password_files_folder + str(password_group_index_int) + ".txt.gpg" - self.executeCommand('gpg --batch --passphrase "' + self.group_mapped_data[password_group_index_int]['password'] + '" -o "' + encrypted_splitted_password_file + '" -c "' + self.decrypted_master_password_file_path + '"') + self.executeCommand('echo "' + self.master_password + '" | gpg --symmetric --armor --batch --passphrase "' + self.group_mapped_data[password_group_index_int]['password'] + '" -o "' + encrypted_splitted_password_file + '"') print(self.getCommandString()) + + def saveJsonFile(self,file_path,data): + with open(file_path, 'w', encoding='utf-8') as file: + json.dump(data, file, ensure_ascii=False, indent=4) - def execute(self): + def saveUserMappedData(self): + for user_id in self.user_mapped_data: + file_path=self.decrypted_password_files_folder+user_id+'.json' + self.saveJsonFile(file_path, self.user_mapped_data[user_id]) + + def saveGroupMappedData(self): + file_path=self.decrypted_password_files_folder+'group_mapped.json' + self.saveJsonFile(file_path, self.group_mapped_data) + + def saveMappedData(self): + self.saveUserMappedData() + self.saveGroupMappedData(); + + def generate(self): self.generateMappedData() - self.generateGroupFiles() + self.saveMappedData() + self.generateEncryptedGroupFiles() def getUserMappedData(self): return self.user_mapped_data diff --git a/scripts/main.py b/scripts/main.py index dce2f90..444df19 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -1,12 +1,14 @@ import argparse from classes.Generate import Generate from classes.Cleanup import Cleanup +from getpass import getpass if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','generate']) parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=range(1,9)) parser.add_argument('--quota', type=int, dest='decryption_quota', choices=range(1,101),required=False) + parser.add_argument('--master-password',type=str, dest='master_password',required=False) args = parser.parse_args() if args.mode == 'cleanup': @@ -15,6 +17,11 @@ if __name__ == '__main__': exit() if args.mode == 'generate': - generate = Generate(args.amount_of_secret_holders, args.decryption_quota) - generate.execute() + if args.master_password is None: + print("Please enter the master password:") + master_password = getpass() + else: + master_password = args.master_password + generate = Generate(args.amount_of_secret_holders, args.decryption_quota,master_password) + generate.generate() exit() \ No newline at end of file