Skip to content

Instantly share code, notes, and snippets.

@weirdestnerd
Last active December 1, 2016 00:45
Show Gist options
  • Save weirdestnerd/5b9966b1ae7affdd2dc0ab2b21638d9f to your computer and use it in GitHub Desktop.
Save weirdestnerd/5b9966b1ae7affdd2dc0ab2b21638d9f to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
import java.security.*;
class User{
String username;
String pw;
String msd_pw;
static String role;
public User(){
username = "";
pw = "";
msd_pw = "";
role = "";
}
public User(String username, String pw, String msd_pw, String role){
this.username = username;
this.pw = pw;
this.msd_pw = msd_pw;
this.role = role;
}
}
public class Auth{
public static String pwToMSD(String pw) throws NoSuchAlgorithmException{
//Copy and paste this section of code
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(pw.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
public static void main (String []args) throws NoSuchAlgorithmException, IOException{
int trials = 3;
Scanner read = new Scanner(System.in);
User newUser = new User();
while (trials != 0){
//Ask User for info
System.out.println("Enter username: \n");
String g_user = read.nextLine();
System.out.println("Enter password: \n");
String g_pw = read.nextLine();
//Convert password to MSD HASH 5
String msd_pw = pwToMSD(g_pw);
//System.out.println("MSD: " + msd_pw); for debugging
//open file
FileInputStream file = new FileInputStream("credential.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(file));
String line;
boolean foundUser = false;
while ( (line = br.readLine()) != null){
String[] arr = line.split("\t");
if (g_user.equals(arr[0]) && msd_pw.equals(arr[1])){
newUser = new User(g_user, g_pw, msd_pw, arr[3]);
foundUser = true;
break;
}
}
if (foundUser){
System.out.println("You are logged in \n");
String roleFileToRead = newUser.role + ".txt";
String roleLine;
String logout = "n";
while (!logout.equals("y")){
FileInputStream roleFile = new FileInputStream(roleFileToRead);
BufferedReader roleBr = new BufferedReader(new InputStreamReader(roleFile));
while ( (roleLine = roleBr.readLine()) != null ){
System.out.println(roleLine);
}
System.out.println("Do you want to log out? ");
logout = read.nextLine();
}
System.out.println("You are logged out");
trials = 3;
}
else{
trials--;
System.out.println("You have " + trials + " trials left");
}
}
System.out.println("You are out of trials");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment