alien_fx_fiend

Database in C code

Oct 11th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define NUM 100 //Take 100 usernames
  6. #define MAXLEN 20 // Max username and password length
  7.  
  8. // Function declarations
  9. int readDatabase(char *usrDB[], int pswrdDB[]);
  10.  
  11. char *getUsername();
  12.  
  13. char *getPassword();
  14.  
  15. int searchUsername(char *username, char *usernameDB[], int size);
  16.  
  17. int attemptLogin(int passwordDB[], int idx);
  18.  
  19. int hashPassword(char *password);
  20.  
  21. int main(int argc, char **argv) {
  22.     char *usernameDB[NUM];        // Array of pointers to strings. Will read usernames from file into this array
  23.     int passwordDB[NUM];        // Array of ints. Will read password hashes from file into this array.
  24.     int numUsers;                // The number of users.
  25.     char response[MAXLEN] = {};    // Generic array for user input.
  26.  
  27.     // Read in username and password
  28.     // YOUR CODE GOES HERE
  29.  
  30.     // Infinite loop
  31.     while (1) {
  32.         int usrIdx;            // Index of target username
  33.         int loginStatus;    // Indicates whether passwords match
  34.         char *username;        // User specified username
  35.  
  36.         //-----------------------------------------------------------
  37.         // YOUR CODE GOES HERE
  38.         //-----------------------------------------------------------
  39.         numUsers = readDatabase(usernameDB, passwordDB);
  40.  
  41.         do {
  42.             printf("Enter username: ");
  43.             username = getUsername();
  44.             usrIdx = searchUsername(username, usernameDB, numUsers);
  45.             free(username);
  46.             if (usrIdx == -1) {
  47.                 printf("Username not found\n");
  48.             }
  49.         } while (usrIdx == -1);
  50.  
  51.         do {
  52.             printf("Enter your password: ");
  53.             // Get password, hash it and compare (inside function).
  54.             loginStatus = attemptLogin(passwordDB, usrIdx);
  55.             if (loginStatus == 0) {
  56.                 printf("Password does not match.\n");
  57.             }
  58.         } while (loginStatus == 0);
  59.         for (int i = 0; i < numUsers; i++) {
  60.             free(usernameDB[i]);
  61.         }
  62.         //
  63.         printf("Logged in.\n");
  64.         printf("Press enter to log out.");
  65.         fgets(response, MAXLEN, stdin);
  66.         printf("Logged out.\n\n");
  67.  
  68.         printf("Log in to another account? (Y/N)");
  69.         fgets(response, MAXLEN, stdin);
  70.         if ((response[0] == 'N') || (response[0] == 'n'))
  71.             break;
  72.     }
  73.  
  74.     return 0;
  75. }
  76.  
  77. // Function definitions
  78.  
  79. int readDatabase(char *usrDB[], int pswrdDB[]) {
  80.     int numUsers = 0, numPasswords = 0;
  81.     char userBuffer[MAXLEN];
  82.     // open username file
  83.     FILE *usrFP = fopen("../usernames.txt", "rt");
  84.     if (usrFP == NULL) {
  85.         printf("Could not open username file.\n");
  86.         exit(1);
  87.     }
  88.     // read username file
  89.     while (fgets(userBuffer, MAXLEN, usrFP) && numUsers < NUM) {
  90.         // Allocate memory for new username.
  91.         usrDB[numUsers] = (char *) malloc(strlen(userBuffer));
  92.         // Check if allocation was successful.
  93.         if (usrDB[numUsers] == NULL) {
  94.             printf("Error allocating memory.\n");
  95.             exit(1);
  96.         }
  97.         // Removes new line character from userBuffer.
  98.         userBuffer[strlen(userBuffer) - 1] = 0;
  99.         // Stores buffer in user database.
  100.         strcpy(usrDB[numUsers], userBuffer);
  101.         numUsers++;
  102.     }
  103.     // close username file
  104.     fclose(usrFP);
  105.  
  106.     // open password file
  107.     FILE *pswrdFP = fopen("../passwords.txt", "rt");
  108.     if (pswrdFP == NULL) {
  109.         printf("Could not open password file.\n");
  110.         exit(1);
  111.     }
  112.     // read password file
  113.     while (!feof(pswrdFP)) {
  114.         // Scan password and increment counter
  115.         fscanf(pswrdFP, "%d", &pswrdDB[numPasswords++]);
  116.     }
  117.     // close password file
  118.     fclose(pswrdFP);
  119.  
  120.     // check number of entries the same
  121.     if (numUsers != numPasswords) {
  122.         printf("Number of users and passwords do not match.\n");
  123.     }
  124.  
  125.     // return number of entries read.
  126.     return numUsers;
  127. }
  128.  
  129. char *getUsername() {
  130.     char buffer[MAXLEN];
  131.     fgets(buffer, MAXLEN, stdin);
  132.     char *username = malloc(strlen(buffer));
  133.     buffer[strlen(buffer) - 1] = 0;
  134.     strcpy(username, buffer);
  135.     return username;
  136. }
  137.  
  138. char *getPassword() {
  139.     char buffer[MAXLEN];
  140.     fgets(buffer, MAXLEN, stdin);
  141.     char *password = malloc(strlen(buffer));
  142.     buffer[strlen(buffer) - 1] = 0;
  143.     strcpy(password, buffer);
  144.     return password;
  145. }
  146.  
  147. int searchUsername(char *username, char *usernameDB[], int size) {
  148.     int i, idx = -1;
  149.     for (i = 0; i < size; i++) {
  150.         if (strcmp(username, usernameDB[i]) == 0) {
  151.             idx = i;
  152.             break;
  153.         }
  154.     }
  155.     return idx;
  156. }
  157.  
  158. int attemptLogin(int passwordDB[], int idx) {
  159.     // get the password using the function get password
  160.     char *password = getPassword();
  161.     int n = 4891;
  162.     int hash = 0;
  163.     // hash the password
  164.     for (int i = 0; i < strlen(password); i++) {
  165.         hash += password[i];
  166.     }
  167.     free(password);
  168.     hash = (hash * hash) % n;
  169.     // check hash code and return 0 or 1 appropriately
  170.     return hash == passwordDB[idx];
  171. }
Add Comment
Please, Sign In to add comment