add annotated main.c
This commit is contained in:
parent
97c5d07f6c
commit
e373ac4a07
73
src/main.annotated.c
Normal file
73
src/main.annotated.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/* ecrypt - generate keys */
|
||||||
|
/* annotated version - explains in great detail what everything does */
|
||||||
|
|
||||||
|
#include <math.h> /* include header file /usr/include/math.h */
|
||||||
|
#include <stdio.h> /* include header file /usr/include/stdio.h */
|
||||||
|
#include <stdlib.h> /* include header file /usr/include/stdlib.h */
|
||||||
|
#include <time.h> /* 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) */
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user