This function is for a user to enter a password in the console. It allocates a buffer with LocalAlloc and prints *s for characters the user types.

There is an ANSI MASM version and Unicode GoASM version in the archive. Here is the MASM version, since that's what most people understand.

If anyone can see any mistakes please tell me but I don't think it's worth optimising and I can't be bothered :)


; onentry : ecx <- STD_INPUT_HANDLE, edx <- STD_OUTPUT_HANDLE
; onexit : eax <- password. Use LocalFree to release.
; LPWSTR __fastcall GetPassword(HANDLE hConIn, HANDLE hConOut);
; correct decoration is @GetPassword@8
GetPassword:
push ebx
push esi
push edi
push ebp
mov esi,ecx
mov ebx,edx
; setup console mode
push eax
invoke GetConsoleMode,esi,esp
push ; save old console mode
and dword ptr ,not (ENABLE_LINE_INPUT or ENABLE_ECHO_INPUT)
push esi
call SetConsoleMode
; allocate a buffer
invoke LocalAlloc,LPTR,256 ; 256 wide
xor ebp,ebp
mov edi,eax
_getpassloop:
invoke ReadConsole,esi,ADDR chChar,1,ADDR dwAmount,NULL
mov al,
cmp al,0dh ; check for CR
jz _gotpass
cmp al,8 ; backspace
jnz @F
test ebp,ebp
jz _getpassloop ; buffer was empty already
invoke WriteConsole,ebx,ADDR szBsSpaceBs,sizeof szBsSpaceBs,ADDR dwAmount,NULL
dec ebp
mov byte ptr ,0 ; zero out last character
jmp _getpassloop
@@: cmp ebp,255
jz _getpassloop ; buffer is full
mov ,al
inc ebp
invoke WriteConsole,ebx,ADDR szPassChar,1,ADDR dwAmount,NULL
jmp _getpassloop
_gotpass:
push esi
call SetConsoleMode
invoke WriteConsole,ebx,ADDR szNewLine,2,ADDR dwAmount,NULL
mov eax,edi
pop ebp
pop edi
pop esi
pop ebx
ret


p.s. in case anyone is bothered i hereby release it into the public domain, feel free to sell it, pretend you wrote it etc.
Attachments:
Posted on 2005-06-06 14:35:03 by stormix
i will sell it and pretend i wrote it
its gonna make me millions of dollars
Posted on 2005-06-07 07:37:05 by comrade
Then make "Comrade OS" and turn it into a multinational monopoly :D
Posted on 2005-06-07 15:23:38 by QvasiModo