Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #include <ctype.h>
- typedef struct Users {
- char Id[12];
- char hashPW[5];
- } User;
- User *user;
- void filePopulate();
- unsigned int rand32Gen();
- char * createXOR();
- void writeFile();
- void R();
- void E(char *in, char *out);
- char * Hashify(int n, char * password);
- void E(char *in, char *out)
- {
- //make the input characters uppercase.
- char c0 = toupper(in[0]);
- char c1 = toupper(in[1]);
- char c2 = toupper(in[2]);
- char c3 = toupper(in[3]);
- out[0] = (c0 & 0x80) ^ (((c0 >> 1) & 0x7F) ^ ((c0) & 0x7F));
- out[1] = ((c1 & 0x80) ^ ((c0 << 7) & 0x80)) ^ (((c1 >> 1) & 0x7F) ^ ((c1) & 0x7F));
- out[2] = ((c2 & 0x80) ^ ((c1 << 7) & 0x80)) ^ (((c2 >> 1) & 0x7F) ^ ((c2) & 0x7F));
- out[3] = ((c3 & 0x80) ^ ((c2 << 7) & 0x80)) ^ (((c3 >> 1) & 0x7F) ^ ((c3) & 0x7F));
- }
- char * Hashify(int n, char * password){
- //allocate a string that is initially cleared
- char * hash = (char*)calloc(12, sizeof(char));
- //if the allocation fails print error message
- if (hash == NULL)
- {
- printf("calloc returned NULL in hashify\n");
- return hash;
- }
- int i;
- //iterate through every 4 byte chunk and perform the hashing function
- for (i = 0; i < n; i++)
- {
- E(&password[4*i], &hash[4*i]);
- }
- return hash;
- }
- int main(int argc, const char * argv[])
- {
- unsigned int uniqueNum;
- char enteredID[5];
- char enteredPass[5];
- user = (User*)calloc(100, sizeof(int));
- int Id=-1;
- int attempts=5;
- strcpy(user[0].Id,"user1");
- strcpy(user[1].Id,"user2");
- strcpy(user[2].Id,"user3");
- strcpy(user[3].Id,"user4");
- strcpy(user[0].hashPW,Hashify(1,"pass1"));
- strcpy(user[1].hashPW,Hashify(1,"pass2"));
- strcpy(user[2].hashPW,Hashify(1,"pass3"));
- strcpy(user[3].hashPW,Hashify(1,"pass4"));
- writeFile();
- uniqueNum = rand32Gen();
- while(1){
- printf("Please enter an already existing User ID: ");
- fgets(enteredID, 32, stdin);
- int i;
- for(i=0;i<4;i++)
- {
- if (Id>0) break;
- else
- if(strncmp(enteredID,user[i].Id,5)==0)
- {
- Id=i;
- printf("User ID is in the table! \n");
- while(1)
- {
- if(attempts==0)
- {
- printf("MAX attempts reached EXITING\n");
- return 0;
- }
- printf("Please enter a password for %s : ",user[i].Id);
- fgets(enteredPass, 32, stdin);
- //ask for a password for the username that was typed above then
- char * hashpass=Hashify(1,enteredPass);
- char * userxor = createXOR(hashpass,uniqueNum);
- char * tablexor = createXOR(user[Id].hashPW,uniqueNum);
- // then user strcmp to check userxor is equal to tablexor if they are then access granted
- int token = strncmp(userxor,tablexor,5);
- if(token == 0)
- {
- printf("Access GRANTED \n");
- return 0;
- }
- else
- {
- printf("Access DENIED!\n");
- attempts--;
- }
- }
- }
- }
- printf("User not in table , enter an existing user!\n");
- continue;
- }
- }
- void filePopulate()
- {
- // Create and open the file, we need rw access since we may have to write to it
- FILE *userFile;
- FILE *hashFile;
- userFile = fopen("users.txt","r");
- hashFile = fopen("hash.txt","r");
- int i = 0;
- // Go through the file and populate the array for userIDs
- while(fscanf(userFile, "%s", user[i].Id) != EOF)
- ++i;
- i = 0;
- // 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;
- fclose(userFile);
- fclose(hashFile);
- }
- void writeFile()
- {
- FILE *userFile;
- FILE *hashFile;
- // printf("I ran\n");
- // Open both files for writing
- if ((userFile = fopen("user.txt", "w+")) == NULL)
- printf("Couldn't create file (userFile.txt)\n");
- if ((hashFile = fopen("hash.txt", "w+")) == NULL)
- printf("Couldn't create file (hashFile.txt)\n");
- // Write to user file and hash file
- for (int i = 0; i < 5; i++)
- fprintf(userFile, "%s\n", user[i].Id);
- for (int i = 0; i < 5; i++)
- fprintf(hashFile, "%s\n", user[i].hashPW);
- //printf("File written.\n");
- fclose(userFile);
- fclose(hashFile);
- }
- unsigned int rand32Gen()
- {
- unsigned int x;
- srand(time(NULL)); // seed to create a true random number
- x = rand() & 0xff;
- x |= (rand() & 0xff) << 8;
- x |= (rand() & 0xff) << 16;
- x |= (rand() & 0xff) << 24;
- return (x);
- }
- char * createXOR(char* hash, unsigned int r)
- {
- int MAX_PASSWORD_LENGTH = 12;
- char * out = (char*)calloc(MAX_PASSWORD_LENGTH, sizeof(char));
- R(hash, out, r);
- R(&hash[4], &out[4], r);
- R(&hash[8], &out[8], r);
- return out;
- }
- //xor function for each bundle (4 bytes)
- void R(char* in, char* out, unsigned int r)// r is the unique number
- {
- out[3] = in[3] ^ (r & 255); //xor the first byte with r's first byte
- r = r >> 8; //shift r by one byte
- out[2] = in[2] ^ (r & 255);
- r = r >> 8;
- out[1] = in[1] ^ (r & 255);
- r = r >> 8;
- out[0] = in[0] ^ (r & 255);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement