I have below code to encrypt the user password, when user enter the password an * symbol is displayed on the cmd prompt. In below code, an extra asterisks is always displaced when I run the command. Please let me know where I am doing wrong.
public static String readPassword(String prompt) {
System.out.print(prompt);
EraserThread et = new EraserThread(prompt);
Thread mask = new Thread(et);
mask.start();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String password = "";
try {
password = in.readLine();
} catch (IOException ioe) {
ioe.printStackTrace();
}
et.stopMasking();
return password;
}
class EraserThread implements Runnable {
private boolean stop;
public EraserThread(String prompt) {
System.out.print(prompt);
}
@Override
public void run() {
stop = true;
while (stop) {
System.out.print("\010*");
try {
Thread.currentThread();
Thread.sleep(1);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
public void stopMasking() {this.stop = false;}
}
i.e. an extra * is always displayed on the screen before entering the password.
Current Output :Enter text to be encrypted:*
expected output : Enter text to be encrypted: