From b3dc24350f4329a0019cedd235471fc92558a205 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Wed, 18 Oct 2023 21:04:07 +0200 Subject: [PATCH] add encrypt and decrypt basics --- src/main.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.h | 8 +++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index e1d5acc..eaa1c02 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,10 @@ void input(int argc, char *argv[]) command.addCommandAlias("remove", "r"); command.addCommand("show", "[script] - Shows a script", showScript); command.addCommandAlias("show", "s"); - + command.addCommand("encrypt", "[script] - Encrypts a script", encryptScript); + command.addCommandAlias("encrypt", "enc"); + command.addCommand("decrypt", "[script] - Decrypts a script", decryptScript); + command.addCommandAlias("decrypt", "dec"); command.addDefaultCommand(runScript); command.runCommand(argv[1], argc, argv); } @@ -107,6 +110,60 @@ void runScript(int argc, char *argv[]) // } } +// encrypt a script in the autom directory +void encryptScript(int argc, char *argv[]) +{ + std::string password = ""; + std::string password2 = ""; + std::string script = home_dir + "/" + argv[1]; + + std::cout << "encrypting file " << script << std::endl; + std::cout << "Enter password: "; + password = EnterPassword(); + std::cout << "Enter password again: "; + password2 = EnterPassword(); + + if (password != password2) + { + std::cout << "Passwords do not match" << std::endl; + return; + } + + if (password == "") + { + std::cout << "Password cannot be empty" << std::endl; + return; + } + + if (!std::filesystem::exists(script)) + { + std::cout << "Script " << argv[1] << " does not exist" << std::endl; + return; + } +} + +// encrypt a script in the autom directory +void decryptScript(int argc, char *argv[]) +{ + std::cout << "decrypting file " << argv[1] << std::endl; +} + +std::string EnterPassword() +{ + std::string password = ""; + char c; + while (true) + { + c = getch(); + if (c == '\n') + break; + password += c; + std::cout << "*"; + } + std::cout << std::endl; + return password; +} + void showScript(int argc, char *argv[]) { diff --git a/src/main.h b/src/main.h index eba237c..d2f561f 100644 --- a/src/main.h +++ b/src/main.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "inputparser.h" #include "command.h" @@ -33,6 +34,13 @@ void editScript(std::string name, std::string dir); void removeScript(int argc, char *argv[]); // show a script in the autom directory void showScript(int argc, char *argv[]); +// encrypt a script in the autom directory +void encryptScript(int argc, char *argv[]); +// decrypt a script in the autom directory +void decryptScript(int argc, char *argv[]); + +std::string EnterPassword(); + // help function for showing help message void help(int argc, char *argv[]);