java - Extra *(astericks) is displayed when entering encrypted password on cmd prompt -


i have below code encrypt user password, when user enter password * symbol displayed on cmd prompt. in below code, asterisks displaced when run command. please let me know 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. * displayed on screen before entering password.

current output :enter text encrypted:*

expected output : enter text encrypted:

the main highlight of erasure thread statement - system.out.print("\010*");.

\010 represents backspace character. use \b.

what thread attempts is, after printing prompt:

enter password: 

it backspace/delete last character, in example : , print *. prompt this:

enter password* 

every millisecond, keeps doing same. i.e. backspacing & placing *. so, * printed, deleted , * again printed in same place. nothing final result - it's still same. in fact happens fast looks static or nothing happening.

once person starts typing, e.g. a, this:

enter password*a 

then after 1 millisecond a gets deleted:

enter password* 

and * placed in position:

enter password** 

this creates illusion ever user types covered *s. actual string obtained when execute readline(). if want see happen in slow motion, increase time 1 second (1000ms).

i hope explains why there's initial * in prompt. 1 quick fix not print @ all. sysout contain simple \010 or \b. linux or sql terminals when 1 inputs password, absolutely nothing appears on terminal until enter.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -