Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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);
- 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; // Indicates whether passwords match
- char *username; // User specified username
- //-----------------------------------------------------------
- // YOUR CODE GOES HERE
- //-----------------------------------------------------------
- numUsers = readDatabase(usernameDB, passwordDB);
- do {
- printf("Enter username: ");
- username = getUsername();
- usrIdx = searchUsername(username, usernameDB, numUsers);
- free(username);
- if (usrIdx == -1) {
- printf("Username not found\n");
- }
- } while (usrIdx == -1);
- do {
- printf("Enter your password: ");
- // Get password, hash it and compare (inside function).
- loginStatus = attemptLogin(passwordDB, usrIdx);
- if (loginStatus == 0) {
- printf("Password does not match.\n");
- }
- } while (loginStatus == 0);
- for (int i = 0; i < numUsers; i++) {
- free(usernameDB[i]);
- }
- //
- 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
- int readDatabase(char *usrDB[], int pswrdDB[]) {
- int numUsers = 0, numPasswords = 0;
- char userBuffer[MAXLEN];
- // open username file
- FILE *usrFP = fopen("../usernames.txt", "rt");
- if (usrFP == NULL) {
- printf("Could not open username file.\n");
- exit(1);
- }
- // read username file
- while (fgets(userBuffer, MAXLEN, usrFP) && numUsers < NUM) {
- // Allocate memory for new username.
- usrDB[numUsers] = (char *) malloc(strlen(userBuffer));
- // Check if allocation was successful.
- if (usrDB[numUsers] == NULL) {
- printf("Error allocating memory.\n");
- exit(1);
- }
- // Removes new line character from userBuffer.
- userBuffer[strlen(userBuffer) - 1] = 0;
- // Stores buffer in user database.
- strcpy(usrDB[numUsers], userBuffer);
- numUsers++;
- }
- // close username file
- fclose(usrFP);
- // open password file
- FILE *pswrdFP = fopen("../passwords.txt", "rt");
- if (pswrdFP == NULL) {
- printf("Could not open password file.\n");
- exit(1);
- }
- // read password file
- while (!feof(pswrdFP)) {
- // Scan password and increment counter
- fscanf(pswrdFP, "%d", &pswrdDB[numPasswords++]);
- }
- // close password file
- fclose(pswrdFP);
- // check number of entries the same
- if (numUsers != numPasswords) {
- printf("Number of users and passwords do not match.\n");
- }
- // return number of entries read.
- return numUsers;
- }
- char *getUsername() {
- char buffer[MAXLEN];
- fgets(buffer, MAXLEN, stdin);
- char *username = malloc(strlen(buffer));
- buffer[strlen(buffer) - 1] = 0;
- strcpy(username, buffer);
- return username;
- }
- char *getPassword() {
- char buffer[MAXLEN];
- fgets(buffer, MAXLEN, stdin);
- char *password = malloc(strlen(buffer));
- buffer[strlen(buffer) - 1] = 0;
- strcpy(password, buffer);
- return password;
- }
- int searchUsername(char *username, char *usernameDB[], int size) {
- int i, idx = -1;
- for (i = 0; i < size; i++) {
- if (strcmp(username, usernameDB[i]) == 0) {
- idx = i;
- break;
- }
- }
- return idx;
- }
- int attemptLogin(int passwordDB[], int idx) {
- // get the password using the function get password
- char *password = getPassword();
- int n = 4891;
- int hash = 0;
- // hash the password
- for (int i = 0; i < strlen(password); i++) {
- hash += password[i];
- }
- free(password);
- hash = (hash * hash) % n;
- // check hash code and return 0 or 1 appropriately
- return hash == passwordDB[idx];
- }
Add Comment
Please, Sign In to add comment