Skip to content

Commit

Permalink
Add -e flag to make encrypting passwords easier for config file use
Browse files Browse the repository at this point in the history
  • Loading branch information
tslater2006 committed Sep 27, 2019
1 parent 95d9813 commit 3f9c970
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Pivet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;

namespace Pivet
{ class Program
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3f9c970

Please sign in to comment.