Masking password input in C++

Ryan Graham
1 min readMay 10, 2020

This was surprisingly hard to find.

Photo by Caspar Camille Rubin on Unsplash

My first few Google results were Windows specific. Who writes software for Windows anymore? Not me. Hard pass.

After that the next solution depends on curses. I don’t want to link against curses. Pass.

Enter termios. It works on my MacBook. It works on Linux. No linking. No installs. Let’s go.

Let’s try it out.

$ ./a.out
Username: ryang
Password:
$

Apparently libc used to implement this in getpass(3), but it was deprecated.

This function is obsolete. Do not use it. If you want to read input
without terminal echoing enabled, see the description of the ECHO
flag in termios(3).

The general guidance is to write your own version just like we did. Okay then.

--

--