Skip to content

Instantly share code, notes, and snippets.

@anta40
Created December 4, 2014 16:00
Show Gist options
  • Save anta40/9e915053c909003a5817 to your computer and use it in GitHub Desktop.
Save anta40/9e915053c909003a5817 to your computer and use it in GitHub Desktop.
Password masking
#include <windows.h>
#include <stdio.h>
#define MAX_LEN 100
char getch(void);
int main(void){
char pswd[MAX_LEN];
char ch;
int x;
printf("Password: ");
for (x = 0; x < MAX_LEN && (ch = getch()) != '\r'; ++x ) {
pswd[x] = ch;
putch('*');
}
pswd[x] = '\0';
return 0;
}
/*
* implementasi getch(): http://www.cplusplus.com/forum/articles/19975/
*/
char getch (void){
HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
INPUT_RECORD irInputRecord;
DWORD dwEventsRead;
CHAR cChar;
while (ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
if (irInputRecord.EventType == KEY_EVENT
&& irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
&& irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
&& irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
{
cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
return cChar;
}
return EOF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment