Skip to content

Instantly share code, notes, and snippets.

@CristianoRC
Created October 1, 2017 03:22
Show Gist options
  • Save CristianoRC/ab09c13b589301fb40db9e1fe551e2bc to your computer and use it in GitHub Desktop.
Save CristianoRC/ab09c13b589301fb40db9e1fe551e2bc to your computer and use it in GitHub Desktop.
public static string LerSenha()
{
StringBuilder pw = new StringBuilder();
bool caracterApagado = false;
while (true)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (deletarTexto(cki))
{
if (pw.Length != 0)
{
Console.Write("\b \b");
pw.Length--;
caracterApagado = true;
}
}
else
{
caracterApagado = false;
}
if (!caracterApagado && verificarCaracterValido(cki))
{
Console.Write('•');
pw.Append(cki.KeyChar);
}
}
return pw.ToString();
}
private static bool verificarCaracterValido(ConsoleKeyInfo tecla)
{
if (char.IsLetterOrDigit(tecla.KeyChar) || char.IsPunctuation(tecla.KeyChar) ||
char.IsSymbol(tecla.KeyChar))
{
return true;
}
else
{
return false;
}
}
private static bool deletarTexto(ConsoleKeyInfo tecla)
{
if (tecla.Key == ConsoleKey.Backspace || tecla.Key == ConsoleKey.Delete)
return true;
else
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment