Using Java: implement a program with the following features. Thanks
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:
Get Answers For Free
Most questions answered within 1 hours.