Created
May 10, 2015 23:32
-
-
Save SplittyDev/13b0bc01971f6f4eff81 to your computer and use it in GitHub Desktop.
Read password from stdin
This file contains 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
public static string ReadPassword () { | |
const char PLACEHOLDER = '*'; | |
var accum = new StringBuilder (); | |
while (true) { | |
var key = Console.ReadKey (true); | |
if (key.Key == ConsoleKey.Backspace) { | |
if (accum.Length > 0) { | |
accum.Remove (accum.Length - 1, 1); | |
Console.Write ("\b \b"); | |
} | |
} else if (key.Key == ConsoleKey.Enter) { | |
Console.Write ('\n'); | |
return accum.ToString (); | |
} else { | |
Console.Write (PLACEHOLDER); | |
accum.Append (key.KeyChar); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment