added serial inferface and testPublic
This commit is contained in:
parent
c156ce8213
commit
77db2af0e6
@ -39,7 +39,7 @@ namespace treasurechestCLI
|
||||
}
|
||||
switch(choice){
|
||||
case 1:
|
||||
|
||||
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
|
14
serial/Commands.cs
Normal file
14
serial/Commands.cs
Normal 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
74
serial/Serial.cs
Normal 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
12
serial/serial.csproj
Normal 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>
|
63
tests/identity/testPublic.cs
Normal file
63
tests/identity/testPublic.cs
Normal 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()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
34
treasurechest/identity/public.cs
Normal file
34
treasurechest/identity/public.cs
Normal 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;}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user