added getpass for text encryption

This commit is contained in:
Kevin Froman 2020-05-24 03:40:51 -05:00
parent 4bb9717891
commit a96285b3a4
7 changed files with 46 additions and 7 deletions

View File

@ -7,7 +7,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" /> <PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="ConsolePasswordMasker" Version="1.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -19,6 +19,7 @@ namespace treasurechestCLI {
stringInst.ENCRYPT_MENU_USE_PASSPHRASE = "Usar frase de contraseña"; stringInst.ENCRYPT_MENU_USE_PASSPHRASE = "Usar frase de contraseña";
stringInst.ENCRYPT_MENU_USE_PUBKEY = "Usar clave pública"; stringInst.ENCRYPT_MENU_USE_PUBKEY = "Usar clave pública";
stringInst.ENTER_MESSAGE_UNTIL_DONE = "Ingrese su mensaje y termine con -q en una nueva línea."; stringInst.ENTER_MESSAGE_UNTIL_DONE = "Ingrese su mensaje y termine con -q en una nueva línea.";
stringInst.PASSPHRASE = "Frase de contraseña";
} }
} }

View File

@ -21,6 +21,7 @@ namespace treasurechestCLI {
public string ENCRYPT_MENU_USE_PASSPHRASE; public string ENCRYPT_MENU_USE_PASSPHRASE;
public string ENCRYPT_MENU_USE_PUBKEY; public string ENCRYPT_MENU_USE_PUBKEY;
public string ENTER_MESSAGE_UNTIL_DONE; public string ENTER_MESSAGE_UNTIL_DONE;
public string PASSPHRASE;
public Strings(){ public Strings(){
@ -44,6 +45,7 @@ namespace treasurechestCLI {
ENCRYPT_MENU_USE_PASSPHRASE = "Use passphrase"; ENCRYPT_MENU_USE_PASSPHRASE = "Use passphrase";
ENCRYPT_MENU_USE_PUBKEY = "Use public key"; ENCRYPT_MENU_USE_PUBKEY = "Use public key";
ENTER_MESSAGE_UNTIL_DONE = "Enter your message and finish with -q on a new line."; ENTER_MESSAGE_UNTIL_DONE = "Enter your message and finish with -q on a new line.";
PASSPHRASE = "Passphrase";
break; break;
} }

21
cli/ui/getpass.cs Normal file
View File

@ -0,0 +1,21 @@
using System.IO;
using System;
namespace getpass{
public class GetPass{
private static StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
public static string getPass(string message){
Console.Write(message + ": ");
Console.SetOut(StreamWriter.Null);
Console.SetError(StreamWriter.Null);
message = Console.ReadLine();
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine();
return message;
}
}
}

View File

@ -1,5 +1,7 @@
using System.IO;
using System; using System;
using treasurechest.STDIOWrapper; using treasurechest.STDIOWrapper;
using getpass;
namespace treasurechestCLI{ namespace treasurechestCLI{
@ -7,6 +9,9 @@ namespace treasurechestCLI{
public static void EncryptMessage(){ public static void EncryptMessage(){
int choice = 0; int choice = 0;
int counter = 1; int counter = 1;
string message;
string passphrase;
translations.Strings strings = new translations.Strings(); translations.Strings strings = new translations.Strings();
string[] encryptMenuOptions = string[] encryptMenuOptions =
@ -16,6 +21,7 @@ namespace treasurechestCLI{
strings.RETURN_TO_PREVIOUS_MENU strings.RETURN_TO_PREVIOUS_MENU
}; };
while(true){ while(true){
counter = 1;
foreach(string option in encryptMenuOptions){ foreach(string option in encryptMenuOptions){
STDIO.O(counter.ToString() + ". " + option); STDIO.O(counter.ToString() + ". " + option);
counter += 1; counter += 1;
@ -39,12 +45,17 @@ namespace treasurechestCLI{
choice = encryptMenuOptions.Length; choice = encryptMenuOptions.Length;
} }
if (choice == 1){ if (choice == 1){
GetMessage.getTypedMessage(); try {
message = GetMessage.getTypedMessage();
}
catch(System.NullReferenceException){
continue;
}
passphrase = GetPass.getPass(strings.PASSPHRASE);
} }
else if (choice == encryptMenuOptions.Length){ else if (choice == encryptMenuOptions.Length){
break; break;
} }
counter = 1;
} }

View File

@ -13,7 +13,12 @@ namespace treasurechestCLI{
string line = ""; string line = "";
STDIO.O(strings.ENTER_MESSAGE_UNTIL_DONE); STDIO.O(strings.ENTER_MESSAGE_UNTIL_DONE);
while (true){ while (true){
line = Console.ReadLine(); try{
line = Console.ReadLine();
}
catch(System.ArgumentNullException){
line = "-q";
}
if (line.Equals("-q")){ if (line.Equals("-q")){
break; break;
} }

View File

@ -1,5 +1,4 @@
using Base58Check; using Base58Check;
using System;
namespace chestcrypto{ namespace chestcrypto{
@ -10,14 +9,15 @@ namespace chestcrypto{
private const string header = "CHEST-MESSAGE"; private const string header = "CHEST-MESSAGE";
private const string footer = "END-CHEST-MESSAGE."; private const string footer = "END-CHEST-MESSAGE.";
// Test simplepackTest.TestPackUnpackBytes
public static string pack(byte[] data){ public static string pack(byte[] data){
return header + Base58CheckEncoding.Encode(data) + footer; return header + Base58CheckEncoding.Encode(data) + footer;
} }
// Test simplepackTest.TestPackUnpackString
public static string pack(string data){ public static string pack(string data){
return pack(System.Text.Encoding.UTF8.GetBytes(data)); return pack(System.Text.Encoding.UTF8.GetBytes(data));
} }
// Test simplepackTest.TestPackUnpackBytes
public static byte[] unpack(string checkedBase58String){ public static byte[] unpack(string checkedBase58String){
if (! checkedBase58String.Contains(header) | ! checkedBase58String.Contains(footer)){ if (! checkedBase58String.Contains(header) | ! checkedBase58String.Contains(footer)){
throw new exceptions.InvalidSimplePackMessage("Message does not have valid header and footer"); throw new exceptions.InvalidSimplePackMessage("Message does not have valid header and footer");