views:

627

answers:

4

I need to read a password from standard input and wanted std::cin not to echo the characters typed by the user...

How can I disable the echo from std::cin?

here is the code that I'm currently using:

string passwd;
cout << "Enter the password: ";
getline( cin, passwd );

Edit: I'm looking for a OS agnostic way to do this. Here there are ways to do this in both Windows and *nix.

+6  A: 

There's nothing in the standard for this.

In unix, you could write some magic bytes depending on the terminal type.

Use getpasswd if it's available.

You can system() /usr/bin/stty -echo to disable echo, and /usr/bin/stty echo to enable it (again, on unix).

This guy explains how to do it without using "stty"; I didn't try it myself.

wrang-wrang
I guess `getpasswd` would really help. But I'm looking for a way to do this without recurring to OS black magic
Vargas
+3  A: 

Only idea what i have, you could read password char by char, and after it just print backspace ("\b") and maybe '*'.

Rin
If someone is logging the terminal output to a file then the whole password will be there. Probably not a good idea, then.
Artelius
Same case as someone logging all key pressing :)
Rin
+3  A: 

If you don't care about portability, you can use _getch() in VC.

#include <iostream>
#include <string>
#include <conio.h>

int main()
{
    std::string password;
    char ch;
    const char ENTER = 13;

    std::cout << "enter the password: ";

    while((ch = _getch()) != ENTER)
    {
     password += ch;
     std::cout << '*';
    }
}

There is also getwch() for wide characters. My advice is that you use NCurse which is available in *nix systems also.

AraK
+3  A: 

@wrang-wrang answer was really good, but did not fulfill my needs, this is what my final code (which was based on this) look like:

void SetStdinEcho(bool enable = true)
{
#ifdef WIN32
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    DWORD mode;
    GetConsoleMode(hStdin, &mode);

    if( !enable )
        mode &= ~ENABLE_ECHO_INPUT;
    else
        mode |= ENABLE_ECHO_INPUT;

    SetConsoleMode(hStdin, mode );

#else
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    if( !enable )
        tty.c_lflag &= ~ECHO;
    else
        tty.c_lflag |= ECHO;

    (void) tcsetattr(STDIN_FILENO, TCSANOW, &tty);
#endif
}
Vargas