i have a hash map that stores user name and password;
Map<
String, byte[]> userData = new HashMap<String, byte[]>();
-this map stores a username of type string and password stoerd as hash value
i can successfully add user (user name& password)
i need help in implementing a getPassword(): this method must ask the user to enter username and should return the associated password..
here is my code; pliz help
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;
public class PasswordService
{
//The hash is to be formed using the SHA algorithm
//to create a MessageDigest
private final String algorithmName = "SHA";
//Use a message digest to create hashed passwords
private MessageDigest md = null;
//We simulate a database of users using who have a login and password
//as a key and value pair in a Map
private Map<String, byte[]> userData;
//complete the constructor
public PasswordService()
{
//intialize the class instance data
userData = new HashMap<String, byte[]>();
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException e) {
System.out.println("No Such Algorithm");
e.printStackTrace();
}
}
public void addUser(String login, String password)
{
Scanner in = new Scanner(System.in);
for(;;){
System.out.print ("Enter login name..");
login = in.nextLine();
System.out.print("Password: ");
password = in.nextLine();
byte[] pass = md.digest(password.getBytes());
userData.put(login, pass);
System.out.println("User added successfully..");
Iterator iterator = userData.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println("Login: " + key + " Password: " + userData.get(key));
}
}
}
public byte[] getPassword(String login)
{
//TODO
Scanner sc = new Scanner(System.in);
System.out.println("Please enter login name....");
login = sc.nextLine();
// String pass = new String(new byte[]<userData>);
byte[] bt = new <byte[]> userData.get(login);
System.out.println("Password " + bt);
return null;
}
}