From d514a4546d9f85dc3583712c329392389e93af45 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Mon, 22 May 2017 17:35:55 -0400 Subject: [PATCH] add Utils class and Utils.ReadLineSecure() --- Utils.cs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Utils.cs diff --git a/Utils.cs b/Utils.cs new file mode 100644 index 0000000..a512dd3 --- /dev/null +++ b/Utils.cs @@ -0,0 +1,46 @@ +using System; + +namespace SteamGuard +{ + public static class Utils + { + public static string ReadLineSecure() + { + string text = ""; + ConsoleKeyInfo key; + int cursorIndex = 0; + do + { + key = Console.ReadKey(true); + if ((int)key.Key >= 65 && (int)key.Key <= 90) + { + text.Insert(cursorIndex, key.KeyChar.ToString()); + cursorIndex++; + } + else if (key.Key == ConsoleKey.Backspace && cursorIndex > 0) + { + text.Remove(cursorIndex - 1, 1); + cursorIndex--; + } + else if (key.Key == ConsoleKey.RightArrow) + { + cursorIndex++; + } + else if (key.Key == ConsoleKey.LeftArrow) + { + cursorIndex--; + } + + if (cursorIndex < 0) + { + cursorIndex = 0; + } + else if (cursorIndex > text.Length) + { + cursorIndex = text.Length - 1; + } + } while (key.Key != ConsoleKey.Enter); + return text; + } + } +}