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.Path;
- import java.nio.file.Paths;
- import java.util.List;
- 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;
- //String role = "";
- while (attempts < 3){
- try {
- System.out.println("Enter Username: ");
- username = input.nextLine();
- if(username.contains("exit")){
- return;
- }
- System.out.println("Enter Password: ");
- password = input.nextLine();
- fileByteStream = new FileInputStream("creds.txt.text");
- inFS = new Scanner(fileByteStream);
- credString = "blank";
- 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));
- }
- while(inFS.hasNextLine()){
- if(attempts >= 3){
- System.out.print("Login failure limit reached. Please contact an administrator to unlock your account.");
- return;
- }
- credString = inFS.nextLine();
- String arr[] = credString.split("\t", 0);
- //System.out.println(arr[4]);
- //role = arr[4];
- if(credString.contains(username)){
- if (credString.contains(sb.toString())){
- System.out.println(" Login Successful");
- String role = arr[4];
- System.out.println(role);
- break;
- }}
- }
- 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