added serial inferface and testPublic

This commit is contained in:
Kevin Froman 2020-06-04 02:09:48 -05:00
parent c156ce8213
commit 77db2af0e6
No known key found for this signature in database
GPG Key ID: BF1CE85F428B8CE3
6 changed files with 198 additions and 1 deletions

View File

@ -39,7 +39,7 @@ namespace treasurechestCLI
}
switch(choice){
case 1:
break;
case 2:
break;

14
serial/Commands.cs Normal file
View File

@ -0,0 +1,14 @@
namespace serial{
public class SerialCommands{
public enum Commands{
AddIdentity,
RemoveIdentity,
EncryptToIdentity,
DecryptFromIdentity,
UnknownCommand,
Exit
}
}
}

74
serial/Serial.cs Normal file
View File

@ -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;
}
}
}
}
}

12
serial/serial.csproj Normal file
View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\treasurechest\treasurechest.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -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()
{
}
}
}

View File

@ -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;}
}
}