How to hide input via CLI with C++
Both displaying empty and displaying asterisks during user typing.
Often we are developing an application that interacts with the user asking for the password and if he is doing it in the presence of someone or recording a video, the password will appear.
On most systems like UNIX this is already hidden by default. So let’s see how to do it with C++ .
Hiding the password
For this we will need to include the following headers:
iostream is already known and commonly used by us.
termios.h is a library that contains the definitions used by the I/O interfaces of terminal , it will be with it that we will hide the typed.
unistd.h is a library that provides access to the POSIX operating system API and makes it compatible with any operating system and compiler.
After adding the headers, just:
- Get the data from the terminal and store it in a variable with the
- Create a new instance of
termios, but this time to replicate the data from the previous instance so we can change
- Hide the typed data and passing to the new instance with the
tcsetattr()function after establishing the rule:
newt.c_lflag &= ~ECHO;
- Interact and get user-entered data with
std::cin, storing in a pre-declared variable
- And finally, return the data display after finishing the program also with
tcsetattr, but to return to the initial instance.
Also in the code we added the data display (on the same line with
\r) typed just for didactic reasons.
The code in short looks like this
Compile and run:
g++ hidden.cpp && ./a.out
Hide input, show password.
If during the optional
std::cout you still want to replace the
std::string with asterisks(
*), add this snippet to your code:
It will appear:
Your password is: ************.
Hide input, show asterisks.
We can see that the command in the terminal
stty -echo (to hide the data typed in the terminal) and
stty echo to return the display do the same thing. Note that the
stty program/command makes use of the
Displaying asterisks instead of empty or letters
I see a lot of people asking this question on StackOverflow and honestly I’ve never seen an effective solution that actually solves it the way one wants, not even using the
curses.h, also compiles with:
-lcurses -ltinfo, not NCURSES) .
So, I created a way that solves this in a simple way, using
do while, excluding
getline() and using
Only the magic is on the line:
newt.c_lflag &= 'a';, I replaced the
~ECHOwith any letter/character, in this
It will appear:
Enter your password: ************, that is, asterisks instead of empty.
Input asterisks, show password.