Created
August 27, 2014 17:14
-
-
Save sean-m/43e0e4222a3ef4da814c to your computer and use it in GitHub Desktop.
C# console app password entry logic. Not secured.
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
/* | |
* Allow user to enter password | |
*/ | |
Console.Write ("Please enter your password (12 - 256 chars)\nand press (Enter|Return): "); | |
bool entered = false; | |
ConsoleKeyInfo pwIn; | |
string pw; | |
StringBuilder sb = new StringBuilder (); | |
do { | |
pwIn = Console.ReadKey (); | |
if (pwIn.Key == ConsoleKey.Backspace) { | |
if (sb.Length > 0) { | |
sb.Remove (sb.Length - 1, 1); | |
} | |
} else if (pwIn.Key == ConsoleKey.Enter) { | |
if (sb.Length < 12 || sb.Length > 256) { | |
Console.Write ("\n"); | |
var color = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine ("The password must be 12 - 256 characters"); | |
Console.ForegroundColor = color; | |
sb.Clear(); | |
Console.Write ("Please enter your password: "); | |
} | |
else { | |
entered = true; | |
} | |
} else { | |
sb.Append (pwIn.KeyChar); | |
Console.Write("\b*"); | |
} | |
} while (!entered); | |
pw = sb.ToString (); | |
sb = null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment