Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package authentication;
- import java.security.MessageDigest;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Scanner;
- public class ValidateCredentials {
- private boolean isValid;
- private String filePath;
- private String credentialsFileName;
- public ValidateCredentials() {
- filePath = "";
- isValid = false;
- credentialsFileName = "credentials";
- }
- public boolean isCredentialsValid(String userName, String passWord) throws Exception {
- 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));
- }
- System.out.println("");
- System.out.println("original:" + original);
- System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
- isValid = readDataFiles(userName, sb.toString());
- return isValid;
- }
- public boolean readDataFiles(String userName, String passWord) throws IOException {
- FileInputStream fileByteStream1 = null; // File input stream
- FileInputStream fileByteStream2 = null; // File input stream
- Scanner inFS1 = null; // Scanner object
- Scanner inFS2 = null; // Scanner object
- String textLine = null;
- String textFileName = null;
- boolean foundCredentials = false;
- // Try to open file
- System.out.println("");
- System.out.println("Opening file " + credentialsFileName + ".txt");
- fileByteStream1 = new FileInputStream(filePath + "credentials.txt");
- inFS1 = new Scanner(fileByteStream1);
- System.out.println("");
- System.out.println("Reading lines of text.");
- while (inFS1.hasNextLine()) {
- textLine = inFS1.nextLine();
- //System.out.println(textLine);
- String[] words = textLine.split("\s");//splits the string based on whitespace
- if (words[0].equals(userName) && textLine.contains(passWord)) {
- foundCredentials = true;
- int last = words.length - 1;
- textFileName = words[last];
- break;
- }
- }
- // Done with file, so try to close it
- System.out.println("");
- System.out.println("Closing file " + credentialsFileName + ".txt");
- if (textLine != null) {
- fileByteStream1.close(); // close() may throw IOException if fails
- }
- if (foundCredentials == true) {
- // Try to open file
- System.out.println("");
- System.out.println("Opening file " + textFileName + ".txt");
- fileByteStream2 = new FileInputStream(filePath + textFileName + ".txt");
- inFS2 = new Scanner(fileByteStream2);
- System.out.println("");
- while (inFS2.hasNextLine()) {
- textLine = inFS2.nextLine();
- System.out.println(textLine);
- }
- // Done with file, so try to close it
- System.out.println("");
- System.out.println("Closing file " + textFileName + ".txt");
- if (textLine != null) {
- fileByteStream2.close(); // close() may throw IOException if fails
- }
- }
- return foundCredentials;
- }
- }
- package authentication;
- import java.io.File;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.List;
- import java.util.Scanner;
- public class Authentication {
- public static void main(String args[]) {
- try {
- Scanner inputCreds;
- inputCreds = new Scanner(new File("src\authentication\credentials.txt"));
- String userCredentials[][] = new String[100][4];
- int count = 0;
- while (inputCreds.hasNextLine()) {
- userCredentials[count][0] = inputCreds.next();
- userCredentials[count][1] = inputCreds.next();
- String l[] = inputCreds.nextLine().split(""[ ]+");
- l[0] = l[0].trim();
- l[0] = l[0].replace(""", "");
- userCredentials[count][2] = l[0];
- userCredentials[count][3] = l[0].trim();
- count++;
- }
- Scanner input = new Scanner(System.in);
- boolean RUN = true;
- int tries = 0;
- while (RUN) {
- System.out.println("Welcome to your Zoo Account Authorization Screen.");
- System.out.println("Please select your option:");
- System.out.println("(1) - Log In.");
- System.out.println("(2) - Exit Program.");
- int ch = Integer.parseInt(input.nextLine().trim());
- if (ch == 1) {
- tries++;
- System.out.print("Username: ");
- String userName = input.nextLine();
- System.out.print("Password: ");
- String passWord = input.nextLine();
- MessageDigest md;
- md = MessageDigest.getInstance("MD5");
- md.update(passWord.getBytes());
- byte[] digest = md.digest();
- StringBuilder sb = new StringBuilder();
- for (byte b : digest) {
- sb.append(String.format("%02x", b & 0xff));
- }
- String hPassword = sb.toString();
- boolean badCreds = true;
- for (int i = 0; i < count; i++) {
- if (userName.contentEquals(userCredentials[i][0])) {
- if (hPassword.contentEquals(userCredentials[i][1])) {
- List<String> data = null;
- switch (userCredentials[i][3]) {
- case "zookeeper":
- data = Files.readAllLines(Paths.get("zookeeper.txt"), Charset.defaultCharset());
- break;
- case "admin":
- data = Files.readAllLines(Paths.get("admin.txt"), Charset.defaultCharset());
- break;
- case "veterinarian":
- data = Files.readAllLines(Paths.get("veterinarian.txt"), Charset.defaultCharset());
- break;
- default:
- break;
- }
- if (data != null) {
- for (String s : data) {
- System.out.println(s);
- }
- }
- tries = 0;
- System.out.println("n1) Logout.");
- System.out.println("2) Exit.");
- ch = Integer.parseInt(input.nextLine().trim());
- if (ch == 2) {
- RUN = false;
- }
- badCreds = false;
- break;
- }
- }
- }
- if (badCreds) {
- System.out.println("Invalid Username or password.");
- }
- } else {
- break;
- }
- if (tries == 3) {
- RUN = false;
- System.out.println("Too many incorrect attempts!");
- }
- }
- } catch (IOException | NumberFormatException | NoSuchAlgorithmException ex) {
- }
- }
- }
Add Comment
Please, Sign In to add comment