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