Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package auth.system;
- /**
- *
- * @author Patrick
- */
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- public class AuthSystem {
- /**
- * @param args the command line arguments
- * @throws java.io.FileNotFoundException
- */
- public static void main(String[] args) throws FileNotFoundException, IOException {
- Scanner input = new Scanner(System.in);
- int attempts = 0;
- String username = "";
- String password = "";
- FileInputStream fileByteStream = null;
- Scanner inFS = null;
- String credString;
- // Loop to keep failed attempts under 3
- while (attempts < 3){
- try {
- System.out.print("Enter Username(\"exit\" to logout): ");
- username = input.nextLine();
- // This if statement gives the user a way to logout
- if(username.contains("exit")){
- return;
- }
- System.out.print("Enter Password: ");
- password = input.nextLine();
- //import creds file
- fileByteStream = new FileInputStream("creds.text");
- inFS = new Scanner(fileByteStream);
- credString = "blank";
- //convert plaintext password from user to md5hash
- String original = password;
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(original.getBytes());
- byte[] digest = md.digest();
- StringBuffer sb = new StringBuffer();
- for (byte b : digest) {
- sb.append(String.format("%02x", b & 0xff));
- }
- //loop only while the inFS, which is reading the file line by line, is not null.
- //This way it exits the loop onces it reaches the end of the file.
- while(inFS.hasNextLine()){
- //kicks the loop out if the user fails more than 3 times
- if(attempts >= 3){
- System.out.print("Login failure limit reached. Please contact an administrator to unlock your account.");
- return;
- }
- //Reading the next line in the file and splitting it into an array
- credString = inFS.nextLine();
- String arr[] = credString.split("\t");
- //comparing user input(username) to what's in the credString variable that's reading the creds file.
- if(credString.contains(username)){
- //if the username passes it comes down here to test if the user plaintext password matches
- //the MD5 hash we have in the creds file once converted
- if (credString.contains(sb.toString())){
- System.out.println("Login Successful");
- System.out.println("");
- //this reads into the array we made earlier to pull out the user role(admin, zookeeper, veterinarian)
- String role = arr[3];
- //Prints to screen the user role file
- Files.lines(Paths.get(role + ".text")).forEach(System.out::println);
- System.out.println("test");
- //Resetting the failure count
- attempts = 0;
- System.exit(0);
- }}
- }
- //If the username is not contained in the current credString it notifies the user of the failure
- //adds to the count for the loop to end and notifies the user of how many fails have happened since last login
- if (!credString.contains(username)) {
- System.out.println("Login Failure");
- attempts = attempts +1;
- System.out.println("Failed attempts(max 3): " + attempts);
- }
- }
- catch (NoSuchAlgorithmException ex) {
- Logger.getLogger(AuthSystem.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement