Advertisement
Guest User

authentication system

a guest
Apr 22nd, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.79 KB | None | 0 0
  1. package authentication_systems;
  2.  
  3. import java.io.File;
  4. import java.nio.charset.Charset;
  5. import java.nio.file.Files;
  6. import java.nio.file.Paths;
  7. import java.security.MessageDigest;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Scanner;
  11.  
  12. public class AuthenticationSystems {
  13.  
  14.     public static void main(String args[]) {
  15.  
  16.         try {
  17.             Scanner scan = null;
  18.             scan = new Scanner(new File("credentials.txt"));
  19.             String credentials[][] = new String[100][4];
  20.             int count = 0;
  21.             while (scan.hasNextLine()) {
  22. //read name
  23.                 credentials[count][0] = scan.next();
  24.                 credentials[count][1] = scan.next();
  25.  
  26. //get original pass from file
  27.                 String l[] = scan.nextLine().split("\"[ ]+");
  28.                 l[0] = l[0].trim();
  29.                 l[0] = l[0].replace("\"", "");
  30.  
  31.                 credentials[count][2] = l[0];
  32.                 credentials[count][3] = l[0].trim();
  33.                 count++;
  34.             }
  35.  
  36. //ask for user input
  37.             Scanner scanio = new Scanner(System.in);
  38.             boolean RUN = true;
  39.             int tries = 0;
  40.  
  41.             while (RUN) {
  42.                 System.out.println("**WELCOME**");
  43.                 System.out.println("1) Login");
  44.                 System.out.println("2) Exit");
  45.  
  46.                 int ch = Integer.parseInt(scanio.nextLine().trim());
  47.  
  48.                 if (ch == 1) {
  49.  
  50. //increment number of attempts
  51.                     tries++;
  52.  
  53. //ask for username and password
  54.                     System.out.print("Input username:");
  55.                     String username = scanio.nextLine();
  56.                     System.out.print("Input password:");
  57.                     String password = scanio.nextLine();
  58.  
  59. //generate hash
  60.                     MessageDigest md;
  61.                     md = MessageDigest.getInstance("MD5");
  62.                     md.update(password.getBytes());
  63.                     byte[] digest = md.digest();
  64.                     StringBuilder sb = new StringBuilder();
  65.                     for (byte b : digest) {
  66.                         sb.append(String.format("%02x", b & 0xff));
  67.                     }
  68.                     String hPassword = sb.toString();
  69.  
  70.                     boolean badUser = true;
  71.                     for (int i = 0; i < count; i++) {
  72.                         if (username.contentEquals(credentials[i][0])) {
  73.                             if (hPassword.contentEquals(credentials[i][1])) {
  74. //if everything looks good. login
  75.                                 List<String> data = null;
  76.                                
  77.  
  78. //check type of user and print
  79.                                 switch (credentials[i][3]) {
  80.                                     case "zookeeper":
  81.                                         data = Files.readAllLines(Paths.get("zookeeper.txt"), Charset.defaultCharset());
  82.                                         break;
  83.                                     case "admin":
  84.                                         data = Files.readAllLines(Paths.get("admin.txt"), Charset.defaultCharset());
  85.                                         break;
  86.                                     case "veterinarian":
  87.                                         data = Files.readAllLines(Paths.get("veterinarian.txt"), Charset.defaultCharset());
  88.                                         break;
  89.                                     default:
  90.                                         break;
  91.                                 }
  92.  
  93.                                 for (String s : data) {
  94.                                     System.out.println(s);
  95.                                 }
  96.  
  97. //reset tries
  98.                                 tries = 0;
  99.  
  100. //asks user whether ot logout or exit
  101.                                 System.out.println("\n1) Logout.");
  102.                                 System.out.println("2) Exit.");
  103.  
  104.                                 ch = Integer.parseInt(scanio.nextLine().trim());
  105.                                 if (ch == 2) {
  106.                                     RUN = false;
  107.                                 }
  108.  
  109.                                 badUser = false;
  110.                                 break;
  111.                             }
  112.                         }
  113.                     }
  114.  
  115.                     if (badUser) {
  116.                         System.out.println("Invalid Username or password.");
  117.                     }
  118.  
  119.                 } else {
  120.                     RUN = false;
  121.                     break;
  122.                 }
  123.  
  124. //if too many invalid attemps, close the program
  125.                 if (tries == 3) {
  126.                     RUN = false;
  127.                     System.out.println("Too many invlaid attempts.");
  128.                 }
  129.  
  130.             }
  131.  
  132.         } catch (Exception ex) {
  133.             ex.printStackTrace();
  134.         }
  135.     }
  136.    
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement