diff options
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 49 |
1 files changed, 30 insertions, 19 deletions
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 @@ | |||
| 9 | 9 | ||
| 10 | #include "encryption.h" | 10 | #include "encryption.h" |
| 11 | #include "ll.h" | 11 | #include "ll.h" |
| 12 | #include "VX-GAMBLEGROUND.h" | ||
| 12 | 13 | ||
| 13 | #include <sys/types.h> | 14 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> | 15 | #include <sys/stat.h> |
| @@ -202,47 +203,57 @@ int rename_format(const char *fullpath, const char *format, ...) { | |||
| 202 | } | 203 | } |
| 203 | 204 | ||
| 204 | 205 | ||
| 205 | 206 | int ENorDE_cryptvxgg(const struct nodelist *list, const char *passphrase, int flag) { | |
| 206 | int encryptvxgg(const struct nodelist *list, const char *passphrase) { | ||
| 207 | int fd = -1; | 207 | int fd = -1; |
| 208 | |||
| 209 | regex_t encext; | 208 | regex_t encext; |
| 210 | regcomp(&encext, "(.+\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE); | 209 | regcomp(&encext, "(.+\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE); |
| 211 | 210 | ||
| 212 | // Encrypt everything | ||
| 213 | for(const struct nodelist *p = list; p != NULL; p = p->next) { | 211 | for(const struct nodelist *p = list; p != NULL; p = p->next) { |
| 214 | if(p->fullpath == NULL) // Make sure I don't accidentally do anything with that last element | 212 | if(p->fullpath == NULL) |
| 215 | continue; | 213 | continue; |
| 216 | if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH) // Don't encrypt already encrypted files | 214 | if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH && flag == VXGG_ENCRYPT) // Don't encrypt already encrypted files |
| 215 | continue; | ||
| 216 | if(regexec(&encext, p->fullpath, 0, NULL, 0) == REG_NOMATCH && flag == VXGG_DECRYPT) // Skip non-matching files | ||
| 217 | continue; | 217 | continue; |
| 218 | 218 | ||
| 219 | fd = open(p->fullpath, O_RDWR); | 219 | fd = open(p->fullpath, O_RDWR); |
| 220 | if(fd < 0) { | 220 | if(fd < 0) { |
| 221 | #ifdef TESTING | 221 | #ifdef TESTING |
| 222 | error(0, errno, "<encryptvxgg> Could not open \"%s\" for encryption", p->fullpath); | 222 | error(0, 0, "<decryptvxgg> Couldn't open \"%s\" for %s", p->fullpath, (flag == VXGG_ENCRYPT) ? "encryption" : "decryption"); |
| 223 | #endif | 223 | #endif |
| 224 | continue; | 224 | continue; |
| 225 | } | 225 | } |
| 226 | |||
| 227 | passencblock(fd, passphrase); | 226 | passencblock(fd, passphrase); |
| 228 | close(fd); | 227 | close(fd); |
| 229 | #ifndef TESTING | 228 | |
| 230 | rename_format(p->fullpath, "%s.vxgg", p->fullpath); | 229 | if(flag == VXGG_ENCRYPT) { |
| 231 | #else | 230 | #ifndef TESTING |
| 232 | rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); | 231 | rename_format(p->fullpath, "%s.vxgg", p->fullpath); |
| 233 | #endif | 232 | #else |
| 233 | rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); | ||
| 234 | #endif | ||
| 235 | } | ||
| 236 | |||
| 237 | if(flag == VXGG_DECRYPT) { | ||
| 238 | char *newname = NULL; | ||
| 239 | #ifndef TESTING | ||
| 240 | asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg")); | ||
| 241 | #else | ||
| 242 | asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg") - PHRASESIZE - 1); | ||
| 243 | #endif | ||
| 244 | rename_format(p->fullpath, newname, p->fullpath); | ||
| 245 | free(newname); | ||
| 246 | } | ||
| 234 | 247 | ||
| 235 | } | 248 | } |
| 236 | 249 | ||
| 250 | /* The beauty of my shitty encryption function is that it's reversible by just running it again with the | ||
| 251 | // same password. Most of this function is just picking the right files to operate on and deciding how to | ||
| 252 | // rename them */ | ||
| 253 | |||
| 237 | return 0; | 254 | return 0; |
| 238 | } | 255 | } |
| 239 | 256 | ||
| 240 | // int decryptvxgg(const struct nodelist *list, const char *passphrase) { | ||
| 241 | |||
| 242 | |||
| 243 | // return 0; | ||
| 244 | // } | ||
| 245 | |||
| 246 | /* | 257 | /* |
| 247 | int main() { | 258 | int main() { |
| 248 | int fd = open("test.txt", O_RDWR); | 259 | int fd = open("test.txt", O_RDWR); |
