Skip to content

Instantly share code, notes, and snippets.

@JasonSantus
Forked from huobazi/gist:1039424
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JasonSantus/e07aaa61ce4293e4334a to your computer and use it in GitHub Desktop.
Save JasonSantus/e07aaa61ce4293e4334a to your computer and use it in GitHub Desktop.
/// <summary>
/// Gets the console secure password.
/// </summary>
/// <returns></returns>
private static SecureString GetConsoleSecurePassword( )
{
SecureString pwd = new SecureString( );
while ( true )
{
ConsoleKeyInfo i = Console.ReadKey( true );
if ( i.Key == ConsoleKey.Enter )
{
break;
}
else if ( i.Key == ConsoleKey.Backspace )
{
pwd.RemoveAt( pwd.Length - 1 );
Console.Write( "\b \b" );
}
else
{
pwd.AppendChar( i.KeyChar );
Console.Write( "*" );
}
}
return pwd;
}
/// <summary>
/// Gets the console password.
/// </summary>
/// <returns></returns>
private static string GetConsolePassword( )
{
StringBuilder sb = new StringBuilder( );
while ( true )
{
ConsoleKeyInfo cki = Console.ReadKey( true );
if ( cki.Key == ConsoleKey.Enter )
{
Console.WriteLine( );
break;
}
if ( cki.Key == ConsoleKey.Backspace )
{
if ( sb.Length > 0 )
{
Console.Write( "\b\0\b" );
sb.Length--;
}
continue;
}
Console.Write( '*' );
sb.Append( cki.KeyChar );
}
return sb.ToString( );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment