From 77db2af0e6b1888438c68c03a150e4ee8e753f73 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 4 Jun 2020 02:09:48 -0500 Subject: [PATCH] added serial inferface and testPublic --- cli/ui/menus/KeyRingMenu.cs | 2 +- serial/Commands.cs | 14 ++++++ serial/Serial.cs | 74 ++++++++++++++++++++++++++++++++ serial/serial.csproj | 12 ++++++ tests/identity/testPublic.cs | 63 +++++++++++++++++++++++++++ treasurechest/identity/public.cs | 34 +++++++++++++++ 6 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 serial/Commands.cs create mode 100644 serial/Serial.cs create mode 100644 serial/serial.csproj create mode 100644 tests/identity/testPublic.cs create mode 100644 treasurechest/identity/public.cs diff --git a/cli/ui/menus/KeyRingMenu.cs b/cli/ui/menus/KeyRingMenu.cs index af3e07a..37eac39 100644 --- a/cli/ui/menus/KeyRingMenu.cs +++ b/cli/ui/menus/KeyRingMenu.cs @@ -39,7 +39,7 @@ namespace treasurechestCLI } switch(choice){ case 1: - + break; case 2: break; diff --git a/serial/Commands.cs b/serial/Commands.cs new file mode 100644 index 0000000..0e53f4d --- /dev/null +++ b/serial/Commands.cs @@ -0,0 +1,14 @@ +namespace serial{ + + public class SerialCommands{ + public enum Commands{ + AddIdentity, + RemoveIdentity, + EncryptToIdentity, + DecryptFromIdentity, + UnknownCommand, + Exit + } + } + +} \ No newline at end of file diff --git a/serial/Serial.cs b/serial/Serial.cs new file mode 100644 index 0000000..daf1d12 --- /dev/null +++ b/serial/Serial.cs @@ -0,0 +1,74 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace serial +{ + class Serial + { + private static SerialCommands.Commands extractCommand(string inp){ + int[] cmd = new int[4]; + int newInt; + int counter = 0; + foreach (char c in inp.ToCharArray()){ + try{ + newInt = Int32.Parse(c.ToString()); + cmd[counter] = newInt; + counter += 1; + } + catch (System.FormatException){ + break; + } + catch (System.IndexOutOfRangeException){ + return SerialCommands.Commands.UnknownCommand; + } + } + return (SerialCommands.Commands) cmd.Sum(); + } + public static string[] extractArgs(string inp, int skip){ + try{ + inp = inp.Substring(skip); + } + catch(System.ArgumentOutOfRangeException){ + return new string[0]; + } + return inp.Split(' '); + } + + static void Main(string[] cliArgs) + { + TextWriter errorWriter = Console.Error; + string input; + SerialCommands.Commands cmd; + string cmdAndArgs; + string[] args; + int cmdDigitCount; + + while (true){ + cmd = SerialCommands.Commands.UnknownCommand; + while (cmd == SerialCommands.Commands.UnknownCommand){ + cmdAndArgs = Console.ReadLine(); + if (cmdAndArgs == null){ + cmd = SerialCommands.Commands.Exit; + break; + } + cmd = extractCommand(cmdAndArgs); + if (cmd == SerialCommands.Commands.Exit){return;} + else if (cmd == SerialCommands.Commands.UnknownCommand){break;} + cmdDigitCount = cmd.ToString().Length; + args = extractArgs(cmdAndArgs, cmdDigitCount); + } + switch(cmd){ + case SerialCommands.Commands.Exit: + return; + default: + case SerialCommands.Commands.UnknownCommand: + errorWriter.Write("Unknown command"); + break; + } + + } + } + } +} diff --git a/serial/serial.csproj b/serial/serial.csproj new file mode 100644 index 0000000..63b057f --- /dev/null +++ b/serial/serial.csproj @@ -0,0 +1,12 @@ + + + + + + + + Exe + netcoreapp3.1 + + + diff --git a/tests/identity/testPublic.cs b/tests/identity/testPublic.cs new file mode 100644 index 0000000..43dad68 --- /dev/null +++ b/tests/identity/testPublic.cs @@ -0,0 +1,63 @@ +using NUnit.Framework; +using chestcrypto.identity; +using chestcrypto; +using System; +using System.Linq; +using Sodium; + +namespace PrivateIndentityTest +{ + public class Tests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void TestPrivateIdentityGetDoublePrivateKey(){ + byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PrivateKey; + byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PrivateKey; + + byte[] combinedKey = new byte[signingKey.Length + encryptionKey.Length]; + Buffer.BlockCopy(signingKey, 0, combinedKey, 0, signingKey.Length); + Buffer.BlockCopy(encryptionKey, 0, combinedKey, signingKey.Length, encryptionKey.Length); + + DoublePrivateKey combinedLoad = new chestcrypto.DoublePrivateKey(combinedKey); + + PrivateIdentity iden = new PrivateIdentity(combinedLoad, "Picard"); + + Assert.IsTrue(Enumerable.SequenceEqual(iden.getPrivateKey().getRawDouble(), combinedLoad.getRawDouble())); + + } + + [Test] + public void TestPrivateIdentityConstructor() + { + byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PrivateKey; + byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PrivateKey; + + byte[] combinedKey = new byte[signingKey.Length + encryptionKey.Length]; + Buffer.BlockCopy(signingKey, 0, combinedKey, 0, signingKey.Length); + Buffer.BlockCopy(encryptionKey, 0, combinedKey, signingKey.Length, encryptionKey.Length); + + DoublePrivateKey combinedLoad = new chestcrypto.DoublePrivateKey(combinedKey); + + PrivateIdentity iden = new PrivateIdentity(combinedLoad, "Picard"); + Assert.AreEqual(iden.getName(), "Picard"); + Assert.AreEqual(iden.getNote(), ""); + + PrivateIdentity iden2 = new PrivateIdentity(combinedLoad, "Picard2", "test"); + Assert.AreEqual(iden2.getName(), "Picard2"); + Assert.AreEqual(iden2.getNote(), "test"); + } + + [Test] + public void TestPrivateIdenToPublic() + { + + } + + + } +} \ No newline at end of file diff --git a/treasurechest/identity/public.cs b/treasurechest/identity/public.cs new file mode 100644 index 0000000..566267e --- /dev/null +++ b/treasurechest/identity/public.cs @@ -0,0 +1,34 @@ +using chestcrypto; + +namespace chestcrypto.identity +{ + + public class PublicIdentity{ + /* + PublicIdentity is a wrapper around a DoublePublicKey providing associated metadata such as alias and note + */ + + private DoublePublicKey key; + private string name; + private string comment; // human's note + + public PublicIdentity(DoublePublicKey doublePrivateKey, string alias){ + key = doublePrivateKey; + name = alias; + comment = ""; + } + + public PublicIdentity(DoublePublicKey doublePrivateKey, string alias, string note){ + key = doublePrivateKey; + name = alias; + comment = note; + } + + public DoublePublicKey getPrivateKey(){return key;} + public string getName(){return name;} + public string getNote(){return comment;} + + + } + +} \ No newline at end of file