Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Defines the main class
- public static void main(String[] args) throws IOException {
- // Calls the login screen method
- loginScreen();
- }
- // This defines the login screen method
- @SuppressWarnings("resource")
- public static void loginScreen() {
- // Defines the string variable to generate password
- @SuppressWarnings("UnusedAssignment")
- String generatePass = "";
- // sets the login attempts to 3
- int flag1 = 0, attmpts = 3;
- // Define new input stream reader object
- BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
- // Display message to login
- System.out.println("nLogin");
- // Try block loop
- try {// FIXME: move closer to file reader
- do {
- // counts down login attempts before locking out user
- attmpts--;
- // prompts user to enter username
- System.out.println("Please Enter Your Username");
- // stores the user input for username
- String userName = br1.readLine();
- // prompts user for password
- System.out.println("Please Enter Your Password");
- // This creates an instance of the MD5 hash algorithm
- // Stores password value
- String original = br1.readLine();
- MessageDigest MD1 = MessageDigest.getInstance("MD5");
- MD1.update(original.getBytes());
- byte[] digest = MD1.digest();
- StringBuilder Sb1 = new StringBuilder();
- // for loop to iterate through the credentials file looking for hash
- for (int k = 0; k < digest.length; k++) {
- Sb1.append(Integer.toString((digest[k] & 0xff) + 0x100, 16).substring(1));
- generatePass = Sb1.toString();
- }
- // sets variable cuurent line
- String CurrentLne;
- // Define an instance of the file reader to read through the credentials file
- BufferedReader bin1 = new BufferedReader(new FileReader
- ("C:\Users\fento\Documents\NetBeansProjects\NewAuthenticate\src\newauthenticate\credentialfile.txt"));
- // Loop through the file until you reach the end of the file
- while ((CurrentLne = bin1.readLine()) != null) {
- // Seperates each line within the file
- String[] strArr = CurrentLne.split("t");
- // If statement to validate that user name matches
- if (strArr[0].equals(userName)) {
- // If statement to verify if password matches
- if (strArr[1].equals(generatePass)) {
- // Assign counter value
- flag1 = 1;
- // Break
- break;
- }
- }
- }
- // If statement to iterate through login attempts beginning at 0
- if (attmpts == 0) {
- // Display a message with number of logins remaining
- System.out.println("You have only" + attmpts + " more login attempts left");
- // Displays an exit message message
- System.out.println("Exit...");
- // Exits the system after 3 fail login attempts
- System.exit(1);
- }
- // If flag is 1
- if (flag1 == 1) {
- // Calls the admin screen method
- AdminLogin();
- // Break
- break;
- } else {
- // Displays a failed or invalid login attempt
- System.out.println("You have entered an Invalid Username or Password.");
- // print a blank line
- System.out.println();
- // prompts user to try to login again
- System.out.println(" Please try again.");
- // print a blank line
- System.out.println();
- // prompts user with number of login attempts left
- System.out.println(attmpts + " more attemptes left.n");
- }
- } // while loop
- while (attmpts > 0);
- } // catches of no such algorithm exception error
- catch (NoSuchAlgorithmException nae) {
- System.out.println("Current Alogorithm does not function: ");
- }
- catch (NoSuchFileException e){
- System.out.println("Current file will not: ");
- }
- catch(IOException e){
- System.out.println("There is a problem reading the current file:");
- }
- }
- // Definition of the admin screen method
- public static void AdminLogin() {
- // Declare variable
- String logOut1;
- // sets up a scanner variable and a new scanner object
- Scanner sc1 = new Scanner(System.in);
- // dispalys a welcome admin prompt to the user
- System.out.println("nWelcome Admin");
- // prompts user to continue or press 99 to exit
- System.out.println("continue or Press 99 for log outn");
- // Do loop
- do {
- logOut1 = sc1.nextLine();
- } // while loop that looks for user inputting 99 to log out
- while (!logOut1.equals("99"));
- // if statement looking for 99 and if entered program exits
- if (logOut1.equals("99")) {
- // method call to the login screen
- loginScreen();
- }
- }
Add Comment
Please, Sign In to add comment