Last active
October 16, 2018 19:12
-
-
Save ethanmpeterson/60128b29723df326c1e68c0a766652a4 to your computer and use it in GitHub Desktop.
APSC 143 Lab 6 Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #define NUM 100 //Take 100 usernames | |
| #define MAXLEN 20 // Max username and password length | |
| // Function declarations | |
| int readDatabase(char *usrDB[], int pswrdDB[]); | |
| char *getUsername(); | |
| char *getPassword(); | |
| int searchUsername(char *username, char *usernameDB[], int size); | |
| int attemptLogin(int passwordDB[], int idx); | |
| int hashPassword(char *password); | |
| void deleteNewLine(char string[]); | |
| int userNum; | |
| int main(int argc, char **argv) | |
| { | |
| char *usernameDB[NUM]; // Array of pointers to strings. Will read usernames from file into this array | |
| int passwordDB[NUM]; // Array of ints. Will read password hashes from file into this array. | |
| int numUsers; // The number of users. | |
| char response[MAXLEN] = {}; // Generic array for user input. | |
| // Read in username and password | |
| // YOUR CODE GOES HERE | |
| // Infinite loop | |
| while(1){ | |
| int usrIdx; // Index of target username | |
| int loginStatus = 0; // Indicates whether passwords match | |
| char *username; // User specified username | |
| //----------------------------------------------------------- | |
| // YOUR CODE GOES HERE | |
| //----------------------------------------------------------- | |
| readDatabase(usernameDB, passwordDB); | |
| do { | |
| username = getUsername(); // grab username from user input | |
| usrIdx = searchUsername(username, usernameDB, userNum); // search the username DB for the given username and return the index of the match | |
| } while (usrIdx == -1); | |
| do { | |
| loginStatus = attemptLogin(passwordDB, usrIdx); // grab password input hash it and compare against hash value in the password db | |
| } while (!loginStatus); | |
| printf("Logged in.\n"); | |
| printf("Press enter to log out."); | |
| fgets(response, MAXLEN, stdin); | |
| printf("Logged out.\n\n"); | |
| printf("Log in to another account? (Y/N)"); | |
| fgets(response, MAXLEN, stdin); | |
| if((response[0] == 'N') || (response[0] == 'n')) | |
| break; | |
| } | |
| return 0; | |
| } | |
| // Function definitions | |
| void deleteNewLine(char string[]) { | |
| if (string[strlen(string) - 1] == '\n') { | |
| string[strlen(string) - 1] = '\0'; | |
| } | |
| } | |
| int readDatabase(char *usrDB[], int pswrdDB[]) { | |
| int numUsers = 0, numPasswords = 0; | |
| char userBuffer[MAXLEN]; | |
| FILE * usrFP; // declare usernames text file | |
| // open username file | |
| usrFP = fopen("/Users/ethanpeterson/Desktop/lab6/usernames.txt", "r"); // open usernames text file | |
| if (usrFP == NULL) { // check that file was successfully retreived | |
| printf("Could not open usernames file\n"); | |
| exit(1); | |
| } | |
| // read username file | |
| while(fgets(userBuffer, MAXLEN, usrFP)) { // go through the file and grab usernames | |
| numUsers++; | |
| deleteNewLine(userBuffer); // remove the newline character so it doesnt interfere with comparisons | |
| usrDB[numUsers] = (char *) malloc(strlen(userBuffer) + 1); // allocate memory for each string in the userDB array | |
| strcpy(usrDB[numUsers], userBuffer); // copy username input into the usernameDB | |
| } | |
| // close username file | |
| fclose(usrFP); | |
| // declare password file variable | |
| FILE * pswrdFP; | |
| pswrdFP = fopen("/Users/ethanpeterson/Desktop/lab6/passwords.txt", "r"); // open password file in read mode | |
| if (pswrdFP == NULL) { // check and report a failure to open the file | |
| printf("FAILED TO OPEN PASSWORD FILE\n"); | |
| exit(1); | |
| } | |
| // read password file | |
| while (!feof(pswrdFP)) { // while not at the end of the file scan in the passwords to the password db using fscanf | |
| fscanf(pswrdFP, "%d", &pswrdDB[numPasswords]); // take in integer with %d since only hash values are stored | |
| numPasswords++; // incriment to keep track while also making sure we are not overwriting the same spot in the array | |
| } | |
| // // close password file | |
| fclose(pswrdFP); | |
| // check number of entries the same | |
| if (numPasswords != numUsers) { | |
| printf("The Number of entries in each file does not match\n"); | |
| } | |
| // return number of entries read. | |
| userNum = numUsers; // store number of users globally outside this function | |
| return numPasswords; | |
| } | |
| char *getPassword() { | |
| char *password = (char *) malloc(MAXLEN); // reserve space in memory for the password input | |
| printf("Enter Password:"); | |
| fgets(password, MAXLEN, stdin); // grab password | |
| deleteNewLine(password); | |
| return password; | |
| } | |
| int searchUsername(char *username, char *usernameDB[], int size) { | |
| // replace with your code | |
| FILE * usernameFile; | |
| usernameFile = fopen("/Users/ethanpeterson/Desktop/lab6/usernames.txt", "r"); // open usernames file in read mode | |
| if (usernameFile == NULL) { // check that file was successfully opened | |
| printf("FAILED TO OPEN USER FILE\n"); | |
| exit(1); | |
| } | |
| for (int i = 0; i < size; i++) { | |
| char buffer[MAXLEN]; // where file input is stored | |
| fgets(buffer, MAXLEN, usernameFile); // grab the username in the file to be added to db | |
| usernameDB[i] = (char *) malloc(strlen(buffer) + 1); // allocate memory for a string in space i of usernameDB | |
| if (usernameDB[i] == NULL) { | |
| printf("ERROR\n"); | |
| exit(1); | |
| } | |
| deleteNewLine(buffer); | |
| strcpy(usernameDB[i], buffer); // copy each username from the file into usernameDB after removing newline | |
| } | |
| for (int j = 0; j <= size; j++) { // search the updated DB for the username passed in | |
| if (!strcmp(usernameDB[j], username)) { // if strcmp returns 0 the strings are the same then return j | |
| return j; // return index of the match | |
| } | |
| } | |
| printf("Username not Found.\n"); | |
| return -1; | |
| } | |
| char *getUsername() { // returns pointer to a string of username | |
| char *username = (char *) malloc(MAXLEN); // allocate RAM for username string | |
| printf("Enter Username:"); | |
| fgets(username, MAXLEN, stdin); | |
| deleteNewLine(username); | |
| return username; | |
| } | |
| int attemptLogin(int passwordDB[], int idx) { | |
| char *password = getPassword(); // prompt the user and get password input from them | |
| int asciiSum = 0; // holds sum of ascii values of each character to calculate the hash | |
| int n = 4891; | |
| int hash = 0; | |
| for (int i = 0; i < strlen(password); i++) { // calculate sum of ascii values | |
| asciiSum += password[i]; | |
| } | |
| asciiSum = asciiSum * asciiSum; | |
| hash = asciiSum % n; | |
| return passwordDB[idx] == hash; // returns 1 if the password db hash matches our calculated value and 0 if not | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment