work on keyring identity storage

This commit is contained in:
Kevin Froman 2020-05-17 02:49:36 -05:00
parent b678db282a
commit 4a4eb1cc61
8 changed files with 103 additions and 25 deletions

View File

@ -25,7 +25,7 @@ namespace DoubleKeyPrivateTests
new chestcrypto.DoublePrivateKey(invalid);
success = true;
}
catch (chestcrypto.InvalidDoubleKeyException){
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
Console.WriteLine("Throws properly for too small array size");
}
if (success){
@ -36,7 +36,7 @@ namespace DoubleKeyPrivateTests
new chestcrypto.DoublePrivateKey(invalid2);
success = true;
}
catch (chestcrypto.InvalidDoubleKeyException){
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
Console.WriteLine("Throws properly for too large array size");
}
if (success){

View File

@ -25,7 +25,7 @@ namespace DoubleKeyPublicTests
new chestcrypto.DoublePublicKey(invalid);
success = true;
}
catch (chestcrypto.InvalidDoubleKeyException){
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
Console.WriteLine("Throws properly for too small array size");
}
if (success){
@ -36,7 +36,7 @@ namespace DoubleKeyPublicTests
new chestcrypto.DoublePublicKey(invalid2);
success = true;
}
catch (chestcrypto.InvalidDoubleKeyException){
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
Console.WriteLine("Throws properly for too large array size");
}
if (success){

View File

@ -5,6 +5,7 @@ using Sodium;
using System.Collections.Generic;
using keyring;
using chestcrypto;
using chestcrypto.exceptions;
namespace KeyRingTests
{
@ -15,6 +16,38 @@ namespace KeyRingTests
{
}
[Test]
public void TestKeyRingStoreNoDupe(){
string tempFile = Path.GetTempFileName();
DoublePublicKey getKey(){
KeyRing keyRing = new KeyRing();
byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey;
byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PublicKey;
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);
DoublePublicKey combo = new DoublePublicKey(signingKey, encryptionKey);
return combo;
}
DoublePublicKey combo = getKey();
KeyRing keyRing = new KeyRing();
keyRing.addPublicKey(combo);
try{
keyRing.addPublicKey(combo);
Assert.Fail();
}
catch(DuplicateIdentityException){
}
List<byte[]> storedKeys = keyRing.getIdentityPublicKeys();
if (storedKeys.Count != 1){
Assert.Fail();
}
}
[Test]
public void TestKeyRingStore()
{
@ -33,16 +66,18 @@ namespace KeyRingTests
bool success = false;
storedKeys.ForEach(delegate(byte[] key)
{
if (key.Equals(combinedKey)){
success = true;
for (int x = 0; x < combinedKey.Length; x++){
if (combinedKey[x] == key[x]){
success = true;
continue;
}
success = false;
}
});
if (! success){
Assert.Fail();
}
}
}
}

View File

@ -17,7 +17,7 @@ namespace chestcrypto{
}
public Identity(DoublePublicKey publicKey){
this.publicKey = publicKey;
}
public Identity(DoublePrivateKey privateKey){

View File

@ -2,20 +2,39 @@ using System;
namespace chestcrypto {
public class InvalidDoubleKeyException : Exception
{
public InvalidDoubleKeyException()
namespace exceptions{
public class DuplicateIdentityException : Exception
{
public DuplicateIdentityException()
{
}
public DuplicateIdentityException(string message)
: base(message)
{
}
public DuplicateIdentityException(string message, Exception inner)
: base(message, inner)
{
}
}
public InvalidDoubleKeyException(string message)
: base(message)
public class InvalidDoubleKeyException : Exception
{
}
public InvalidDoubleKeyException()
{
}
public InvalidDoubleKeyException(string message, Exception inner)
: base(message, inner)
{
public InvalidDoubleKeyException(string message)
: base(message)
{
}
public InvalidDoubleKeyException(string message, Exception inner)
: base(message, inner)
{
}
}
}

View File

@ -14,10 +14,10 @@ namespace chestcrypto{
public DoublePrivateKey(byte[] sign, byte[] encrypt){
if (sign.Length != 64){
throw new InvalidDoubleKeyException("Signing private key must be 64 bytes in length.");
throw new exceptions.InvalidDoubleKeyException("Signing private key must be 64 bytes in length.");
}
if (encrypt.Length != 32){
throw new InvalidDoubleKeyException("Signing private key must be 32 bytes in length.");
throw new exceptions.InvalidDoubleKeyException("Signing private key must be 32 bytes in length.");
}
signingPrivateKey = sign;
encryptPrivateKey = encrypt;
@ -25,7 +25,7 @@ namespace chestcrypto{
public DoublePrivateKey(byte[] combinedKey){
if (combinedKey.Length != 96){
throw new InvalidDoubleKeyException("Invalid key length, must be 96 bytes in length");
throw new exceptions.InvalidDoubleKeyException("Invalid key length, must be 96 bytes in length");
}
for (int i = 0; i < combinedKey.Length; i++){
if (i < 64){

View File

@ -11,12 +11,18 @@ namespace chestcrypto{
public byte[] getRawDouble(){
return ByteCombiner.Combine(signingPublicKey, encryptPublicKey);
}
public byte[] getSigningPublicKey(){
return signingPublicKey;
}
public byte[] getEncryptPublicKey(){
return encryptPublicKey;
}
public DoublePublicKey(byte[] sign, byte[] encrypt){
// Construct double key from two separate byte arrays
if (sign.Length != 32 || encrypt.Length != 32){
throw new InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes");
throw new exceptions.InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes");
}
signingPublicKey = sign;
encryptPublicKey = encrypt;
@ -25,7 +31,7 @@ namespace chestcrypto{
public DoublePublicKey(byte[] joinedKey){
// Construct double key from one bytearray
if (joinedKey.Length != 64){
throw new InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes");
throw new exceptions.InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes");
}
for (int i = 0; i < joinedKey.Length; i++){
if (i < 32){

View File

@ -1,5 +1,6 @@
using chestcrypto;
using chestcrypto.identity;
using chestcrypto.exceptions;
using System.Collections.Generic;
namespace keyring{
@ -9,16 +10,29 @@ namespace keyring{
private string storageFile = null;
private List<Identity> identities = new List<Identity>();
private bool identityExists(Identity iden){
bool success = false;
identities.ForEach(delegate(Identity ident)
{
if (ident.getDoublePublicKey().Equals(iden.getDoublePublicKey())){
success = true;
return;
}
});
return success;
}
public KeyRing(string storageFile){
}
public KeyRing(){}
public List<byte[]> getIdentityPublicKeys(){
List<byte[]> pubKeys;
List<byte[]> pubKeys = new List<byte[]>();
identities.ForEach(delegate(Identity identity){
pubKeys.Add(identity.getDoublePublicKey().getRawDouble());
});
return pubKeys;
}
public void addPublicKey(DoublePublicKey key){
@ -26,7 +40,11 @@ namespace keyring{
Identity newIdentity = new Identity(key);
if (identityExists(newIdentity)){
throw new DuplicateIdentityException("An identity with that public key already exists");
}
identities.Add(newIdentity);
}