Home » U++ Library support » U++ Core » ReadSecret() function for reading passwords, etc. from the console.
ReadSecret() function for reading passwords, etc. from the console. [message #48525] |
Thu, 13 July 2017 23:06 |
Oblivion
Messages: 1092 Registered: August 2007
|
Senior Contributor |
|
|
Hello,
One thing I found in the U++ core lacking on the console-based apps side is a ReadStdIn() function with the ability to suppress echoes of the inputs from the stdin.
Please find attached the patched Stream.{h,cpp,tpp} files.
These files now contain three related funtions: ReadSecret(), EnableEcho(), and DisableEcho().
ReadSecret():
String ReadSecret()
{
DisableEcho();
String s = ReadStdIn();
EnableEcho();
return s;
}
EnableEcho():
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, TCSANOW, &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
}
DisableEcho():
void DisableEcho()
{
EnableEcho(false);
}
I also added a topic++ entry for the ReadSecret() function in the api doc file.
Tested this code on Win7-10, and Linux.
Please consider adding this -or a better version of this function- to stream utilities, since reading passwords and secret phrases from console is not uncommon.
Best regards.
Oblivion.
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
[Updated on: Fri, 14 July 2017 08:34] Report message to a moderator
|
|
|
|
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: 1092 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: Thu Apr 25 01:56:11 CEST 2024
Total time taken to generate the page: 1.07101 seconds
|