From 24e66042cec89cadeebccff42d03427d32add1ea Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 29 Jun 2024 06:59:48 -0500 Subject: fuck randomness all my homies hate randomness --- src/main.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 6395acd..dc9e0bd 100644 --- a/src/main.c +++ b/src/main.c @@ -15,16 +15,93 @@ #include "search.h" #include "ll.h" +#include #include #include +#include #include +#include #include - #include #include +#include + +#include + +// Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361) +int randint(int n) { + if ((n - 1) == RAND_MAX) { + return random(); + } else { + // Supporting larger values for n would requires an even more + // elaborate implementation that combines multiple calls to rand() + assert (n <= RAND_MAX); + + // Chop off all of the values that would cause skew... + int end = RAND_MAX / n; // truncate skew + assert (end > 0); + end *= n; + + // ... and ignore results from rand() that fall above that limit. + // (Worst case the loop condition should succeed 50% of the time, + // so we can expect to bail out of this loop pretty quickly.) + int r; + while ((r = random()) >= end); + + return r % n; + } +} + +char* genphrase(size_t phrasesize) { + char *phrase = calloc(phrasesize, sizeof(char)); + if(phrase == NULL) { + error(0, errno, "Couldn't allocate space for generating random phrase"); + return NULL; + } + + for(size_t i = 0; i < phrasesize; i++) { + phrase[i] = randint(25 + 1) + 65; + if(random() > RAND_MAX / 2) + phrase[i] += 32; + } + + return phrase; +} + +// Initialize random()'s seed. Return values indicate quality of initialization: 0 = best, 3 = worst +int initseed() { + char randbytes[256]; unsigned int seed = 0; + int fail[2] = {0, 0}; + if(getrandom(randbytes, sizeof(randbytes), 0) < 0) { + error(0, errno, "Couldn't get random bytes for initstate"); + fail[0] = 1; + } + if(getrandom(&seed, sizeof(seed), 0) < 0) { + error(0, errno, "Couldn't get random bytes for seed"); + fail[1] = 1; + } + + // Gross I know but I haven't slept in a day and don't care anymore + if(fail[0] && fail[1]) { // Couldn't get either randombyte + srand(time(NULL)); + return 3; + } else if(fail[0] && !fail[1]) { // Couldn't get initstate array + srand(seed); + return 2; + } else if(fail[1] && !fail[0]) { // Couldn't get seed + initstate(time(NULL), randbytes, sizeof(randbytes)); + return 1; + } else { + initstate(seed, randbytes, sizeof(randbytes)); + } + + return 0; +} int main() { - // Get folders + /* + + // Get files struct nodelist *files = scanfiles("./", alphasort); // Encrypt those files @@ -40,6 +117,22 @@ int main() { } nodelist_delete(files); + //*/ + + //initseed(); + + // ok something is fucking up in the initseed function that isn't fucking up here. Great + srand(time(NULL)); + + // WHY THE FUCK IS THIS CRASHING + char phrase[32]; + for(size_t i = 0; i < sizeof(phrase); i++) { + phrase[i] = randint(25 + 1) + 65; + if(random() > (RAND_MAX / 2)) + phrase[i] += 32; + } + + printf("%s\n", phrase); return 0; } \ No newline at end of file -- cgit v1.2.3