Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide input on command line

I know that command line interfaces like Git and others are able to hide input from a user (useful for passwords). Is there a way to programmtically do this in Java? I'm taking password input from a user and I would like their input to be "hidden" on that particular line (but not on all of them). Here's my code for it (though I doubt it would be helpful...)

try (Scanner input = new Scanner(System.in)) {   //I'm guessing it'd probably be some property you set on the scanner or System.in right here...   System.out.print("Please input the password for " + name + ": ");   password = input.nextLine(); } 
like image 618
kentcdodds Avatar asked May 30 '12 15:05

kentcdodds


People also ask

How to hide user input in Bash?

Using the stty Command The stty command displays or changes the settings of a terminal and is also useful for hiding user input. We can use the echo setting of the stty command for enabling or disabling the echoing of input characters.

How to hide the user input in Java?

console(); String username = ""; String password = ""; if (console == null) { System. out. print("Enter username: "); username = input.

How do I remove a terminal input?

Use either key shortcut: Esc , #

How do I hide command line arguments in Photoshop?

I found below program which hide command line argument in ps command. Code: #include <string. h> int main(int argc, char **argv) { // process command line arguments.... // hide command line arguments if (argc > 1) { char *arg_end; arg_end = argv[argc-1] + strlen (argv[argc-1]); *arg_end = ' '; } // ... }


2 Answers

Try java.io.Console.readPassword. You'll have to be running at least Java 6 though.

   /**     * Reads a password or passphrase from the console with echoing disabled     *     * @throws IOError     *         If an I/O error occurs.     *     * @return  A character array containing the password or passphrase read     *          from the console, not including any line-termination characters,     *          or <tt>null</tt> if an end of stream has been reached.     */     public char[] readPassword() {         return readPassword("");     } 

Beware though, this doesn't work with the Eclipse console. You'll have to run the program from a true console/shell/terminal/prompt to be able to test it.

like image 96
adarshr Avatar answered Sep 21 '22 17:09

adarshr


Yes can be done. This is called Command-Line Input Masking. You can implement this easily.

You can uses a separate thread to erase the echoed characters as they are being entered, and replaces them with asterisks. This is done using the EraserThread class shown below

import java.io.*;  class EraserThread implements Runnable {    private boolean stop;     /**     *@param The prompt displayed to the user     */    public EraserThread(String prompt) {        System.out.print(prompt);    }     /**     * Begin masking...display asterisks (*)     */    public void run () {       stop = true;       while (stop) {          System.out.print("\010*");      try {         Thread.currentThread().sleep(1);          } catch(InterruptedException ie) {             ie.printStackTrace();          }       }    }     /**     * Instruct the thread to stop masking     */    public void stopMasking() {       this.stop = false;    } } 

With using this thread

public class PasswordField {     /**     *@param prompt The prompt to display to the user     *@return The password as entered by the user     */    public static String readPassword (String 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();       }       // stop masking       et.stopMasking();       // return the password entered by the user       return password;    } } 

This Link discuss in details.

like image 36
Vijay Shanker Dubey Avatar answered Sep 23 '22 17:09

Vijay Shanker Dubey