diff options
| author | @syxhe <https://t.me/syxhe> | 2024-06-29 23:00:12 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2024-06-29 23:00:12 -0500 |
| commit | 33eca360a97dae7aab5e8d30376759aa9fc34c61 (patch) | |
| tree | 3b15729a0360b3454a0acbf89414877f64cc362f /src | |
| parent | 24e66042cec89cadeebccff42d03427d32add1ea (diff) | |
Add argp shit
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 119 |
1 files changed, 73 insertions, 46 deletions
| @@ -19,15 +19,57 @@ | |||
| 19 | #include <sys/types.h> | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> | 20 | #include <sys/stat.h> |
| 21 | #include <assert.h> | 21 | #include <assert.h> |
| 22 | #include <string.h> | ||
| 22 | #include <unistd.h> | 23 | #include <unistd.h> |
| 23 | #include <stdlib.h> | 24 | #include <stdlib.h> |
| 24 | #include <fcntl.h> | 25 | #include <fcntl.h> |
| 25 | #include <errno.h> | 26 | #include <errno.h> |
| 26 | #include <error.h> | 27 | #include <error.h> |
| 28 | #include <argp.h> | ||
| 27 | #include <time.h> | 29 | #include <time.h> |
| 28 | 30 | ||
| 29 | #include <stdio.h> | 31 | #include <stdio.h> |
| 30 | 32 | ||
| 33 | #define PHRASESIZE 32 | ||
| 34 | |||
| 35 | const char *argp_program_version = "Alpha-0.1"; | ||
| 36 | const char *argp_program_bug_address = "@syxhe on Telegram (https://t.me/syxhe)"; | ||
| 37 | |||
| 38 | static char doc[] = "lol"; | ||
| 39 | static char argdoc[] = "lmao"; | ||
| 40 | |||
| 41 | static struct argp_option options[] = { | ||
| 42 | {.name = "passphrase", .key = 'p', .arg = "key", .flags = 0, .doc = "Specify passphrase for encryption/decryption", .group = 0}, | ||
| 43 | {"decrypt", 'd', 0, 0, "Skip the slots minigame and immediately decrypt (or encrypt) files", 0}, | ||
| 44 | {0} | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct arguments { | ||
| 48 | char *inputpass; | ||
| 49 | int skipslots; | ||
| 50 | }; | ||
| 51 | |||
| 52 | static error_t parse_opt(int key, char *arg, struct argp_state *state) { | ||
| 53 | struct arguments *args = state->input; | ||
| 54 | |||
| 55 | switch(key) { | ||
| 56 | case 'p': | ||
| 57 | args->inputpass = arg; | ||
| 58 | break; | ||
| 59 | |||
| 60 | case 'd': | ||
| 61 | args->skipslots = 1; | ||
| 62 | break; | ||
| 63 | |||
| 64 | default: | ||
| 65 | return ARGP_ERR_UNKNOWN; | ||
| 66 | } | ||
| 67 | |||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | static struct argp argp = {options, parse_opt, argdoc, doc, NULL, 0, 0}; | ||
| 72 | |||
| 31 | // Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361) | 73 | // Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361) |
| 32 | int randint(int n) { | 74 | int randint(int n) { |
| 33 | if ((n - 1) == RAND_MAX) { | 75 | if ((n - 1) == RAND_MAX) { |
| @@ -68,40 +110,40 @@ char* genphrase(size_t phrasesize) { | |||
| 68 | return phrase; | 110 | return phrase; |
| 69 | } | 111 | } |
| 70 | 112 | ||
| 71 | // Initialize random()'s seed. Return values indicate quality of initialization: 0 = best, 3 = worst | ||
| 72 | int initseed() { | ||
| 73 | char randbytes[256]; unsigned int seed = 0; | ||
| 74 | int fail[2] = {0, 0}; | ||
| 75 | if(getrandom(randbytes, sizeof(randbytes), 0) < 0) { | ||
| 76 | error(0, errno, "Couldn't get random bytes for initstate"); | ||
| 77 | fail[0] = 1; | ||
| 78 | } | ||
| 79 | if(getrandom(&seed, sizeof(seed), 0) < 0) { | ||
| 80 | error(0, errno, "Couldn't get random bytes for seed"); | ||
| 81 | fail[1] = 1; | ||
| 82 | } | ||
| 83 | 113 | ||
| 84 | // Gross I know but I haven't slept in a day and don't care anymore | 114 | int main(int argc, char *argv[]) { |
| 85 | if(fail[0] && fail[1]) { // Couldn't get either randombyte | 115 | struct arguments args; |
| 86 | srand(time(NULL)); | 116 | args.inputpass = NULL; |
| 87 | return 3; | 117 | args.skipslots = 0; |
| 88 | } else if(fail[0] && !fail[1]) { // Couldn't get initstate array | ||
| 89 | srand(seed); | ||
| 90 | return 2; | ||
| 91 | } else if(fail[1] && !fail[0]) { // Couldn't get seed | ||
| 92 | initstate(time(NULL), randbytes, sizeof(randbytes)); | ||
| 93 | return 1; | ||
| 94 | } else { | ||
| 95 | initstate(seed, randbytes, sizeof(randbytes)); | ||
| 96 | } | ||
| 97 | 118 | ||
| 98 | return 0; | 119 | argp_parse(&argp, argc, argv, ARGP_NO_ARGS, 0, &args); |
| 99 | } | ||
| 100 | 120 | ||
| 101 | int main() { | 121 | if(args.inputpass != NULL) { |
| 102 | /* | 122 | if(strlen(args.inputpass) != PHRASESIZE) { |
| 123 | error(1, 0, "Encryption passphrase must be exactly %d characters long", PHRASESIZE); | ||
| 124 | } | ||
| 103 | 125 | ||
| 104 | // Get files | 126 | printf("Using input passphrase \"%s\"\n", args.inputpass); |
| 127 | } else { | ||
| 128 | // Fuck you | ||
| 129 | srandom((unsigned int)time(NULL)); | ||
| 130 | |||
| 131 | char phrase[PHRASESIZE]; | ||
| 132 | for(size_t i = 0; i < sizeof(phrase); i++) { | ||
| 133 | phrase[i] = randint(25 + 1) + 65; | ||
| 134 | if(random() > (RAND_MAX / 2)) | ||
| 135 | phrase[i] += 32; | ||
| 136 | } | ||
| 137 | |||
| 138 | printf("Encryption phrase: %s\n\nWrite this phrase down EXACTLY if you want to recover your files\n", phrase); | ||
| 139 | |||
| 140 | if(args.skipslots == 0) { | ||
| 141 | printf("Hit Enter to contine, or CTRL+C to cancel..."); | ||
| 142 | getchar(); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | /* Get files | ||
| 105 | struct nodelist *files = scanfiles("./", alphasort); | 147 | struct nodelist *files = scanfiles("./", alphasort); |
| 106 | 148 | ||
| 107 | // Encrypt those files | 149 | // Encrypt those files |
| @@ -112,27 +154,12 @@ int main() { | |||
| 112 | continue; | 154 | continue; |
| 113 | } | 155 | } |
| 114 | 156 | ||
| 115 | passencblock(fd, "We do a little trolling"); | 157 | passencblock(fd, phrase); |
| 116 | close(fd); | 158 | close(fd); |
| 117 | } | 159 | } |
| 118 | 160 | ||
| 119 | nodelist_delete(files); | 161 | nodelist_delete(files); |
| 120 | //*/ | 162 | //*/ |
| 121 | 163 | ||
| 122 | //initseed(); | ||
| 123 | |||
| 124 | // ok something is fucking up in the initseed function that isn't fucking up here. Great | ||
| 125 | srand(time(NULL)); | ||
| 126 | |||
| 127 | // WHY THE FUCK IS THIS CRASHING | ||
| 128 | char phrase[32]; | ||
| 129 | for(size_t i = 0; i < sizeof(phrase); i++) { | ||
| 130 | phrase[i] = randint(25 + 1) + 65; | ||
| 131 | if(random() > (RAND_MAX / 2)) | ||
| 132 | phrase[i] += 32; | ||
| 133 | } | ||
| 134 | |||
| 135 | printf("%s\n", phrase); | ||
| 136 | |||
| 137 | return 0; | 164 | return 0; |
| 138 | } \ No newline at end of file | 165 | } \ No newline at end of file |
