473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.
Nov 14 '05 #1
11 2090
Pflanzen Gold <pf**********@yahoo.com> scribbled the following:
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?


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
Nov 14 '05 #2
pf**********@yahoo.com (Pflanzen Gold) writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
Pflanzen Gold wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4

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
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 14 '05 #5
Moonie <Mo***********@mail.codecomments.com> wrote:
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
have to include <unistd.h>, that's where read(2) is declared.
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6
Je***********@physik.fu-berlin.de scribbled the following:
Moonie <Mo***********@mail.codecomments.com> wrote:
else if (ch < 32 || ch > 127)
;
else
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
Nov 14 '05 #7
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Je***********@physik.fu-berlin.de scribbled the following:
Moonie <Mo***********@mail.codecomments.com> wrote:
else if (ch < 32 || ch > 127)
;
else
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.


Grrrrr;-) Make that

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

Thanks, Joona.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #8
Moonie wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Je***********@physik.fu-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>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10
Keith Thompson <ks***@mib.org> scribbled the following:
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Je***********@physik.fu-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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #11

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
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: magda muskala | last post by:
hi, I have a little problem with the following part of my code. I use post method to get the password from the form . Because there is an encrypted password in mysql database, I check with $pass...
6
by: Lou | last post by:
Please can someone put me out my misery! Im trying to find a multiple user/password protection script that will redirect the specific user to a specific directory. At the moment I have set up...
2
by: Yang | last post by:
Hi Is it possible to mask console input in a c# console application? What I mean is: if I entered string "test" in the console, it'll echo "****"(or nothing) instead of the string "test". I've...
0
by: Kenneth Keeley | last post by:
Hi, How can I do the below code in ASP.Net as I wish to provide an indication to my staff on our Intranet site of when they will need to change there password. On Error Resume Next Const...
2
by: ykgoh | last post by:
Hi. I've a problem of being able to create and remove a directory but unable to write a file inside the created directory for some strange reason. I suspect that this problem could be vaguely...
14
by: student_steve | last post by:
Hey guys, here is some code for a password security measure in a website: <?php session_start(); $errorMessage = ''; if (isset($_POST) && isset($_POST)) { if ($_POST === 'steven' && $_POST...
6
by: chsadaki | last post by:
Hello I have a problem in retrieving a row form a table that I created in mysql db. I insert these values in the table 'Bell',password('123'). But the problem is in my php application I cant...
20
by: _mario.lat | last post by:
hallo, I use PHP and I'd like to not write in hardcoded way password and login to access to mysql. how to not write password in code for access to mysql? How can I do? I'd like that who see my...
2
by: emerth | last post by:
Hi, I have a command line PHP script that logs into a network service with a user name specified on the command line. The script then prompts for a password. Currently the password is echoed...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.