From bed4b90c91037dc4727303b241321c9af03b757a Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 3 Sep 2024 01:28:19 -0500 Subject: Consolidate encryption and decryption function --- src/encryption.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 8b0055c..8cccca8 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -9,6 +9,7 @@ #include "encryption.h" #include "ll.h" +#include "VX-GAMBLEGROUND.h" #include #include @@ -202,47 +203,57 @@ int rename_format(const char *fullpath, const char *format, ...) { } - -int encryptvxgg(const struct nodelist *list, const char *passphrase) { +int ENorDE_cryptvxgg(const struct nodelist *list, const char *passphrase, int flag) { int fd = -1; - regex_t encext; regcomp(&encext, "(.+\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE); - // Encrypt everything for(const struct nodelist *p = list; p != NULL; p = p->next) { - if(p->fullpath == NULL) // Make sure I don't accidentally do anything with that last element + if(p->fullpath == NULL) continue; - if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH) // Don't encrypt already encrypted files + if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH && flag == VXGG_ENCRYPT) // Don't encrypt already encrypted files + continue; + if(regexec(&encext, p->fullpath, 0, NULL, 0) == REG_NOMATCH && flag == VXGG_DECRYPT) // Skip non-matching files continue; fd = open(p->fullpath, O_RDWR); if(fd < 0) { #ifdef TESTING - error(0, errno, " Could not open \"%s\" for encryption", p->fullpath); + error(0, 0, " Couldn't open \"%s\" for %s", p->fullpath, (flag == VXGG_ENCRYPT) ? "encryption" : "decryption"); #endif continue; } - passencblock(fd, passphrase); close(fd); - #ifndef TESTING - rename_format(p->fullpath, "%s.vxgg", p->fullpath); - #else - rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); - #endif + + if(flag == VXGG_ENCRYPT) { + #ifndef TESTING + rename_format(p->fullpath, "%s.vxgg", p->fullpath); + #else + rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); + #endif + } + + if(flag == VXGG_DECRYPT) { + char *newname = NULL; + #ifndef TESTING + asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg")); + #else + asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg") - PHRASESIZE - 1); + #endif + rename_format(p->fullpath, newname, p->fullpath); + free(newname); + } } + /* The beauty of my shitty encryption function is that it's reversible by just running it again with the + // same password. Most of this function is just picking the right files to operate on and deciding how to + // rename them */ + return 0; } -// int decryptvxgg(const struct nodelist *list, const char *passphrase) { - - -// return 0; -// } - /* int main() { int fd = open("test.txt", O_RDWR); -- cgit v1.2.3