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: 1093 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: Tue May 07 01:19:26 CEST 2024
Total time taken to generate the page: 0.01872 seconds
|