Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. /*
  2. Alice aNicePasswrd a?m?g?a?z??f
  3. Bob aCoolPasswrd a???jxa?z??f
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11.  
  12. typedef struct Users {
  13. char Id[12];
  14. char hashPW[32];
  15. } User;
  16.  
  17. // Prototypes
  18. User * user;
  19. void passwordHasher(char *aPW, char *aHashedPw);
  20. void E(char *in, char *out);
  21. int passwordCompare(char *pw1, char *pw2);
  22. void UserPrompts();
  23. void filePopulate();
  24. void passwdPadder(char *aString);
  25. void usrNameValidity(char *userIdTrial);
  26. void passwordChecker(char *apw);
  27. void userCreation();
  28. void fileRepop(); //writes to file after any changes
  29. void PasswordCreation(int location);
  30. static FILE *userFile;
  31. static FILE *hashFile;
  32.  
  33.  
  34. int main()
  35. {
  36. user = malloc(sizeof(User) * 60);
  37. filePopulate(); // read and populate user array
  38. UserPrompts(); //Initial User Interaction
  39. printf("Goodbye");
  40. }
  41.  
  42. void filePopulate()
  43. {
  44. // Create and open the file, we need rw access since we may have to write to it
  45. userFile = fopen("users.txt","rw");
  46. hashFile = fopen("hash.txt","rw");
  47. int i = 0;
  48.  
  49. // Go through the file and populate the array for userIDs
  50. while(fscanf(userFile, "%s", user[i].Id) != EOF)
  51. ++i;
  52.  
  53. i = 0; // reset i for another use
  54.  
  55. // Obviously in this case the hashPWs are ordered in the same order the userIDs are ordered
  56. while(fscanf(hashFile, "%s", user[i].hashPW) != EOF)
  57. ++i;
  58. }
  59.  
  60.  
  61. void UserPrompts()
  62. {
  63. char enteredId[32];
  64. char enteredPw[12];
  65. char hashedPW[32];
  66. int n = 5;
  67. int attempts = 0;
  68. int size = 60;
  69. // Get user input
  70. printf("Input User ID: ");
  71. fgets(enteredId, 32, stdin);
  72. usrNameValidity(enteredId);
  73.  
  74. // Clean out new input so it matches perfectly with what's in the array
  75. enteredId[strcspn(enteredId, "\r\n")] = '\0';
  76. //User = malloc(sizeof(User) * size); // Allows up to 60 users (fixed size)
  77. for (int i=0;i<size;i++)
  78. {
  79. if (strcmp(enteredId,user[i].Id) == 0) //strcmp(pw1,pw2) == 0 //
  80. {
  81. int location = i;
  82. printf("ID found\n");
  83. PasswordCreation(location);
  84. }
  85. }
  86. printf("INVALID, ID NOT FOUND! ");
  87. userCreation();
  88.  
  89. }
  90. void PasswordCreation(int location)
  91. {
  92. int n = 5;
  93. int i = location;
  94. int attempts = 0;
  95. int size = 60;
  96. char enteredId[32];
  97. char enteredPw [13];
  98. //char hashedPW[32];
  99. char ahashedPW[13];
  100. //while(attempts < n)
  101. //{
  102. printf("\n Enter password: ");
  103. fgets(enteredPw,13,stdin); // might need the same fix as above
  104. //passwordChecker(enteredPw);
  105. passwordHasher(enteredPw, ahashedPW);
  106. printf("%s", ahashedPW);
  107. printf("\n%s", user[i].hashPW);
  108. if (!(strcmp(ahashedPW, user[i].hashPW) == 0))
  109. {
  110.  
  111. printf("\nEnter New Password: ");
  112. fgets(enteredPw,12,stdin);
  113. passwordChecker(enteredPw);
  114. passwordHasher(enteredPw, ahashedPW);
  115. strcpy(user[i].hashPW, ahashedPW);
  116. fileRepop();
  117. printf("your password has been changed");
  118. attempts = n;
  119. }
  120. attempts ++;
  121. //}
  122.  
  123. }
  124.  
  125. void userCreation()
  126. {
  127. char enteredId[32];
  128. char enteredPw[12];
  129.  
  130. hashFile = fopen("hash.txt","w");
  131.  
  132. //Get input
  133. printf("\nCreate new ID: ");
  134. fgets(enteredId, 32, stdin);
  135. //fprintf(userFile, "\n%s", entered
  136. printf("\nCreate new password: ");
  137. fgets(enteredPw, 12, stdin);
  138. fprintf(userFile, "\n%s", enteredId);
  139. // enteredPW now goes through hash and hash is stored in hashpw file
  140. char *temp = malloc(sizeof(char *) * 12);
  141. passwordHasher(enteredPw, temp);
  142. fprintf(hashFile, "\n%s", temp);
  143. //UserPrompts();
  144. }
  145.  
  146.  
  147. void E(char *in, char *out)
  148. {
  149. out[0]=(in[0]&0x80)^(((in[0]>>1)&0x7F)^((in[0])&0x7F));
  150. out[1]=((in[1]&0x80)^((in[0]<<7)&0x80))^(((in[1]>>1)&0x7F)^((in[1])&0x7F));
  151. out[2]=((in[2]&0x80)^((in[1]<<7)&0x80))^(((in[2]>>1)&0x7F)^((in[2])&0x7F));
  152. out[3]=((in[3]&0x80)^((in[2]<<7)&0x80))^(((in[3]>>1)&0x7F)^((in[3])&0x7F));
  153. }
  154.  
  155. //Takes a string and returns a "E()"ed string
  156. void passwordHasher(char *aPW, char *aHashedPw)
  157. {
  158. char *hashedPw = malloc(sizeof(char) * 12);
  159. char *input = aPW;
  160. //blocks and their hashed space allocation
  161. char *b1 = malloc(4);
  162. char *b2 = malloc(4);
  163. char *b3 = malloc(4);
  164. char *h1 = malloc(4);
  165. char *h2 = malloc(4);
  166. char *h3 = malloc(4);
  167.  
  168. //Uppercasing input
  169. //passwdPadder(input);
  170.  
  171. for (int i= 0; i <strlen(input); i++)
  172. input[i] = toupper(input[i]);
  173.  
  174. //defining contents of blocks
  175. strncpy(b1, input, 4);
  176. strncpy(b2, input+4, 4);
  177. strncpy(b3, input+8, 4);
  178. //defining end of string
  179. b1[4]='\0';
  180. b2[4]='\0';
  181. b3[4]='\0';
  182. //hashing(E()ing) each block using e
  183. E(b1, h1);
  184. h1[4]='\0';
  185. strcat(hashedPw, h1);
  186. E(b2, h2);
  187. h2[4]='\0';
  188. strcat(hashedPw, h2);
  189. E(b3, h3);
  190. h3[4]='\0';
  191. strcat(hashedPw, h3);
  192. strcpy(aHashedPw, hashedPw);
  193. }
  194.  
  195. void passwdPadder(char *aString)
  196. {
  197. int size = (int)strlen(aString);
  198. //pads passwords shorter than 12 characters with empty space chaaracter
  199. for (int i = size; i <12 ; i++)
  200. aString[i]=32;
  201. }
  202.  
  203. void usrNameValidity(char *userIdTrial)
  204. {
  205. char usr[32];
  206. strcpy(usr, userIdTrial);
  207. int idSize = (int)strlen(usr);
  208.  
  209. if (idSize > 3 && idSize <33)
  210. return;
  211. else
  212. {
  213. printf("INVALID, ID NOT FOUND! \nCreate New ID: ");
  214. UserPrompts();
  215.  
  216. }
  217. }
  218.  
  219. //• Passwords must include upper and lower case letters and numbers only,
  220. //i.e. no special characters
  221. void passwordChecker(char * apw)
  222. {
  223. char pw[12];
  224. //if value anything other than 0, it meants there is an invalid character in string
  225. int test = 0;
  226. strcpy(pw, apw);
  227. int size = (int)strlen(pw);
  228. if (size > 12)
  229. {
  230. printf("Password is too long, Try again\n");
  231. UserPrompts();
  232. }
  233. for (int i = 0; i <size; i++)
  234. {
  235. if ((pw[i] <48) || (pw[i] > 57 && pw[i] < 65) || (pw[i] > 90 && pw[i] < 96))
  236. test ++;
  237. }
  238. if (test != 0)
  239. {
  240. printf("invalid password, use only numbers and letters \n Any characters 12 will not count");
  241. UserPrompts();
  242. }
  243. }
  244.  
  245. void fileRepop()
  246. {
  247. userFile = fopen("users.txt","w+");
  248. hashFile = fopen("hash.txt","w+");
  249. for (int i = 0; i < 60; i++)
  250. fprintf(userFile, "%s\n", user[i].Id);
  251.  
  252. for (int i = 0; i < 60; i++){
  253. fprintf(hashFile, "%s\n", user[i].hashPW);
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement