Created
February 9, 2017 06:59
-
-
Save mohemohe/2b5ee720b01f6d1f50c99f1ae328dab1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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