mask password on command line?

J

John Currier

Andrew said:
Headless. No (not forgetting). I mentioned it on a variety
of other sub-threads you seem to have missed.

We are now talking about use of JOptionPane in other environments.

(And FWIW - I was never entirely clear, and no longer care,
if the OP's environment *is* 'headless')

I guess it's a misunderstanding of the basic definition of a
command-line (a.k.a console) program. There's a very good reason that
JOptionPane/JPasswordField was never mentioned as a possible solution
to Sun's original problem. The reason is that the original problem
deals with non-GUI environments. Here's a snippet from the original
bug:

"Not all applications of Java can assume that a GUI is available, so
AWT's setEchoChar stuff is not usable for this problem."

To quote you from yesterday:
I find that odd, since it seems to me that any console
application that supports user interaction can showInputDialog...

Is there something I am missing?

Yes, you're missing something. Your first sentence isn't a valid
assumption. *Many* console applications that support user interaction
can *not* showInputDialog.

John
 
C

Chris Smith

John Currier said:
The bugParade entry for this request has been closed with a 'fixed'
status:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4050435

I can't really figure out any deails, but it's supposedly fixed.

It's fixed in Mustang, for which snapshots is available. For details on
the API, see:

http://download.java.net/jdk6/docs/api/index.html

Specifically, look at:

java.lang.System.console()
java.io.Console

You'll notice that, far from providing a curses-like general-purpose
console API, the new additions are incredibly limited, and basically
serve no useful purpose other than to enable the collection of password
input without echoing the characters back to the screen.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

John Currier

Chris said:
It's fixed in Mustang, for which snapshots is available. For details on
the API, see:

http://download.java.net/jdk6/docs/api/index.html

Specifically, look at:

java.lang.System.console()
java.io.Console

You'll notice that, far from providing a curses-like general-purpose
console API, the new additions are incredibly limited, and basically
serve no useful purpose other than to enable the collection of password
input without echoing the characters back to the screen.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Thanks for the clarification Chris.

John
http://schemapsy.sourceforge.net
 
J

jeremiah johnson

It doesn't help you now, but in Java 1.6, there is a ReadPassword()
function that is used with a DataInputReader(System.in) that will do
exactly what you want.

Until then I don't know how to help.

Sorry. The rest of the thread has gone off topic.
 
Joined
Feb 9, 2012
Messages
1
Reaction score
0
New Tread

The simplest way to do this would be to do something as follows. (NOTE: I am only 15 and am not an expert, but this seems to work well):

import java.io.PrintWriter;
public class PasswordFilter extends Thread{
private PrintWriter out;
PasswordFilter(PrintWriter out){
this.out = out;
}
public void kill(){
_continue = false;
}
public void run(){
_continue = true;
while(_continue){
out.flush();
out.print("\b"+"*");
try {
sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private boolean _continue;
}


NOTE: The code above works over a socket, too! The out.flush() call seems to be needed for the expected output, though. I really don't know why, but without it it didn't work well over a Socket connection.

You can implement another class to work it on a socket like this:

import java.net.*;
public class ServerClass{
public static void main(String[] args) {
int port = 4000;
try {
System.out.println("Welcome to the Server "+ "application!\nListening on port number " + port);
ServerSocket ss = new ServerSocket(port);
int i = 0;
while(true){
i++;
Socket s = ss.accept();
System.out.println("Now serving:" +i+"\nConnected to " + ss.getInetAddress().toString());
Thread t = new Thread(new Thr(s, ss));
t.start();
}} catch (IOException e) {
System.out.println("Server ERROR!");
}
}

And then another:

import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;
public class Thr implements Runnable {
public static Socket s;

Thr(Socket RFLSocket, ServerSocket RFLServerSocket){
ss = RFLServerSocket;
s = RFLSocket;
}
public void run(){
try{
Scanner in = new Scanner(s.getInputStream());
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
while(true){
PasswordFilter filter = new PasswordFilter(out);
out.println("Enter your password");
filter.start();
in = in.reset();
String password = in.nextLine();
filter.kill();
if(password.equals("passw0rd"){
out.println("You entered the correct password!");
break;
}
else out.println("WRONG!!!");
}
}
catch(Exception e){}
}
}

NOTE: The actual code used will be much more complicated, but this serves as an outline to create a simple Socket Telnet application.

And if you're wandering, this is being used by me to make a MUD! More 15 year olds should really become nerds!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top