Home » U++ Library support » U++ Core » ReadSecret() function for reading passwords, etc. from the console.
Re: ReadSecret() function for reading passwords, etc. from the console. [message #49219 is a reply to message #48528] |
Mon, 08 January 2018 14:07   |
Oblivion
Messages: 1210 Registered: August 2007
|
Senior Contributor |
|
|
Hello Mirek,
Improved version:
It seems that TCSADRAIN is recommended over TCSANOW, when reading secret input.
Also, I added the line: "Cout().PutEol()" to ReadSecret() function, as it should move to a new line, after it has read input.
String ReadSecret()
{
DisableEcho();
String s = ReadStdIn();
EnableEcho();
Cout().PutEol();
return s;
}
void EnableEcho(bool b)
{
#ifdef PLATFORM_POSIX
termios t;
tcgetattr(STDIN_FILENO, &t);
if(b) t.c_lflag |= ECHO;
else t.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSADRAIN, &t);
#elif PLATFORM_WIN32
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(h, &mode);
if(b) mode |= ENABLE_ECHO_INPUT;
else mode &= ~ENABLE_ECHO_INPUT;
SetConsoleMode(h, mode);
#endif
}
Best regards,
Oblviion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
|
|
|
Goto Forum:
Current Time: Mon Jun 09 22:13:21 CEST 2025
Total time taken to generate the page: 0.04527 seconds
|