Guest User

Untitled

a guest
Aug 16th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. package authentication;
  2. import java.security.MessageDigest;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6.  
  7. public class ValidateCredentials {
  8.  
  9. private boolean isValid;
  10. private String filePath;
  11. private String credentialsFileName;
  12.  
  13. public ValidateCredentials() {
  14. filePath = "";
  15.  
  16. isValid = false;
  17. credentialsFileName = "credentials";
  18. }
  19.  
  20. public boolean isCredentialsValid(String userName, String passWord) throws Exception {
  21. String original = passWord;
  22. MessageDigest md = MessageDigest.getInstance("MD5");
  23. md.update(original.getBytes());
  24. byte[] digest = md.digest();
  25. StringBuffer sb = new StringBuffer();
  26. for (byte b : digest) {
  27. sb.append(String.format("%02x", b & 0xff));
  28. }
  29.  
  30. System.out.println("");
  31. System.out.println("original:" + original);
  32. System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
  33.  
  34. isValid = readDataFiles(userName, sb.toString());
  35.  
  36. return isValid;
  37. }
  38.  
  39. public boolean readDataFiles(String userName, String passWord) throws IOException {
  40. FileInputStream fileByteStream1 = null; // File input stream
  41. FileInputStream fileByteStream2 = null; // File input stream
  42.  
  43. Scanner inFS1 = null; // Scanner object
  44. Scanner inFS2 = null; // Scanner object
  45.  
  46. String textLine = null;
  47. String textFileName = null;
  48.  
  49. boolean foundCredentials = false;
  50.  
  51. // Try to open file
  52. System.out.println("");
  53. System.out.println("Opening file " + credentialsFileName + ".txt");
  54. fileByteStream1 = new FileInputStream(filePath + "credentials.txt");
  55. inFS1 = new Scanner(fileByteStream1);
  56.  
  57. System.out.println("");
  58. System.out.println("Reading lines of text.");
  59.  
  60. while (inFS1.hasNextLine()) {
  61. textLine = inFS1.nextLine();
  62. //System.out.println(textLine);
  63.  
  64. String[] words = textLine.split("\s");//splits the string based on whitespace
  65.  
  66. if (words[0].equals(userName) && textLine.contains(passWord)) {
  67. foundCredentials = true;
  68. int last = words.length - 1;
  69. textFileName = words[last];
  70. break;
  71. }
  72. }
  73.  
  74. // Done with file, so try to close it
  75. System.out.println("");
  76. System.out.println("Closing file " + credentialsFileName + ".txt");
  77.  
  78. if (textLine != null) {
  79. fileByteStream1.close(); // close() may throw IOException if fails
  80. }
  81.  
  82. if (foundCredentials == true) {
  83. // Try to open file
  84. System.out.println("");
  85. System.out.println("Opening file " + textFileName + ".txt");
  86.  
  87. fileByteStream2 = new FileInputStream(filePath + textFileName + ".txt");
  88. inFS2 = new Scanner(fileByteStream2);
  89.  
  90. System.out.println("");
  91.  
  92. while (inFS2.hasNextLine()) {
  93. textLine = inFS2.nextLine();
  94. System.out.println(textLine);
  95. }
  96.  
  97. // Done with file, so try to close it
  98. System.out.println("");
  99. System.out.println("Closing file " + textFileName + ".txt");
  100.  
  101. if (textLine != null) {
  102. fileByteStream2.close(); // close() may throw IOException if fails
  103. }
  104. }
  105.  
  106. return foundCredentials;
  107. }
  108.  
  109. }
  110.  
  111. package authentication;
  112.  
  113. import java.io.File;
  114. import java.io.IOException;
  115. import java.nio.charset.Charset;
  116. import java.nio.file.Files;
  117. import java.nio.file.Paths;
  118. import java.security.MessageDigest;
  119. import java.security.NoSuchAlgorithmException;
  120. import java.util.List;
  121. import java.util.Scanner;
  122.  
  123.  
  124. public class Authentication {
  125. public static void main(String args[]) {
  126. try {
  127. Scanner inputCreds;
  128. inputCreds = new Scanner(new File("src\authentication\credentials.txt"));
  129. String userCredentials[][] = new String[100][4];
  130. int count = 0;
  131.  
  132. while (inputCreds.hasNextLine()) {
  133. userCredentials[count][0] = inputCreds.next();
  134. userCredentials[count][1] = inputCreds.next();
  135.  
  136. String l[] = inputCreds.nextLine().split(""[ ]+");
  137. l[0] = l[0].trim();
  138. l[0] = l[0].replace(""", "");
  139. userCredentials[count][2] = l[0];
  140. userCredentials[count][3] = l[0].trim();
  141. count++;
  142. }
  143.  
  144. Scanner input = new Scanner(System.in);
  145. boolean RUN = true;
  146. int tries = 0;
  147.  
  148. while (RUN) {
  149. System.out.println("Welcome to your Zoo Account Authorization Screen.");
  150. System.out.println("Please select your option:");
  151. System.out.println("(1) - Log In.");
  152. System.out.println("(2) - Exit Program.");
  153.  
  154. int ch = Integer.parseInt(input.nextLine().trim());
  155.  
  156. if (ch == 1) {
  157. tries++;
  158. System.out.print("Username: ");
  159. String userName = input.nextLine();
  160. System.out.print("Password: ");
  161. String passWord = input.nextLine();
  162.  
  163. MessageDigest md;
  164. md = MessageDigest.getInstance("MD5");
  165. md.update(passWord.getBytes());
  166. byte[] digest = md.digest();
  167. StringBuilder sb = new StringBuilder();
  168. for (byte b : digest) {
  169. sb.append(String.format("%02x", b & 0xff));
  170. }
  171. String hPassword = sb.toString();
  172.  
  173. boolean badCreds = true;
  174. for (int i = 0; i < count; i++) {
  175. if (userName.contentEquals(userCredentials[i][0])) {
  176. if (hPassword.contentEquals(userCredentials[i][1])) {
  177. List<String> data = null;
  178.  
  179. switch (userCredentials[i][3]) {
  180. case "zookeeper":
  181. data = Files.readAllLines(Paths.get("zookeeper.txt"), Charset.defaultCharset());
  182. break;
  183. case "admin":
  184. data = Files.readAllLines(Paths.get("admin.txt"), Charset.defaultCharset());
  185. break;
  186. case "veterinarian":
  187. data = Files.readAllLines(Paths.get("veterinarian.txt"), Charset.defaultCharset());
  188. break;
  189. default:
  190. break;
  191. }
  192. if (data != null) {
  193. for (String s : data) {
  194. System.out.println(s);
  195. }
  196. }
  197. tries = 0;
  198.  
  199. System.out.println("n1) Logout.");
  200. System.out.println("2) Exit.");
  201.  
  202. ch = Integer.parseInt(input.nextLine().trim());
  203. if (ch == 2) {
  204. RUN = false;
  205. }
  206. badCreds = false;
  207. break;
  208. }
  209. }
  210. }
  211. if (badCreds) {
  212. System.out.println("Invalid Username or password.");
  213. }
  214. } else {
  215. break;
  216. }
  217. if (tries == 3) {
  218. RUN = false;
  219. System.out.println("Too many incorrect attempts!");
  220. }
  221. }
  222. } catch (IOException | NumberFormatException | NoSuchAlgorithmException ex) {
  223. }
  224. }
  225. }
Add Comment
Please, Sign In to add comment