diff --git a/Pivet/Program.cs b/Pivet/Program.cs index cff00ac..0f12579 100644 --- a/Pivet/Program.cs +++ b/Pivet/Program.cs @@ -5,6 +5,8 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; +using System.Security; namespace Pivet { class Program @@ -39,6 +41,7 @@ static void Main(string[] args) var configFile = "config.json"; var jobToRun = ""; var wantsBuilder = false; + var passwordEncryptMode = false; ShowProgress = false; if (args.Length > 1) @@ -79,6 +82,18 @@ static void Main(string[] args) { ShowProgress = true; } + if (args[0].ToLower().Equals("-e")) + { + passwordEncryptMode = true; + } + } + + if (passwordEncryptMode) + { + Console.Write("Enter the password you want to encrypt: "); + string pass = ReadPassword('*'); + Console.WriteLine("Encrypted: " + PasswordCrypto.EncryptPassword(pass)); + return; } if (File.Exists(configFile) == false) @@ -156,6 +171,50 @@ static void Main(string[] args) Logger.Write("All done!"); } + public static string ReadPassword(char mask) + { + const int ENTER = 13, BACKSP = 8, CTRLBACKSP = 127; + int[] FILTERED = { 0, 27, 9, 10 /*, 32 space, if you care */ }; // const + + + SecureString securePass = new SecureString(); + + char chr = (char)0; + + while ((chr = System.Console.ReadKey(true).KeyChar) != ENTER) + { + if (((chr == BACKSP) || (chr == CTRLBACKSP)) + && (securePass.Length > 0)) + { + System.Console.Write("\b \b"); + securePass.RemoveAt(securePass.Length - 1); + + } + // Don't append * when length is 0 and backspace is selected + else if (((chr == BACKSP) || (chr == CTRLBACKSP)) && (securePass.Length == 0)) + { + } + + // Don't append when a filtered char is detected + else if (FILTERED.Count(x => chr == x) > 0) + { + } + + // Append and write * mask + else + { + securePass.AppendChar(chr); + System.Console.Write(mask); + } + } + + System.Console.WriteLine(); + IntPtr ptr = new IntPtr(); + ptr = Marshal.SecureStringToBSTR(securePass); + string plainPass = Marshal.PtrToStringBSTR(ptr); + Marshal.ZeroFreeBSTR(ptr); + return plainPass; + } } internal class Logger