From 42a5b93d67c77802deb864c67e5bed4551aff8c0 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 11 Dec 2022 18:46:32 +0100 Subject: [PATCH] Optimized code --- Readme.md | 16 +++++++++++----- data/encrypted/user_files/.gitignore | 1 - scripts/classes/Cleanup.py | 4 ++-- scripts/classes/Encryption.py | 15 +++++++-------- scripts/main.py | 17 ++++++++++++----- 5 files changed, 32 insertions(+), 21 deletions(-) delete mode 100644 data/encrypted/user_files/.gitignore diff --git a/Readme.md b/Readme.md index c55cc2f..6a43ae3 100644 --- a/Readme.md +++ b/Readme.md @@ -43,14 +43,14 @@ python scripts/main.py --mode cleanup --file-types encrypted ## decrypt ### decrypt automatic -To decrypt the data type in: +To decrypt the data execute: ```bash python scripts/main.py --mode decrypt ``` ### decrypt defined user -To decrypt the data for a defined user type in: +To decrypt the data for a defined user execute: ```bash python scripts/main.py --mode decrypt --user "<>" @@ -58,13 +58,19 @@ python scripts/main.py --mode decrypt --user "<>" ## encrypt -### encrypt all data +### encrypt main data ```bash -python scripts/main.py --amount 3 --quota 50 --mode encrypt --add-user-information --master-password "{{master_password}}" +python scripts/main.py --secret-holders-amount "<>" --quota "<>" --mode encrypt --master-password "<>" --input-directory "<>" ``` -### encrypt master-password file +### encrypt master password + +To encrypt the master-password file and to create the neccessary encrypted meta data execute: + +```bash +python scripts/main.py --secret-holders-amount "<>" --quota "<>" --mode encrypt --add-user-information --master-password "<>" --create-meta-data +``` ## todo - add data-input attribut diff --git a/data/encrypted/user_files/.gitignore b/data/encrypted/user_files/.gitignore deleted file mode 100644 index 3f2045d..0000000 --- a/data/encrypted/user_files/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.gpg \ No newline at end of file diff --git a/scripts/classes/Cleanup.py b/scripts/classes/Cleanup.py index ed98aed..b6ab248 100644 --- a/scripts/classes/Cleanup.py +++ b/scripts/classes/Cleanup.py @@ -19,7 +19,7 @@ class Cleanup(): try: self.cli.executeCommand('rm -r ' + folder_path + '*') except Exception as error: - print(error) + pass def cleanupFiles(self,file_type): for folder_path in self.getAllFilePaths(file_type): @@ -29,7 +29,7 @@ class Cleanup(): try: self.cli.executeCommand('find "' + self.paths.getDataFolderPath(Paths.TYPE_ENCRYPTED) + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v') except Exception as error: - print(error) + pass self.cleanupFiles(Paths.TYPE_DECRYPTED) def deleteAll(self): diff --git a/scripts/classes/Encryption.py b/scripts/classes/Encryption.py index 79a7acb..3244e23 100644 --- a/scripts/classes/Encryption.py +++ b/scripts/classes/Encryption.py @@ -40,10 +40,10 @@ class Encryption(): self.user_mapped_data[user_id]['about'][label] = content; def getCoSecretHoldersRange(): - return range(Encryption.MINIMUM_SECRET_HOLDERS,Encryption.MAXIMUM_SECRET_HOLDERS) + return range(Encryption.MINIMUM_SECRET_HOLDERS,(Encryption.MAXIMUM_SECRET_HOLDERS+1)) def getSecretHoldersRange(): - return range(1,Encryption.MAXIMUM_SECRET_HOLDERS) + return range(1,(Encryption.MAXIMUM_SECRET_HOLDERS+1)) def getStartnumber(self): index = 0 @@ -84,7 +84,7 @@ class Encryption(): def compileData(self): self.compileContacts() index = self.getStartnumber() - while index < self.getEndnumber(): + while index <= self.getEndnumber(): password_group_name = ''.join(sorted(str(index))) if self.isGroupValid(password_group_name): password_group_index_int = int(password_group_name) @@ -126,11 +126,10 @@ class Encryption(): data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data} self.encryptToJsonFile(data,file_path,self.master_password) - def encryptMainData(self): - self.cli.executeCommand('tar -C"' + self.paths.getDecryptedMainDataStandartFolder() + '" -cvzf - ./ | gpg -c --batch --passphrase "' + self.master_password +'" > "' + self.paths.getEncryptedMainDataFile() + '"') + def encryptMainData(self,input_directory): + self.cli.executeCommand('tar -C"' + input_directory + '" -cvzf - ./ | gpg -c --batch --passphrase "' + self.master_password +'" > "' + self.paths.getEncryptedMainDataFile() + '"') - def encryptAll(self): + def encryptMetaData(self): self.encryptUserFile() self.encryptAccumulatedFile() - self.encryptGroupFiles() - self.encryptMainData() + self.encryptGroupFiles() \ No newline at end of file diff --git a/scripts/main.py b/scripts/main.py index 421964c..3e26c92 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -33,12 +33,14 @@ try: parser = argparse.ArgumentParser() parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','encrypt','decrypt']) parser.add_argument('--file-types',type=str, dest='file_types',required=False,choices=[Paths.TYPE_DECRYPTED, Paths.TYPE_ENCRYPTED]) - parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=Encryption.getCoSecretHoldersRange()) + parser.add_argument('--secret-holders-amount',type=int, dest='amount_of_secret_holders',required=False,choices=Encryption.getCoSecretHoldersRange(),help="Needed for creating of encryption meta data.") 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) parser.add_argument('--user-password',type=str, dest='user_password',required=False) parser.add_argument('--user',type=int, dest='user',choices=Encryption.getSecretHoldersRange(),required=False) - parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction) + parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction, help="Add additional information to users.") + parser.add_argument('--input-directory',type=str,dest='input_directory',required=False, help="The directory from which the data should be encrypted.") + parser.add_argument('--create-meta-data',type=bool, dest='create_meta_data', default=False, required=False, action=argparse.BooleanOptionalAction, help="When mode is encrypt and this flag is set, the encrypted meta data is created.") args = parser.parse_args() print("Application started.") @@ -149,13 +151,18 @@ try: else: master_password = args.master_password encrypt = Encryption(cli,paths,args.amount_of_secret_holders, args.decryption_quota, master_password) - if args.add_user_information is not None: + if args.add_user_information is True: for user_id in encrypt.user_mapped_data: - for label in ['name','phone','email','address']: + for label in ['name','phone','email','address','notes']: print("Enter attribut <<" + label + ">> for user <<" + user_id+ ">>:" ) encrypt.addInformationToUser(user_id, label, str(input())) encrypt.compileData() - encrypt.encryptAll() + if args.create_meta_data is True: + print("Create and encrypt meta data.") + encrypt.encryptMetaData() + if args.input_directory is not None: + print("Encrypt main data.") + encrypt.encryptMainData(args.input_directory) dirty_exit() except KeyboardInterrupt: print("Program interrupted by user.")