how to write a password routine that doesn't echo to the screen?

P

Pflanzen Gold

Hi,

When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?

I am running mingw on an XP platform,

Thanks,

Pflanzen.
 
K

Keith Thompson

When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?

I am running mingw on an XP platform,

It can't be done portably in C. For non-portable solutions, try a
system-specific newsgroup.
 
J

Joona I Palaste

Pflanzen Gold said:
When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?

You can't do it, within the limits of ISO standard C code. You have to
use OS-specific APIs.
 
C

CBFalconer

Pflanzen said:
When entering a password we don't really see the characters - we see
**** instead. How can I write such a routine in C? How can I 'echo
off' the inputs? How can I get a characters/strings from the keyboard
without typing them and ENTER at the end? Like read(kbd,...) in BASIC?

I am running mingw on an XP platform,

You can't do it in standard portable C, as discussed here. Any
specific system will probably have means, so you need to find a
newsgroup that discusses your system. The resultant code will not
be portable, and thus is off-topic here.
 
M

Moonie

the topic: he asked how to write the function for mingw for xp, i happe
to answer his question. which was on topic. maybe the wrong forum, bu
it is a forum on c. so i don't understand. i don;t happen to see
mingw forum. i assume that this forums fits in all categories and al
compilers on c programming where the other one doesn't fit. kinda lik
the c++ forum, i do see a vc++ forum. i assume that the c and c++ foru
is for everybody on general programming. i assume. i could be wrong
please tell me which forum to use. by mistake i posted dos, windows
and linux not just the mingw version. so sorry


-
Mooni
 
J

Jens.Toerring

Moonie said:
the topic: he asked how to write the function for mingw for xp, i happen
to answer his question. which was on topic. maybe the wrong forum, but
it is a forum on c. so i don't understand. i don;t happen to see a
mingw forum. i assume that this forums fits in all categories and all
compilers on c programming where the other one doesn't fit. kinda like
the c++ forum, i do see a vc++ forum. i assume that the c and c++ forum
is for everybody on general programming. i assume. i could be wrong.
please tell me which forum to use. by mistake i posted dos, windows,
and linux not just the mingw version. so sorry.

Your assuption is wrong. This group has always been about the language
C and not about some implementations of C. That's the same difference
as let's say between the abstract concept of a car and a Mercedes Benz
S320 model. So, things that can be only done using system specific
extensions to C and/or rely on some obscure compiler features are off-
topic here. For such questions there are usually enough other groups/
mailing lists that deal with that kind of problems where the experts
for that kind of questions are around and can immediately comment with
authority on it when you make mistakes in the information you post.

Now a few points concerning your getpass() functions (I re-indented
it to make it readable). First of all, it won't do you any good on
Unix (including Linux) since there's already a getpass() function
and your redefition will only make the linker unhappy. Moreover
#else
unsigned char x;
read( 0, &x, 1 );
return (int) x;
#endif

which seems to be thought to be for Linux will definitely _not_
keep the characters from appearing on the screen. And if the
user hits ^D (or the password is read from a file and read() hits
the end of the file) that will return rubbish, reading the value
of an uninitialized variable. And even if it would you would still
char* getpass( const char *prompt )
{
int ch, len, maxlen=9;

Since when there's a law that passwords aren't allowed to be
longer than 9 chars?
static char s[10], *p;

len = 0;
puts((char*)prompt);

Get rid of the useless cast. puts() expects a 'const char *'. as
its argument. And puts() also prints an additional '\n', which
usually isn't what you want in that situation.
p = s;

while ((ch = mygetch()) != '\r' && ch != '\n' && ch != 27 &&
len <= maxlen)

That keeps the user from e.g. entering 10 characters and then hitting
the backspace key several times. Instead, a password that the user
knows to be wrong gets returned automatically. And on several systems
hitting the escape key won't stop entering a password. And what
about your mygetch() function returning EOF?
{
if (ch == '\b')
{
if ( len > 0 )

That would be clearer if written as

if ( ch == '\b' && len > 0 )
{
putchar('\b');
putchar(' ');
putchar('\b');
--len;
--p;
}
}
else if (ch < 32 || ch > 127)
;
else

Why don't you simply write

else if ( ch >= 32 || ch <= 127 )

And this will of course only work with a ASCII. And at least 32
could easily be replaced by "' '". Even worse, on some systems
it might be allowed to have characters in passwords not in that
range and people having such a password won't be able to enter
theirs.
{
putchar("*");

There are several systems where nothing at all gets shown while you
enter a password (which, IMHO, is a lot better because nobody can see
how many letters your password has, which is an important piece of
information since it reduces the number of possibilities an
attacker has to try quite a lot).
*p++ = (char) ch;
++len;
}
}

*p = '\0';
return( s );
}

Regards, Jens
 
J

Joona I Palaste

(e-mail address removed)-berlin.de scribbled the following:
Why don't you simply write
else if ( ch >= 32 || ch <= 127 )

Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.
 
J

Jens.Toerring

Joona I Palaste said:
(e-mail address removed)-berlin.de scribbled the following:
Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.

Grrrrr;-) Make that

else if ( ch >= 32 && ch <= 127 )

Thanks, Joona.
Regards, Jens
 
C

CBFalconer

Moonie said:
the topic: he asked how to write the function for mingw for xp, i happen
to answer his question. which was on topic. maybe the wrong forum, but
it is a forum on c. so i don't understand. i don;t happen to see a
mingw forum. i assume that this forums fits in all categories and all
compilers on c programming where the other one doesn't fit. kinda like
the c++ forum, i do see a vc++ forum. i assume that the c and c++ forum
is for everybody on general programming. i assume. i could be wrong.
please tell me which forum to use. by mistake i posted dos, windows,
and linux not just the mingw version. so sorry.

If it isn't portable to all systems meeting the C standard, it is
OT here. Asking if something is OT (off-topic) is on-topic.
 
K

Keith Thompson

Joona I Palaste said:
(e-mail address removed)-berlin.de scribbled the following: [...]
Why don't you simply write
else if ( ch >= 32 || ch <= 127 )

Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.

<OT>i</OT>.
 
J

Joona I Palaste

Keith Thompson said:
Joona I Palaste said:
(e-mail address removed)-berlin.de scribbled the following: [...]
Why don't you simply write
else if ( ch >= 32 || ch <= 127 )

Um, because it's always going to be true? I dare you to find a number
that is neither greater-or-equal than 32 nor less-or-equal than 127.
<OT>i</OT>.

Well yes, but that's hardly going to fit in a normal char or int type,
is it? Doesn't C99 have a standard complex type? How do you represent
i in that, and how does it compare to real numbers?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
 
M

Moonie

i did say i am sorry. i typed it in about 15 minutes. you have to us
curses with noecho() if you don't want it to display. i wanted it to b
as portable as possible i used stdio which most compilers support. an
with mingw you can getch() from conio instead of mygetch(); i progra
for windows and dos console


-
Mooni
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top