开发者

How do you do masked password entry on windows using character overwriting?

开发者 https://www.devze.com 2022-12-23 17:08 出处:网络
Currently I am using this implementation to hide user input during password entry: void setEcho(bool enable) {

Currently I am using this implementation to hide user input during password entry:

void setEcho(bool enable) {
  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);
}

The user needs to be able to have positive feed back that text entry is being made. What techniques are available in a Win32 environment using C++?


The solution is to not use stdio.h but to use conio.h. The following routine solves the above problem. Note that disabling echo is no longer needed:

void scanPass(char* passwordEntry, int length) {
  int index, ch;

  for(index = 0; index < (length - 1) && ((ch = _getch()) != EOF)
                                      && (ch != 13); index++) {
    passwordEntry[index] = (char)ch;
    _putch('*');
  }

  passwordEntry[index] = '\0';
}

The answer in this case is to use the correct tool for the job. (And to know of the right tool.)

0

精彩评论

暂无评论...
验证码 换一张
取 消