开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜