aboutsummaryrefslogtreecommitdiff
path: root/password.c
diff options
context:
space:
mode:
authorchupax <leonardja3@protonmail.com>2024-08-20 09:50:20 -0500
committerchupax <leonardja3@protonmail.com>2024-08-20 09:50:20 -0500
commitb2df2a824a78f2beebdbc0c0d5b08bb0bd566c4b (patch)
treef91428bb5b7865ea0d43c047054a5a5dd0cafea9 /password.c
First commit
Generates one random password. 5 digit number, random word w/ random case, and random characters
Diffstat (limited to 'password.c')
-rw-r--r--password.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/password.c b/password.c
new file mode 100644
index 0000000..e028b61
--- /dev/null
+++ b/password.c
@@ -0,0 +1,89 @@
+// https://github.com/dwyl/english-words
+
+#include <sodium/randombytes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sodium.h>
+#include <ctype.h> //To Upper
+
+
+long count_lines(FILE *File) { // Accepts file as input
+ char c;
+ long line_counter = 0;
+ while ((c = getc(File)) != EOF) {
+ if (c == '\n') {
+ line_counter++; // Increment when each line number is passed
+ }
+ }
+ return(line_counter); // Return the found value
+}
+
+void randomize_case(char *a) { // Accepts a string as input
+ for (int i = 0; i < strlen(a); i++) { // Loop through the length of the string
+ int coin = randombytes_uniform(2); // Decide if the current character will be capitalized or lowercase
+ if (coin == 1) {
+ a[i] = toupper(a[i]); // Change current character into a capital
+ }
+ }
+}
+
+void generate_integers(void) { // Generates a string of random integers
+ int length = 5; // TO-DO: Make random eventually
+ for (int i=0; i<length; i++) {
+ int random = randombytes_uniform(length); // Generate random number
+ printf("%d", random);
+ }
+}
+
+void generate_characters(char *set) {
+ for (int i = 0; i < 5; i++) {
+ int random_character = randombytes_uniform(strlen(set));
+ printf("%c", set[random_character]);
+ }
+}
+
+int main (int argc, char **argv) {
+ char c;
+ FILE *words;
+ long line_counter;
+ long line_total, r;
+ char str[20];
+
+ char character_set[28] = "!@#$%^&*()_-+={}[]|~`<,>.?:;"; // Character set used to generate random characters
+ if (sodium_init() < 0) return 1; // Returns an error if sodium isn't working correctly
+ words = fopen("word_list.txt", "r"); // Open text file
+
+
+ line_total = count_lines(words); // Count the total number of lines in the file
+ fseek(words, 0, SEEK_SET); // Reset pointer to beginning of file
+
+ char temp[2]; // Cannot copy c directly to str. needs a buffer
+ temp[1] = '\0'; // Makes the array a string
+
+ r = randombytes_uniform(line_total + 1); // Generates random line number to use for string
+ temp[1] = '\0'; // Makes the array a string
+
+ while ((c = getc(words)) != EOF) {
+ int i;
+ temp[0] = c; // Temporally move the current character into temp
+ if ((r == i) && (c != '\n')) { // If the current line is the designated word & it isn't the break character
+ strcat(str, temp); // Copy the character to the string
+ }
+ if (c == '\n') {
+ i++; // Increment the line counter
+ }
+ }
+
+ //** Passwords are generated by just printing outputs of different functions. **/
+ randomize_case(str);
+ generate_integers();
+ printf("%s", str);
+ generate_characters(character_set);
+
+ printf("\n");
+ fseek(words, 0, SEEK_SET); // Reset pointer to beginning of file
+ fclose(words);
+ /** printf("LINE TOTAL: %li\n", line_total); **/
+}
+