Question

Using Java: implement a program with the following features. Thanks Produce a menu for the user...

Using Java: implement a program with the following features. Thanks

  • Produce a menu for the user with 3 choices.
    1. Add a new user
    2. Attempt login
    3. See all users
  • If the user chooses #1, prompt them for a username (a string). Then a password (a string). Using SHA-2 hash the password they gave you, and store the resulting username and hashed password in a datastructure of your choice. (It can be 2 arrays, one for usernames, one for hashed passwords).
  • If the user chooses #2, prompt them for a username and password. When they enter the username, first search to see if you have a matching username stored. If you don't give them a "unknown user" error. When they enter their password, hash it with SHA-2 again, and compare the resulting hash with the stored hash for that user. If they are correct print "Login successful", if not "Login failed".
  • If the user chooses #3, print out all the usernames and hashed passwords you have.
  • Note: When hashing the password you can use any strength SHA that is supported by your language. You'll likely need to use a salt when you calculate the hash, that can be hardcoded in your application.
  • Note: You do not have to use files or databases to store the username/password hashes. They'll only be in memory

Homework Answers

Answer #1

Here is the requried program with proper comments:

package users;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

public class Users {

        //Constant Salt for SHA algorithm
        private static final String SALT = "SALTSAMPLE";

        //MAx Array Size declared to avoid array out of bound exception
        private static final int MAX_USERS=100;

        //User name and password arrays
        private static String[] userNames=new String[100];      
        private static String[] passwords=new String[100];

        //Constructor
        public Users() {

        }

        public static void main(String[] args) {
                //Scanner for input
                Scanner sc=new Scanner(System.in);
                //variable to take choice
                int choice=0;
                //keep track of user count
                int userCount=0;
                do {
                        System.out.println("Enter Your choice:\nAdd new user(1)\nAttempt Login(2)\nSee all Users(3)\nExit(4):");
                        //get choice
                        choice=sc.nextInt();
                        //Variables to get user name
                        String userName="";
                        //variable to get password
                        String password="";
                        //variable to keep hashed password
                        String hashedPassword="";
                        sc.nextLine();
                        switch (choice) {
                        //For adding new user
                        case 1:
                                System.out.println("Enter User Name:");
                                userName=sc.nextLine();
                                System.out.println("Enter password:");
                                password=sc.nextLine();
                                hashedPassword=getSecurePassword(password);
                                //check if maximum users reached
                                if(userCount<MAX_USERS) {
                                        //If not add user
                                        userNames[userCount]=userName;
                                        passwords[userCount]=hashedPassword;
                                        //increase user count
                                        userCount++;
                                }
                                else {
                                        System.out.println("Max User count reached");
                                }
                                //usernameAndPasswords.put(userName, hashedPassword);
                                break;
                                //For attemptig login   
                        case 2:
                                System.out.println("Enter User Name:");
                                userName=sc.nextLine();

                                boolean islogin=false;
                                boolean isuserfound=false;
                                for(int i=0;i<userCount;i++) {
                                        if(userNames[i].equalsIgnoreCase(userName)) {
                                                isuserfound=true;
                                                break;
                                        }
                                }
                                if(!isuserfound) {
                                        System.out.println("Unknown User");

                                }
                                else {
                                        System.out.println("Enter password:");
                                        password=sc.nextLine();
                                        hashedPassword=getSecurePassword(password);
                                        for(int i=0;i<userCount;i++) {
                                                if(passwords[i].equalsIgnoreCase(hashedPassword)) {
                                                        System.out.println("Login successful");
                                                        islogin=true;
                                                        break;
                                                }
                                        }

                                        if(!islogin)
                                                System.out.println("Login failed");
                                }
                                break;

                                //for printing all users
                        case 3:
                                for(int i=0;i<userCount;i++) {
                                        System.out.println("User Name : "+userNames[i] +" Password : "+passwords[i]);
                                }

                                break;
                        default:
                                break;                                  
                        }
                        System.out.println();
                }
                while(choice!=4);
                System.out.println("Program Exited");
        }

        /**
         * Generates a hashed password using SHA-256 and constant SALT
         * @param password: password to be hashed
         * @return: hashed password
         */
        public static String getSecurePassword(String password) {

                String generatedPassword = null;
                try {
                        MessageDigest md = MessageDigest.getInstance("SHA-256");
                        md.update(SALT.getBytes());
                        byte[] bytes = md.digest(password.getBytes());
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < bytes.length; i++) {
                                sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
                        }
                        generatedPassword = sb.toString();
                } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                }
                return generatedPassword;
        }
}

Program output:

Enter Your choice:
Add new user(1)
Attempt Login(2)
See all Users(3)
Exit(4):
1
Enter User Name:
user 1
Enter password:
password 1

Enter Your choice:
Add new user(1)
Attempt Login(2)
See all Users(3)
Exit(4):
1
Enter User Name:
user2
Enter password:
password2

Enter Your choice:
Add new user(1)
Attempt Login(2)
See all Users(3)
Exit(4):
2
Enter User Name:
user3
Unknown User

Enter Your choice:
Add new user(1)
Attempt Login(2)
See all Users(3)
Exit(4):
2
Enter User Name:
user 1
Enter password:
password 1
Login successful

Enter Your choice:
Add new user(1)
Attempt Login(2)
See all Users(3)
Exit(4):
3
User Name : user 1 Password : 7d9eeb5d8a1dd61574701e03449851cfe6b7c82fda377479e3389365c510a497
User Name : user2 Password : 0ba240ed711e7da851d7b661c7a7d7c873e7b08924c6cf30b7fd84557640f14e

Enter Your choice:
Add new user(1)
Attempt Login(2)
See all Users(3)
Exit(4):
4

Program Exited

Console output screen shot:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT