Here is the code I have so far for an authentication system

Here is the code I have so far for an authentication system. Could someone please help edit the code to allow a particular user named \"rosario.dawson\" and \"bruce.grizzlybear\" with a special greeting that states: \"Hello, System Admin! As administrator, you have access to the zoo\'s main computer system. This allows you to monitor users in the system and their roles.\"

The program refers to a text document which explains the MD5 section of the code. There is no way for me to attach the file here so I have copied and pasted the credentials below. Youmay have to recreate this text file in order for it to work with the program. The code works with these credentials but I need the special greeting for system admin roles. The credentials are as follows:

griffin.keyes   108de81c31bf9c622f76876b74e9285f   \"alphabet soup\"   zookeeper
rosario.dawson   3e34baa4ee2ff767af8c120a496742b5   \"animal doctor\"   admin
bernie.gorilla   a584efafa8f9ea7fe5cf18442f32b07b   \"secret password\"   veterinarian
donald.monkey   17b1b7d8a706696ed220bc414f729ad3   \"M0nk3y business\"   zookeeper
jerome.grizzlybear   3adea92111e6307f8f2aae4721e77900   \"grizzly1234\"   veterinarian
bruce.grizzlybear   0d107d09f5bbe40cade3de5c71e9e9b7   \"letmein\"   admin

I HAVE SUPPLIED THE CODE BELOW:

import java.io.File;
import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

public class Authenticator {

public static void main(String[] args) throws FileNotFoundException, NoSuchAlgorithmException {
Authenticator a = new Authenticator();
Scanner credential = new Scanner(System.in);
  
//loop for unsuccessful attempts
for (int i = 0; i < 3; i++) {
System.out.print(\"enter username: \");
String user = credential.nextLine();
  
System.out.print(\"enter password: \");
String pass = credential.nextLine();
  
  
Scanner input = new Scanner(new File(\"password.txt\"));
String password = null;
  
while (input.hasNext()) {

String output = input.next();
if (output.equals(user)) {
password = input.next();
}

}
  
//check credentials against document
if (a.md5Convert(pass).equals(password)) {
System.out.println(\"Welcome \" + user);
if (a.menu() == 0) {
System.exit(0);
}
else{
System.out.println(\"wrong choice\");
System.exit(0);
}
}
else {
  
System.out.println(\"Wrong credentials!! Please try again\");
System.out.println(\"Do you wish to continue? Press any key to try again or \'N\' to quit\");
String Continue = credential.nextLine();
if(Continue.toUpperCase().charAt(0)==\'N\'){
System.exit(0);
}
}
}

}

public String md5Convert(String original) throws NoSuchAlgorithmException {

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));
}

return sb.toString();
}

public int menu() {
System.out.println(\"Press 0 to log out\");
Scanner choice = new Scanner(System.in);
int c = choice.nextInt();
return c;
}
}

Solution

Hi,

Look you can change the logic as follows. When the program starts, read 4 values, say - username , hexvalue, password and role. If the username and password credentials match with the original ones present in the document, the check if the value stored in the variable \" role \" is admin. If the entered value is admin (and it is supposed to be so - check in document), the print the message \"Hello, System Admin! As administrator, you have access to the zoo\'s main computer system. This allows you to monitor users in the system and their roles.\" .

This is one way you can achieve that.

Hopefully this helps.

Here is the code I have so far for an authentication system. Could someone please help edit the code to allow a particular user named \
Here is the code I have so far for an authentication system. Could someone please help edit the code to allow a particular user named \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site