Skip to content

Instantly share code, notes, and snippets.

@mohemohe
Created February 9, 2017 06:59
Show Gist options
  • Select an option

  • Save mohemohe/2b5ee720b01f6d1f50c99f1ae328dab1 to your computer and use it in GitHub Desktop.

Select an option

Save mohemohe/2b5ee720b01f6d1f50c99f1ae328dab1 to your computer and use it in GitHub Desktop.
using System;
namespace MoheMohe
{
public class PasswordHelper
{
public static string ReadFromStdIn()
{
var password = new System.Text.StringBuilder();
new Action(() =>
{
while (true)
{
var input = Console.ReadKey(true);
switch (input.Key)
{
case ConsoleKey.Enter:
return;
case ConsoleKey.Backspace:
var deleteConsole = false;
if (password.Length > 1)
{
password.Remove(password.Length - 2, 1);
deleteConsole = true;
}
else if (password.Length == 1)
{
password.Clear();
deleteConsole = true;
}
if (deleteConsole)
{
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
break;
default:
password.Append(input.KeyChar);
Console.Write("*");
break;
}
}
})();
return password.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment