Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 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.Path;
  17. import java.nio.file.Paths;
  18. import java.util.List;
  19.  
  20.  
  21. public class AuthSystem {
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      * @throws java.io.FileNotFoundException
  26.      */
  27.     public static void main(String[] args) throws FileNotFoundException, IOException {
  28.         Scanner input = new Scanner(System.in);
  29.         int attempts = 0;
  30.         String username = "";
  31.         String password = "";
  32.         FileInputStream fileByteStream = null;
  33.         Scanner inFS = null;
  34.         String credString;
  35.         //String role = "";
  36.        
  37.        
  38.         while (attempts < 3){
  39.             try {
  40.                 System.out.println("Enter Username: ");
  41.                 username = input.nextLine();
  42.                 if(username.contains("exit")){
  43.                     return;
  44.                 }
  45.                 System.out.println("Enter Password: ");
  46.                 password = input.nextLine();
  47.                 fileByteStream = new FileInputStream("creds.txt.text");
  48.                 inFS = new Scanner(fileByteStream);
  49.                 credString = "blank";
  50.                
  51.                
  52.                
  53.                 String original = password;  
  54.                 MessageDigest md = MessageDigest.getInstance("MD5");
  55.                 md.update(original.getBytes());
  56.                 byte[] digest = md.digest();
  57.                 StringBuffer sb = new StringBuffer();
  58.                 for (byte b : digest) {
  59.                     sb.append(String.format("%02x", b & 0xff));
  60.                 }
  61.                
  62.  
  63.                
  64.                 while(inFS.hasNextLine()){
  65.                    
  66.                 if(attempts >= 3){
  67.                     System.out.print("Login failure limit reached. Please contact an administrator to unlock your account.");
  68.                     return;
  69.                     }    
  70.                 credString = inFS.nextLine();
  71.                 String arr[] = credString.split("\t", 0);
  72.                 //System.out.println(arr[4]);
  73.                 //role = arr[4];
  74.                
  75.                 if(credString.contains(username)){
  76.                        
  77.                     if (credString.contains(sb.toString())){
  78.                         System.out.println(" Login Successful");
  79.                         String role = arr[4];
  80.                         System.out.println(role);
  81.                        
  82.                         break;
  83.                        
  84.                     }}
  85.                 }                
  86.                
  87.                
  88.                 if (!credString.contains(username)) {
  89.                     System.out.println("Login Failure");
  90.                     attempts = attempts +1;
  91.                     System.out.println("Failed attempts(max 3): " + attempts);
  92.                    
  93.                     }    
  94.                
  95.                
  96.  
  97.                
  98.                
  99.             } catch (NoSuchAlgorithmException ex) {
  100.                 Logger.getLogger(AuthSystem.class.getName()).log(Level.SEVERE, null, ex);
  101.             }
  102.            
  103.            
  104.         }
  105.                
  106.     }
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement