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.
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 previous message
Oblivion is currently offline  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


 
Read Message
Read Message
Read Message
Read Message
Previous Topic: _mm_pause not defined
Next Topic: Question about SubRange.
Goto Forum:
  


Current Time: Tue May 07 01:19:26 CEST 2024

Total time taken to generate the page: 0.01872 seconds