Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.security.MessageDigest;
- import java.util.HashMap;
- import java.util.Map;
- class User {
- // fucntion to check if user is valid or not
- public int isUserValid(String username, String password) throws Exception
- {
- int validId = 0;
- // <userName,hash password>
- Map<String, String> credentials = new HashMap<>();
- credentials.put("griffin.keyes", "93f725a07423fe1c889f448b33d21f46"); // password == java
- credentials.put("rosario.dawson", "2bff04a9b5f68427573d086983df5eed"); // password == pizza
- credentials.put("bernie.gorilla", "1801bc89e752077204c92b3dd9f9f62d"); // password == dew
- credentials.put("donald.monkey", "23eeeb4347bdd26bfc6b7ee9a3b755dd"); // password == python
- credentials.put("jerome.grizzlybear", "fc35fdc70d5fc69d269883a822c7a53e"); // password == html
- credentials.put("bruce.grizzlybear", "e77989ed21758e78331b20e477fc5582"); // password == dev
- // here we are generating the md5 hash of our password
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(password.getBytes());
- byte[] digest = md.digest();
- StringBuffer passward_hash = new StringBuffer();
- for (byte b : digest) {
- passward_hash.append(String.format("%02x", b & 0xff));
- }
- // we are iterating through the hash map and comparing the input passward and username with saved ones
- // by using for each loop
- for(Map.Entry it : credentials.entrySet())
- {
- // if password and username match with the password and username in hash map return the id of hashmap
- if(username.contentEquals(it.getKey().toString()) && passward_hash.toString().contentEquals(it.getValue().toString()))
- {
- return validId;
- }
- validId ++;
- }
- // if not match return -1
- return -1;
- }
- // this function shows diffrent msg according to diffrent user
- public void prineWelcomeMsg(int id)
- {
- switch(id)
- {
- case 0:
- {
- System.out.println();
- System.out.println("Hello, Zookeeper!\n" +
- "\n" + "As zookeeper, you have access to all of the"
- + " animals'" + " information and their" + " daily"
- + " monitoring logs. This " + "allows you to track their "
- + "feeding habits, habitat conditions, and general welfare.");
- System.out.println();
- break;
- }
- case 1:
- {
- System.out.println();
- System.out.println("Hello, Zookeeper!\n" +
- "\n" + "As zookeeper, you have access to all of the"
- + " animals'" + " information and their" + " daily"
- + " monitoring logs. This " + "allows you to track their "
- + "feeding habits, habitat conditions, and general welfare.");
- System.out.println();
- break;
- }
- case 2:
- {
- System.out.println();
- System.out.println("Hello, Veterinarian!\n" + "\n" + "As "
- + "veterinarian, you have access to all of the animals'"
- + " health records. This allows you to view each animal's "
- + "medical history and current treatments/illnesses "
- + "(if any), and to maintain a vaccination log.");
- System.out.println();
- break ;
- }
- case 3:
- {
- System.out.println();
- System.out.println("Hello, Veterinarian!\n" + "\n" + "As "
- + "veterinarian, you have access to all of the animals'"
- + " health records. This allows you to view each animal's "
- + "medical history and current treatments/illnesses "
- + "(if any), and to maintain a vaccination log.");
- System.out.println();
- break;
- }
- case 4:
- {
- System.out.println();
- System.out.println("Hello, System Admin!\n" + "\n" + "As"
- + " administrator, you have access to the zoo's main "
- + "computer system. This allows you to monitor users in "
- + "the system and their roles.");
- break;
- }
- case 5:
- {
- System.out.println();
- System.out.println("Hello, System Admin!\n" + "\n" + "As"
- + " administrator, you have access to the zoo's main "
- + "computer system. This allows you to monitor users in "
- + "the system and their roles.");
- break;
- }
- default :
- {
- // empty or fill anything
- }
- }
- }
- }
- public class Authentication {
- public static void main(String[] args) throws Exception {
- BufferedReader scnr = new BufferedReader(new InputStreamReader(System.in));
- String userName = null;
- String usersType [] = {"zookeeper","zookeeper1","veterinarian","veterinarian1","Admin","Admin1"};
- User userObj = new User()
- ;
- int isValidId = -1;
- while(isValidId == -1)
- {
- System.out.println("Enter username:");
- userName = scnr.readLine();
- System.out.println("Enter password:");
- String password1 = scnr.readLine();
- isValidId = userObj.isUserValid(userName, password1) ;
- if (isValidId != -1 )
- {
- System.out.println("Valid User");
- userObj.prineWelcomeMsg(isValidId);
- }
- else
- System.out.println("InValid User");
- }
- }
- }
Add Comment
Please, Sign In to add comment