Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. package auth.system;
  2.  
  3. /**
  4.  *
  5.  * @author Patrick
  6.  */
  7. import java.security.MessageDigest;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.util.Scanner;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import java.io.FileInputStream;
  13. import java.io.FileNotFoundException;
  14. import java.io.IOException;
  15. import java.nio.file.Files;
  16. import java.nio.file.Paths;
  17.  
  18.  
  19.  
  20. public class AuthSystem {
  21.  
  22.     /**
  23.      * @param args the command line arguments
  24.      * @throws java.io.FileNotFoundException
  25.      */
  26.     public static void main(String[] args) throws FileNotFoundException, IOException {
  27.         Scanner input = new Scanner(System.in);
  28.         int attempts = 0;
  29.         String username = "";
  30.         String password = "";
  31.         FileInputStream fileByteStream = null;
  32.         Scanner inFS = null;
  33.         String credString;
  34.  
  35.        
  36.         // Loop to keep failed attempts under 3
  37.         while (attempts < 3){
  38.             try {
  39.                 System.out.print("Enter Username(\"exit\" to logout): ");
  40.                 username = input.nextLine();
  41.                 // This if statement gives the user a way to logout
  42.                 if(username.contains("exit")){
  43.                     return;
  44.                 }
  45.                 System.out.print("Enter Password: ");
  46.                 password = input.nextLine();
  47.                 //import creds file
  48.                 fileByteStream = new FileInputStream("creds.text");
  49.                 inFS = new Scanner(fileByteStream);
  50.                 credString = "blank";
  51.                
  52.                
  53.                 //convert plaintext password from user to md5hash
  54.                 String original = password;  
  55.                 MessageDigest md = MessageDigest.getInstance("MD5");
  56.                 md.update(original.getBytes());
  57.                 byte[] digest = md.digest();
  58.                 StringBuffer sb = new StringBuffer();
  59.                 for (byte b : digest) {
  60.                     sb.append(String.format("%02x", b & 0xff));
  61.                 }
  62.                
  63.  
  64.                 //loop only while the inFS, which is reading the file line by line, is not null.
  65.                 //This way it exits the loop onces it reaches the end of the file.
  66.             while(inFS.hasNextLine()){
  67.                 //kicks the loop out if the user fails more than 3 times    
  68.                 if(attempts >= 3){
  69.                     System.out.print("Login failure limit reached. Please contact an administrator to unlock your account.");
  70.                     return;
  71.                     }
  72.                 //Reading the next line in the file and splitting it into an array
  73.                 credString = inFS.nextLine();
  74.                 String arr[] = credString.split("\t");
  75.                
  76.                 //comparing user input(username) to what's in the credString variable that's reading the creds file.
  77.                 if(credString.contains(username)){
  78.                    
  79.                     //if the username passes it comes down here to test if the user plaintext password matches
  80.                     //the MD5 hash we have in the creds file once converted
  81.                     if (credString.contains(sb.toString())){
  82.                         System.out.println("Login Successful");
  83.                         System.out.println("");
  84.                        
  85.                         //this reads into the array we made earlier to pull out the user role(admin, zookeeper, veterinarian)
  86.                         String role = arr[3];
  87.                         //Prints to screen the user role file
  88.                         Files.lines(Paths.get(role + ".text")).forEach(System.out::println);
  89.                         System.out.println("test");
  90.                        
  91.                         //Resetting the failure count
  92.                         attempts = 0;
  93.                         System.exit(0);
  94.                        
  95.                     }}
  96.                 }                
  97.                
  98.                 //If the username is not contained in the current credString it notifies the user of the failure
  99.                 //adds to the count for the loop to end and notifies the user of how many fails have happened since last login
  100.                 if (!credString.contains(username)) {
  101.                     System.out.println("Login Failure");
  102.                     attempts = attempts +1;
  103.                     System.out.println("Failed attempts(max 3): " + attempts);
  104.                    
  105.                     }    
  106.                
  107.  
  108.             }
  109.             catch (NoSuchAlgorithmException ex) {
  110.                 Logger.getLogger(AuthSystem.class.getName()).log(Level.SEVERE, null, ex);
  111.             }
  112.            
  113.         }
  114.                
  115.     }
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement