Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
Oblivion is currently offline  Oblivion
Messages: 574
Registered: August 2007
Location: Turkey
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.


[Updated on: Fri, 14 July 2017 08:34]

Report message to a moderator

Re: ReadSecret() function for reading passwords, etc. from the console. [message #48528 is a reply to message #48525] Sat, 15 July 2017 11:06 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11991
Registered: November 2005
Ultimate Member
This is a good addition, thank you!

Mirek
Re: ReadSecret() function for reading passwords, etc. from the console. [message #49219 is a reply to message #48528] Mon, 08 January 2018 14:07 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 574
Registered: August 2007
Location: Turkey
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


Re: ReadSecret() function for reading passwords, etc. from the console. [message #49220 is a reply to message #49219] Mon, 08 January 2018 14:24 Go to previous message
mirek is currently offline  mirek
Messages: 11991
Registered: November 2005
Ultimate Member
OK. Applied.
Previous Topic: _mm_pause not defined
Next Topic: Question about SubRange.
Goto Forum:
  


Current Time: Thu Aug 22 17:50:06 CEST 2019

Total time taken to generate the page: 0.02683 seconds