From a0ae3ac369b132c1950d068330062b1a602ffa8a Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 10 Sep 2017 14:00:26 -0400 Subject: [PATCH] fix #24, Unhandled exception after issuing passkey with special characters in prompt --- Utils.cs | 61 ++++++++++++-------------------------------------------- 1 file changed, 13 insertions(+), 48 deletions(-) diff --git a/Utils.cs b/Utils.cs index 7c636e8..4284700 100644 --- a/Utils.cs +++ b/Utils.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; namespace SteamGuard { @@ -6,55 +7,19 @@ namespace SteamGuard { 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)) - { - if (cursorIndex == 0 || cursorIndex == text.Length - 1) - { - text += key.KeyChar.ToString(); - } - else - { - 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--; - } + // Have bash handle the password input, because apparently it's impossible in C#, + // and we don't need to worry about windows compatibility. + string bash_cmd = @"read -s -p ""Password: "" password; echo $password"; + Process p = new Process(); + p.StartInfo.UseShellExecute = false; + p.StartInfo.FileName = "bash"; + p.StartInfo.Arguments = string.Format("-c '{0}'", bash_cmd); + p.StartInfo.RedirectStandardOutput = true; + p.Start(); - if (cursorIndex < 0) - { - cursorIndex = 0; - } - else if (text.Length == 0 && cursorIndex > text.Length) - { - cursorIndex = 0; - } - else if (text.Length != 0 && cursorIndex >= text.Length) - { - cursorIndex = text.Length - 1; - } - - // For debugging: - // Console.Title = string.Format("{0}/{1} - {2} ({3}) - {4}", cursorIndex, text.Length, key.Key.ToString(), (int)key.Key, text); - } while (key.Key != ConsoleKey.Enter); - return text; + p.WaitForExit(); + Console.WriteLine(); + return p.StandardOutput.ReadToEnd().Trim(); } } }