Skip to content

Instantly share code, notes, and snippets.

@OrigamiTech
Created January 22, 2011 21:46
Show Gist options
  • Save OrigamiTech/791515 to your computer and use it in GitHub Desktop.
Save OrigamiTech/791515 to your computer and use it in GitHub Desktop.
Function to get password input from a CLI without displaying the password.
static string getPassword()
{
string Pass = "";
char c = Console.ReadKey(true).KeyChar;
while (c != (char)0x0D)
{
if (c == 0x08)
{
if (Pass.Length > 0)
{
Pass = Pass.Substring(0, Pass.Length - 1);
Console.CursorLeft--;
Console.Write(" ");
Console.CursorLeft--;
}
}
else
{
Pass += c;
Console.Write("*");
}
c = Console.ReadKey(true).KeyChar;
}
Console.WriteLine("");
return Pass;
}
@BenWoodford
Copy link

program PasswordHide;
uses crt;

var password                          :string;
    tmp                               :char;
    currentx,currenty                 :integer;

procedure convpass(tmp:char);
begin
     password:=concat(password,tmp);
     gotoxy(currentx,currenty);
     write('*');
     currentx:=currentx+1;
end;

begin
     clrscr;
     write('Enter Password: ');
     currentx:=16;
     currenty:=1;
     repeat
          tmp:=readkey;
          if(ord(tmp) <> 13) then
                  convpass(tmp);
     until ord(tmp) = 13;
     writeln;
     writeln('Your password is: ',password);
     readln;
end.

God dammit there's no way to get the current row so I have to set y all the time. :<

@OrigamiTech
Copy link
Author

doesn't repeat/until need begin/end blocks?

@BenWoodford
Copy link

I guess not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment