From e373ac4a074d979dd5ac2edef5f3e27140909b7e Mon Sep 17 00:00:00 2001 From: Liam Waldron Date: Fri, 24 Mar 2023 12:32:48 -0400 Subject: [PATCH] add annotated main.c --- src/main.annotated.c | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main.annotated.c diff --git a/src/main.annotated.c b/src/main.annotated.c new file mode 100644 index 0000000..3cce064 --- /dev/null +++ b/src/main.annotated.c @@ -0,0 +1,73 @@ +/* ecrypt - generate keys */ +/* annotated version - explains in great detail what everything does */ + +#include /* include header file /usr/include/math.h */ +#include /* include header file /usr/include/stdio.h */ +#include /* include header file /usr/include/stdlib.h */ +#include /* include header file /usr/include/time.h */ + +/* define ANSI_COLOR_BLUE and ANSI_COLOR_RESET */ +#include "../include/colors.h" /* include header file ../include/colors.h */ + +/* include configuration file */ +#include "config.h" + +/* create new void function called "randPasswdGen" with parameter "N", of data type "int" (integer) */ +void randPasswdGen(int N) { + int c = 0; /* initialize counter variable */ + int randomizer = 0; /* initialize randomizer variable */ + + /* seed random number generator with current time, so numbers will be different every time */ + srand((unsigned int)(time(NULL))); + + /* create arrays defining numbers, lowercase letters, uppercase letters, and symbols */ + char numbers[] = "1234567890"; + char letter[] = "abcdefghijklmnopqrstuvwxyz"; + char LETTER[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + char symbols[] = "!@#$%^&*?"; + + /* initialize password variable, with data type "char" (character) */ + char password[N]; + + /* change value of randomizer so it equals the output of function rand() modulo 4 + (modulo is the same as division except it returns remainder instead of quotient) */ + randomizer = rand() % 4; + + /* this is a bit ugly but it gets the job done */ + /* create a for loop, where counter (c) will equal 0, counter must be less than N (password length), + counter will be incremented by one each time loop is run */ + for (c = 0; c < N; c++) { + /* create a switch statement, which will run certain code blocks if a case is met */ + switch (randomizer) { + case 1: /* if randomizer = 1 */ + password[c] = numbers[rand() % 10]; + randomizer = rand() % 4; + printf(ANSI_COLOR_BLUE "%c" ANSI_COLOR_RESET, password[c]); + break; + case 2: /* if randomizer = 2 */ + password[c] = symbols[rand() % 8]; + randomizer = rand() % 4; + printf(ANSI_COLOR_BLUE "%c" ANSI_COLOR_RESET, password[c]); + break; + case 3: /* if randomizer = 3 */ + password[c] = LETTER[rand() % 26]; + randomizer = rand() % 4; + printf(ANSI_COLOR_BLUE "%c" ANSI_COLOR_RESET, password[c]); + break; + default: /* if randomizer doesn't meet any cases */ + password[c] = letter[rand() % 26]; + randomizer = rand() % 4; + printf(ANSI_COLOR_BLUE "%c" ANSI_COLOR_RESET, password[c]); + } + } +} + +/* run main function */ +int main() { + int N = PASSWD_LENGTH; /* password length, can be customized by config.h */ + + randPasswdGen(N); /* run function randPasswdGen with parameter N */ + printf("\n"); /* remove newline character for zsh users */ + + return 0; /* exit with status 0 (no error) */ +}