diff options
| author | @syxhe <https://t.me/syxhe> | 2024-06-29 06:59:48 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2024-06-29 06:59:48 -0500 |
| commit | 24e66042cec89cadeebccff42d03427d32add1ea (patch) | |
| tree | c8c051ce28c5fde7311d2adc3fb36c35091efb81 /src | |
| parent | 221f41078fc42fffce49e59ed338819516594607 (diff) | |
fuck randomness all my homies hate randomness
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 97 |
1 files changed, 95 insertions, 2 deletions
| @@ -15,16 +15,93 @@ | |||
| 15 | #include "search.h" | 15 | #include "search.h" |
| 16 | #include "ll.h" | 16 | #include "ll.h" |
| 17 | 17 | ||
| 18 | #include <sys/random.h> | ||
| 18 | #include <sys/types.h> | 19 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> | 20 | #include <sys/stat.h> |
| 21 | #include <assert.h> | ||
| 20 | #include <unistd.h> | 22 | #include <unistd.h> |
| 23 | #include <stdlib.h> | ||
| 21 | #include <fcntl.h> | 24 | #include <fcntl.h> |
| 22 | |||
| 23 | #include <errno.h> | 25 | #include <errno.h> |
| 24 | #include <error.h> | 26 | #include <error.h> |
| 27 | #include <time.h> | ||
| 28 | |||
| 29 | #include <stdio.h> | ||
| 30 | |||
| 31 | // Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361) | ||
| 32 | int randint(int n) { | ||
| 33 | if ((n - 1) == RAND_MAX) { | ||
| 34 | return random(); | ||
| 35 | } else { | ||
| 36 | // Supporting larger values for n would requires an even more | ||
| 37 | // elaborate implementation that combines multiple calls to rand() | ||
| 38 | assert (n <= RAND_MAX); | ||
| 39 | |||
| 40 | // Chop off all of the values that would cause skew... | ||
| 41 | int end = RAND_MAX / n; // truncate skew | ||
| 42 | assert (end > 0); | ||
| 43 | end *= n; | ||
| 44 | |||
| 45 | // ... and ignore results from rand() that fall above that limit. | ||
| 46 | // (Worst case the loop condition should succeed 50% of the time, | ||
| 47 | // so we can expect to bail out of this loop pretty quickly.) | ||
| 48 | int r; | ||
| 49 | while ((r = random()) >= end); | ||
| 50 | |||
| 51 | return r % n; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | char* genphrase(size_t phrasesize) { | ||
| 56 | char *phrase = calloc(phrasesize, sizeof(char)); | ||
| 57 | if(phrase == NULL) { | ||
| 58 | error(0, errno, "Couldn't allocate space for generating random phrase"); | ||
| 59 | return NULL; | ||
| 60 | } | ||
| 61 | |||
| 62 | for(size_t i = 0; i < phrasesize; i++) { | ||
| 63 | phrase[i] = randint(25 + 1) + 65; | ||
| 64 | if(random() > RAND_MAX / 2) | ||
| 65 | phrase[i] += 32; | ||
| 66 | } | ||
| 67 | |||
| 68 | return phrase; | ||
| 69 | } | ||
| 70 | |||
| 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 | |||
| 84 | // Gross I know but I haven't slept in a day and don't care anymore | ||
| 85 | if(fail[0] && fail[1]) { // Couldn't get either randombyte | ||
| 86 | srand(time(NULL)); | ||
| 87 | return 3; | ||
| 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 | |||
| 98 | return 0; | ||
| 99 | } | ||
| 25 | 100 | ||
| 26 | int main() { | 101 | int main() { |
| 27 | // Get folders | 102 | /* |
| 103 | |||
| 104 | // Get files | ||
| 28 | struct nodelist *files = scanfiles("./", alphasort); | 105 | struct nodelist *files = scanfiles("./", alphasort); |
| 29 | 106 | ||
| 30 | // Encrypt those files | 107 | // Encrypt those files |
| @@ -40,6 +117,22 @@ int main() { | |||
| 40 | } | 117 | } |
| 41 | 118 | ||
| 42 | nodelist_delete(files); | 119 | nodelist_delete(files); |
| 120 | //*/ | ||
| 121 | |||
| 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); | ||
| 43 | 136 | ||
| 44 | return 0; | 137 | return 0; |
| 45 | } \ No newline at end of file | 138 | } \ No newline at end of file |
