Home > Backend Development > C++ > How Can I Hide User Input from the Console During Password Entry?

How Can I Hide User Input from the Console During Password Entry?

Mary-Kate Olsen
Release: 2025-01-01 13:01:10
Original
366 people have browsed it

How Can I Hide User Input from the Console During Password Entry?

Hiding User Input from Standard Input

When retrieving sensitive information like passwords from standard input, displaying the typed characters is undesirable. This article explores platform-agnostic methods to disable character echoing during input.

Code Example

Consider the following code snippet:

string passwd;
cout << "Enter the password: ";
getline(cin, passwd);
Copy after login

This code prompts the user to enter a password, but the characters typed are visibly displayed. To conceal user input, we employ platform-specific techniques outlined below.

Platform-Specific Solutions

Windows

#ifdef WIN32
#include <windows.h>
Copy after login

In Windows systems, SetConsoleMode can be used to toggle character echoing. Disable echoing by setting ENABLE_ECHO_INPUT to 0:

SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode & ~ENABLE_ECHO_INPUT);
Copy after login

Linux/macOS

#else
#include <termios.h>
Copy after login

For Linux and macOS systems, tcgetattr and tcsetattr are employed to retrieve and update terminal settings. Disable echoing by clearing the ECHO bit in the c_lflag field:

tty.c_lflag &^= ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Copy after login

Final Code

Combining these techniques, the following code provides a cross-platform solution to disable character echoing during password input:

#include <iostream>
#include <string>

void SetStdinEcho(bool enable = true) {
  #ifdef WIN32
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin, &mode);

    if (!enable)
      mode &= ~ENABLE_ECHO_INPUT;
    else
      mode |= ENABLE_ECHO_INPUT;

    SetConsoleMode(hStdin, mode);
  #else
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    if (!enable)
      tty.c_lflag &^= ECHO;
    else
      tty.c_lflag |= ECHO;

    (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty);
  #endif
}

int main() {
  SetStdinEcho(false);

  std::string password;
  std::cin >> password;

  SetStdinEcho(true);

  std::cout << password << std::endl;

  return 0;
}
Copy after login

The above is the detailed content of How Can I Hide User Input from the Console During Password Entry?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template