Java Forum / General / October 2005
mask password on command line?
djbitchpimp@snowboard.com - 19 Oct 2005 22:49 GMT I need a simple way to mask a string (password field) while inputing characters on the command line. It can either cover the string with '*' or just not advance the cursor.
Does anyone have any ideas?
Real Gagnon - 19 Oct 2005 23:10 GMT > I need a simple way to mask a string (password field) while inputing > characters on the command line. It can either cover the string with '*' > or just not advance the cursor. > > Does anyone have any ideas? One idea is to start a thread to print backspace characters to the console to hide the current input.
Example at : http://www.rgagnon.com/javadetails/java-0375.html
Bye.
 Signature Real Gagnon from Quebec, Canada * Looking for Java or PB code examples ? Visit Real's How-to * http://www.rgagnon.com/howto.html
Roedy Green - 19 Oct 2005 23:24 GMT >I need a simple way to mask a string (password field) while inputing >characters on the command line. It can either cover the string with '*' >or just not advance the cursor. > >Does anyone have any ideas? You could write a small MASM or C command line utility to do it and leave the result in a file or the set environment.
You could use 4NT as your command processor. It has such as beast called INPUT /Password
see http://mindprod.com/jgloss/fornt.html
You could delay entering the password until the program starts, then you have access to Java's JPasswordField
you could hire me to solve your problem for $50 US.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Andrew Thompson - 20 Oct 2005 02:44 GMT > I need a simple way to mask a string (password field) while inputing > characters on the command line. It can either cover the string with '*' > or just not advance the cursor. > > Does anyone have any ideas? Here's (something like) how I would approach it assuming Java ..1.2+
<sscce> import javax.swing.*;
public class IHaveASecret {
private static final String password = "aaa";
public static void main(String[] args) { JPasswordField passwordField = new JPasswordField(10); while (!( ( new String(passwordField.getPassword()) ).equals(password) )) { passwordField.setText(""); JOptionPane.showMessageDialog( null, passwordField, "Password Required (aaa)", JOptionPane.WARNING_MESSAGE ); } JOptionPane.showMessageDialog(null, "Welcome, secure user!"); } } </sscce>
HTH
John Currier - 20 Oct 2005 02:53 GMT > I need a simple way to mask a string (password field) while inputing > characters on the command line. It can either cover the string with '*' > or just not advance the cursor. > > Does anyone have any ideas? 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.
John http://schemaspy.sourceforge.net
Andrew Thompson - 20 Oct 2005 03:06 GMT >>I need a simple way to mask a string (password field) while inputing >>characters on the command line. ...
> The bugParade entry for this request has been closed with a 'fixed' > status: > http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4050435 It has generated a *lot* of interest and comments, but oddly, nobody mentioned the JOptionPane/JPassWordField path.
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?
Roedy Green - 20 Oct 2005 03:59 GMT >It has generated a *lot* of interest and comments, but oddly, >nobody mentioned the JOptionPane/JPassWordField path. I did in my first post on the topic.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Andrew Thompson - 20 Oct 2005 04:29 GMT >>It has generated a *lot* of interest and comments, but oddly, >>nobody mentioned the JOptionPane/JPassWordField path. > > I did in my first post on the topic. Yes you did, on your first post on this thread, but...
The 'It' I was referring to was.. this 'It' > <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4050435> There are no mentions of JOptionPane in that bug report (at least, not that my Mozilla could 'text search').
John Currier - 20 Oct 2005 05:32 GMT > >>I need a simple way to mask a string (password field) while inputing > >>characters on the command line. [quoted text clipped - 10 lines] > > Is there something I am missing? What exactly is a console application?
A command-line application doesn't use Swing or AWT. In most cases it doesn't know or care if a GUI is available (think telnet or ssh sessions to a remote box without X or another GUI).
John http://schemaspy.sourceforge.net (a command-line application that generates pages for a GUI representation)
Andrew Thompson - 20 Oct 2005 05:40 GMT >>>>I need a simple way to mask a string (password field) while inputing >>>>characters on the command line. [quoted text clipped - 5 lines] >>It has generated a *lot* of interest and comments, but oddly, >>nobody mentioned the JOptionPane/JPassWordField path. ...
> What exactly is a console application? > > A command-line application doesn't use Swing or AWT. In most cases it > doesn't know or care if a GUI is available (think telnet or ssh > sessions to a remote box without X or another GUI). 'Headless' environments? Yes that would be a problem.
I had not thought the Headless enviroment would apply to this situation (I was thinking 'Headless - batch files only'), and if it does, then yes, that completely explains why GUI components are unavailable/unusable.
Roedy Green - 20 Oct 2005 03:58 GMT >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 think everyone is confusing the Java console with the command line you use to fire up java.exe.
I think he wanted to pass the parameter in on the command line, not get it from the console after the Java program started
If he is willing to get it after the program starts, forget the console. Use JPasswordField and a little JDialog box
.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Chris Smith - 20 Oct 2005 18:51 GMT > 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.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
John Currier - 20 Oct 2005 19:37 GMT > > The bugParade entry for this request has been closed with a 'fixed' > > status: [quoted text clipped - 23 lines] > Chris Smith - Lead Software Developer/Technical Trainer > MindIQ Corporation Thanks for the clarification Chris.
John http://schemapsy.sourceforge.net
jeremiah johnson - 20 Oct 2005 04:44 GMT > I need a simple way to mask a string (password field) while inputing > characters on the command line. It can either cover the string with '*' > or just not advance the cursor. > > Does anyone have any ideas? I have not done this in Java, but assuming your program is running when the user is to enter the password, couldn't you just turn off local echo while you're prompting for a password?
again, i'm not familiar with Java's text input facilities, but I'd think turning off local echo would give you exactly what you wanted.
jeremiah
djbitchpimp@snowboard.com - 20 Oct 2005 05:15 GMT Just to clarify, it is a console application - all input and output is done on the console. So JOptionPane/JPassWordField will not work.
Andrew Thompson - 20 Oct 2005 05:36 GMT > Just to clarify, it is a console application - all input and output is > done on the console. What is that? A *rule*?
I do not get it.
>So JOptionPane/JPassWordField will not work. *Technically*, as the problem has been described to my understanding, it will. The code example I posted started from 'the console', and ended handing control back to it.
Where's the problem?
John Currier - 20 Oct 2005 06:00 GMT > > Just to clarify, it is a console application - all input and output is > > done on the console. [quoted text clipped - 10 lines] > > Where's the problem? I believe what you just described is a GUI program, not a command-line (i.e. console) program. As soon as you introduce a graphical user interface then, by definition, you've got a GUI.
John http://schemaspy.sourceforge.net
Roedy Green - 20 Oct 2005 09:24 GMT >> Just to clarify, it is a console application - all input and output is >> done on the console. > >What is that? A *rule*? One problem with using JPasswordField is as soon as you use even that one widget you will drag in a ton of related GUI junk that will stay loaded till the end of execution.
I think the easiest answer is a little C utility to collect the password and stuff it in the environment where you can extract it and add it as a system property or argument to the command line.
See http://mindprod.com/jgloss/environment.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Andrew Thompson - 20 Oct 2005 09:32 GMT >>>Just to clarify, it is a console application - all input and output is >>>done on the console. [quoted text clipped - 4 lines] > one widget you will drag in a ton of related GUI junk that will stay > loaded till the end of execution. (shrugs) ..and what? It takes but a moment, and what does it matter if it loads a few Meg of classes that remain in memory? I'd posit that any program that crashes with OOME's when a JOptionPane is shown, is already suffering memory problems.
( Your words carry the taint of 'premature optimisation', the way I read them. )
Roedy Green - 20 Oct 2005 12:14 GMT >( Your words carry the taint of 'premature optimisation', >the way I read them. ) Poor old Knuth, when he dies will roll in his grave, when he learns how his words have been interpreted so often to mean "Writing slow programs deliberately is a mark of virtue." like the way people in medieval times bragged about how rarely they bathed.
It is not wicked to know what will run quickly or slowly or to even spread that knowledge. If you can optimise early without extra effort that is a GOOD thing. That is not premature.
"Premature" does not mean "early" or "design-stage".
What Knuth was railing against was micro coding in optimisations that a modern compiler does for you, making the code harder to read and maintain, without even determining if that code was a bottleneck.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Andrew Thompson - 20 Oct 2005 14:58 GMT >>( Your words carry the taint of 'premature optimisation', >>the way I read them. ) > > Poor old Knuth, when he dies will roll in his grave, when he learns > how his words have been interpreted so often to mean "Writing slow > programs .. If it were 'slow', I would not recommend it. What are your benchmark's revealing? How much time does it take?
<but then..> What is your definition of slow? </but then..>
zero - 20 Oct 2005 14:23 GMT Andrew Thompson <seemysites@www.invalid> wrote in news:JCI5f.23130 $U51.22901@news-server.bigpond.net.au:
>>>>Just to clarify, it is a console application - all input and output is >>>>done on the console. [quoted text clipped - 12 lines] > ( Your words carry the taint of 'premature optimisation', > the way I read them. ) I think you (and some other people) are forgetting that some platforms simply do not have a graphical interface. It is perfectly reasonable (and in fact quite common among expert users) to install Linux without installing the graphical components. And you would still have a completely functional multi-user, multi-tasking environment - just without the windows. And without an underlying window manager, Java cannot show any frames, dialogs, etc.
Andrew Thompson - 20 Oct 2005 14:58 GMT > Andrew Thompson <seemysites@www.invalid> wrote >>> [quoted text clipped - 8 lines] >> >>(shrugs) ..and what? It takes but a moment, ...
> I think you (and some other people) are forgetting that some platforms > simply do not have a graphical interface. 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')
John Currier - 20 Oct 2005 16:08 GMT > >>Roedy Green wrote: > >>> [quoted text clipped - 19 lines] > (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
John Currier - 20 Oct 2005 13:45 GMT > >> Just to clarify, it is a console application - all input and output is > >> done on the console. [quoted text clipped - 10 lines] > > See http://mindprod.com/jgloss/environment.html If you add the password to the command line then anyone on the machine can see it (assuming Unix) by looking at the output of ps (another extremely useful command-line tool).
John http://schemaspy.sourceforge.net
Gordon Beaton - 20 Oct 2005 07:51 GMT > Just to clarify, it is a console application - all input and output is > done on the console. So JOptionPane/JPassWordField will not work. If you can control the console input mode (for example, with stty on a unix-like platform) then you can turn off local echo as another poster has already suggested. I've posted sample code to do this several times in the past:
http://groups.google.com/group/comp.lang.java.programmer/msg/a132c7feda18187a
/gordon
 Signature [ do not email me copies of your followups ] g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 20 Oct 2005 09:30 GMT >If you can control the console input mode (for example, with stty on a >unix-like platform) then you can turn off local echo as another poster >has already suggested. I've posted sample code to do this several >times in the past: I don't think that would work in windows. Local echo suppression would be a command to the command interpreter which is not even necessarily running at the point your program is. If it works in Unix, that means Java implements the console in a more native way.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Gordon Beaton - 20 Oct 2005 10:25 GMT > I don't think that would work in windows. Local echo suppression > would be a command to the command interpreter which is not even > necessarily running at the point your program is. If it works in > Unix, that means Java implements the console in a more native way. Java doesn't implement the text console that it runs in on either platform, the OS does. Java reads stdin from a file descriptor in both cases.
It has nothing to do with the command interpreter either. The OS provides the console as a logical device (e.g. /dev/tty on unix, con: or something like that on windows) that the process communicates with through the file descriptor. Changing the console mode is a simple matter of configuring the device to behave differently (assuming that it has that capability).
On Unix and similar platforms like Linux, there is a set of APIs for configuring the console (unavailable to Java), and a command line tool "stty" (which can be used either from within the Java application, or in the shell prior to starting the application).
Potientially the mechanism should work on Windows if the console has such a capability and there is a way to manage it from an application, however I have no idea what facilities might be available. That doesn't prevent me from posting a solution that is sufficient for some users though.
/gordon
 Signature [ do not email me copies of your followups ] g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 20 Oct 2005 12:18 GMT >Java doesn't implement the text console that it runs in on either >platform, the OS does. how do you know this?
Obviously the command interpreter is not there.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Gordon Beaton - 20 Oct 2005 13:09 GMT > how do you know this? Basic understanding of how an operating system works. Stdin and stdout look the same for every process. Redirection works as expected, without any help from the application. No process creates the environment it's run within. Some experience with windows too, I'm afraid.
I don't have an authoritative document to refer to if that's what you mean.
/gordon
 Signature [ do not email me copies of your followups ] g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 20 Oct 2005 09:18 GMT >Just to clarify, it is a console application - all input and output is >done on the console. So JOptionPane/JPassWordField will not work. Also keep in mind the DOS box console in a window of a program referred to as the command interpreter. I think the Java console is just a window of a Java Application. They have almost nothing in common other than a similar Spartan LAF.
You can't run programs in the Java console. None of the console display and colour configuring commands affect it. It is unaffected by your font and window size choices in the command interpreter.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Thomas Weidenfeller - 20 Oct 2005 11:14 GMT > Just to clarify, it is a console application - all input and output is > done on the console. So JOptionPane/JPassWordField will not work. Java is not well suited for console applications at all. If fact, it provides nothing, absolutely nothing for a serious console application.
Consider
- Another language - seriously
- An additional, JNI based, platform-specific text API library like JCurses, or
- to change the application to a GUI application.
/Thomas
 Signature The comp.lang.java.gui FAQ: ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
jeremiah johnson - 20 Oct 2005 21:42 GMT 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.
-- jeremiah();
> I need a simple way to mask a string (password field) while inputing > characters on the command line. It can either cover the string with '*' > or just not advance the cursor. > > Does anyone have any ideas?
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|