Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Alice aNicePasswrd a?m?g?a?z??f
- Bob aCoolPasswrd a???jxa?z??f
- */
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- typedef struct Users {
- char Id[12];
- char hashPW[32];
- } User;
- // Prototypes
- User * user;
- void passwordHasher(char *aPW, char *aHashedPw);
- void E(char *in, char *out);
- int passwordCompare(char *pw1, char *pw2);
- void UserPrompts();
- void filePopulate();
- void passwdPadder(char *aString);
- void usrNameValidity(char *userIdTrial);
- void passwordChecker(char *apw);
- void userCreation();
- void fileRepop(); //writes to file after any changes
- void PasswordCreation(int location);
- static FILE *userFile;
- static FILE *hashFile;
- int main()
- {
- user = malloc(sizeof(User) * 60);
- filePopulate(); // read and populate user array
- UserPrompts(); //Initial User Interaction
- printf("Goodbye");
- }
- void filePopulate()
- {
- // Create and open the file, we need rw access since we may have to write to it
- userFile = fopen("users.txt","rw");
- hashFile = fopen("hash.txt","rw");
- int i = 0;
- // Go through the file and populate the array for userIDs
- while(fscanf(userFile, "%s", user[i].Id) != EOF)
- ++i;
- i = 0; // reset i for another use
- // Obviously in this case the hashPWs are ordered in the same order the userIDs are ordered
- while(fscanf(hashFile, "%s", user[i].hashPW) != EOF)
- ++i;
- }
- void UserPrompts()
- {
- char enteredId[32];
- char enteredPw[12];
- char hashedPW[32];
- int n = 5;
- int attempts = 0;
- int size = 60;
- // Get user input
- printf("Input User ID: ");
- fgets(enteredId, 32, stdin);
- usrNameValidity(enteredId);
- // Clean out new input so it matches perfectly with what's in the array
- enteredId[strcspn(enteredId, "\r\n")] = '\0';
- //User = malloc(sizeof(User) * size); // Allows up to 60 users (fixed size)
- for (int i=0;i<size;i++)
- {
- if (strcmp(enteredId,user[i].Id) == 0) //strcmp(pw1,pw2) == 0 //
- {
- int location = i;
- printf("ID found\n");
- PasswordCreation(location);
- }
- }
- printf("INVALID, ID NOT FOUND! ");
- userCreation();
- }
- void PasswordCreation(int location)
- {
- int n = 5;
- int i = location;
- int attempts = 0;
- int size = 60;
- char enteredId[32];
- char enteredPw [13];
- //char hashedPW[32];
- char ahashedPW[13];
- //while(attempts < n)
- //{
- printf("\n Enter password: ");
- fgets(enteredPw,13,stdin); // might need the same fix as above
- //passwordChecker(enteredPw);
- passwordHasher(enteredPw, ahashedPW);
- printf("%s", ahashedPW);
- printf("\n%s", user[i].hashPW);
- if (!(strcmp(ahashedPW, user[i].hashPW) == 0))
- {
- printf("\nEnter New Password: ");
- fgets(enteredPw,12,stdin);
- passwordChecker(enteredPw);
- passwordHasher(enteredPw, ahashedPW);
- strcpy(user[i].hashPW, ahashedPW);
- fileRepop();
- printf("your password has been changed");
- attempts = n;
- }
- attempts ++;
- //}
- }
- void userCreation()
- {
- char enteredId[32];
- char enteredPw[12];
- hashFile = fopen("hash.txt","w");
- //Get input
- printf("\nCreate new ID: ");
- fgets(enteredId, 32, stdin);
- //fprintf(userFile, "\n%s", entered
- printf("\nCreate new password: ");
- fgets(enteredPw, 12, stdin);
- fprintf(userFile, "\n%s", enteredId);
- // enteredPW now goes through hash and hash is stored in hashpw file
- char *temp = malloc(sizeof(char *) * 12);
- passwordHasher(enteredPw, temp);
- fprintf(hashFile, "\n%s", temp);
- //UserPrompts();
- }
- void E(char *in, char *out)
- {
- out[0]=(in[0]&0x80)^(((in[0]>>1)&0x7F)^((in[0])&0x7F));
- out[1]=((in[1]&0x80)^((in[0]<<7)&0x80))^(((in[1]>>1)&0x7F)^((in[1])&0x7F));
- out[2]=((in[2]&0x80)^((in[1]<<7)&0x80))^(((in[2]>>1)&0x7F)^((in[2])&0x7F));
- out[3]=((in[3]&0x80)^((in[2]<<7)&0x80))^(((in[3]>>1)&0x7F)^((in[3])&0x7F));
- }
- //Takes a string and returns a "E()"ed string
- void passwordHasher(char *aPW, char *aHashedPw)
- {
- char *hashedPw = malloc(sizeof(char) * 12);
- char *input = aPW;
- //blocks and their hashed space allocation
- char *b1 = malloc(4);
- char *b2 = malloc(4);
- char *b3 = malloc(4);
- char *h1 = malloc(4);
- char *h2 = malloc(4);
- char *h3 = malloc(4);
- //Uppercasing input
- //passwdPadder(input);
- for (int i= 0; i <strlen(input); i++)
- input[i] = toupper(input[i]);
- //defining contents of blocks
- strncpy(b1, input, 4);
- strncpy(b2, input+4, 4);
- strncpy(b3, input+8, 4);
- //defining end of string
- b1[4]='\0';
- b2[4]='\0';
- b3[4]='\0';
- //hashing(E()ing) each block using e
- E(b1, h1);
- h1[4]='\0';
- strcat(hashedPw, h1);
- E(b2, h2);
- h2[4]='\0';
- strcat(hashedPw, h2);
- E(b3, h3);
- h3[4]='\0';
- strcat(hashedPw, h3);
- strcpy(aHashedPw, hashedPw);
- }
- void passwdPadder(char *aString)
- {
- int size = (int)strlen(aString);
- //pads passwords shorter than 12 characters with empty space chaaracter
- for (int i = size; i <12 ; i++)
- aString[i]=32;
- }
- void usrNameValidity(char *userIdTrial)
- {
- char usr[32];
- strcpy(usr, userIdTrial);
- int idSize = (int)strlen(usr);
- if (idSize > 3 && idSize <33)
- return;
- else
- {
- printf("INVALID, ID NOT FOUND! \nCreate New ID: ");
- UserPrompts();
- }
- }
- //• Passwords must include upper and lower case letters and numbers only,
- //i.e. no special characters
- void passwordChecker(char * apw)
- {
- char pw[12];
- //if value anything other than 0, it meants there is an invalid character in string
- int test = 0;
- strcpy(pw, apw);
- int size = (int)strlen(pw);
- if (size > 12)
- {
- printf("Password is too long, Try again\n");
- UserPrompts();
- }
- for (int i = 0; i <size; i++)
- {
- if ((pw[i] <48) || (pw[i] > 57 && pw[i] < 65) || (pw[i] > 90 && pw[i] < 96))
- test ++;
- }
- if (test != 0)
- {
- printf("invalid password, use only numbers and letters \n Any characters 12 will not count");
- UserPrompts();
- }
- }
- void fileRepop()
- {
- userFile = fopen("users.txt","w+");
- hashFile = fopen("hash.txt","w+");
- for (int i = 0; i < 60; i++)
- fprintf(userFile, "%s\n", user[i].Id);
- for (int i = 0; i < 60; i++){
- fprintf(hashFile, "%s\n", user[i].hashPW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement