work on keyring identity storage
This commit is contained in:
parent
b678db282a
commit
4a4eb1cc61
@ -25,7 +25,7 @@ namespace DoubleKeyPrivateTests
|
|||||||
new chestcrypto.DoublePrivateKey(invalid);
|
new chestcrypto.DoublePrivateKey(invalid);
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
catch (chestcrypto.InvalidDoubleKeyException){
|
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
|
||||||
Console.WriteLine("Throws properly for too small array size");
|
Console.WriteLine("Throws properly for too small array size");
|
||||||
}
|
}
|
||||||
if (success){
|
if (success){
|
||||||
@ -36,7 +36,7 @@ namespace DoubleKeyPrivateTests
|
|||||||
new chestcrypto.DoublePrivateKey(invalid2);
|
new chestcrypto.DoublePrivateKey(invalid2);
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
catch (chestcrypto.InvalidDoubleKeyException){
|
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
|
||||||
Console.WriteLine("Throws properly for too large array size");
|
Console.WriteLine("Throws properly for too large array size");
|
||||||
}
|
}
|
||||||
if (success){
|
if (success){
|
||||||
|
@ -25,7 +25,7 @@ namespace DoubleKeyPublicTests
|
|||||||
new chestcrypto.DoublePublicKey(invalid);
|
new chestcrypto.DoublePublicKey(invalid);
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
catch (chestcrypto.InvalidDoubleKeyException){
|
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
|
||||||
Console.WriteLine("Throws properly for too small array size");
|
Console.WriteLine("Throws properly for too small array size");
|
||||||
}
|
}
|
||||||
if (success){
|
if (success){
|
||||||
@ -36,7 +36,7 @@ namespace DoubleKeyPublicTests
|
|||||||
new chestcrypto.DoublePublicKey(invalid2);
|
new chestcrypto.DoublePublicKey(invalid2);
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
catch (chestcrypto.InvalidDoubleKeyException){
|
catch (chestcrypto.exceptions.InvalidDoubleKeyException){
|
||||||
Console.WriteLine("Throws properly for too large array size");
|
Console.WriteLine("Throws properly for too large array size");
|
||||||
}
|
}
|
||||||
if (success){
|
if (success){
|
||||||
|
@ -5,6 +5,7 @@ using Sodium;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using keyring;
|
using keyring;
|
||||||
using chestcrypto;
|
using chestcrypto;
|
||||||
|
using chestcrypto.exceptions;
|
||||||
|
|
||||||
namespace KeyRingTests
|
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]
|
[Test]
|
||||||
public void TestKeyRingStore()
|
public void TestKeyRingStore()
|
||||||
{
|
{
|
||||||
@ -33,16 +66,18 @@ namespace KeyRingTests
|
|||||||
bool success = false;
|
bool success = false;
|
||||||
storedKeys.ForEach(delegate(byte[] key)
|
storedKeys.ForEach(delegate(byte[] key)
|
||||||
{
|
{
|
||||||
if (key.Equals(combinedKey)){
|
for (int x = 0; x < combinedKey.Length; x++){
|
||||||
|
if (combinedKey[x] == key[x]){
|
||||||
success = true;
|
success = true;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
if (! success){
|
if (! success){
|
||||||
Assert.Fail();
|
Assert.Fail();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,7 +17,7 @@ namespace chestcrypto{
|
|||||||
|
|
||||||
}
|
}
|
||||||
public Identity(DoublePublicKey publicKey){
|
public Identity(DoublePublicKey publicKey){
|
||||||
|
this.publicKey = publicKey;
|
||||||
}
|
}
|
||||||
public Identity(DoublePrivateKey privateKey){
|
public Identity(DoublePrivateKey privateKey){
|
||||||
|
|
||||||
|
@ -2,6 +2,24 @@ using System;
|
|||||||
|
|
||||||
namespace chestcrypto {
|
namespace chestcrypto {
|
||||||
|
|
||||||
|
namespace exceptions{
|
||||||
|
public class DuplicateIdentityException : Exception
|
||||||
|
{
|
||||||
|
public DuplicateIdentityException()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public DuplicateIdentityException(string message)
|
||||||
|
: base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public DuplicateIdentityException(string message, Exception inner)
|
||||||
|
: base(message, inner)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class InvalidDoubleKeyException : Exception
|
public class InvalidDoubleKeyException : Exception
|
||||||
{
|
{
|
||||||
public InvalidDoubleKeyException()
|
public InvalidDoubleKeyException()
|
||||||
@ -18,5 +36,6 @@ namespace chestcrypto {
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -14,10 +14,10 @@ namespace chestcrypto{
|
|||||||
|
|
||||||
public DoublePrivateKey(byte[] sign, byte[] encrypt){
|
public DoublePrivateKey(byte[] sign, byte[] encrypt){
|
||||||
if (sign.Length != 64){
|
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){
|
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;
|
signingPrivateKey = sign;
|
||||||
encryptPrivateKey = encrypt;
|
encryptPrivateKey = encrypt;
|
||||||
@ -25,7 +25,7 @@ namespace chestcrypto{
|
|||||||
|
|
||||||
public DoublePrivateKey(byte[] combinedKey){
|
public DoublePrivateKey(byte[] combinedKey){
|
||||||
if (combinedKey.Length != 96){
|
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++){
|
for (int i = 0; i < combinedKey.Length; i++){
|
||||||
if (i < 64){
|
if (i < 64){
|
||||||
|
@ -11,12 +11,18 @@ namespace chestcrypto{
|
|||||||
public byte[] getRawDouble(){
|
public byte[] getRawDouble(){
|
||||||
return ByteCombiner.Combine(signingPublicKey, encryptPublicKey);
|
return ByteCombiner.Combine(signingPublicKey, encryptPublicKey);
|
||||||
}
|
}
|
||||||
|
public byte[] getSigningPublicKey(){
|
||||||
|
return signingPublicKey;
|
||||||
|
}
|
||||||
|
public byte[] getEncryptPublicKey(){
|
||||||
|
return encryptPublicKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public DoublePublicKey(byte[] sign, byte[] encrypt){
|
public DoublePublicKey(byte[] sign, byte[] encrypt){
|
||||||
// Construct double key from two separate byte arrays
|
// Construct double key from two separate byte arrays
|
||||||
if (sign.Length != 32 || encrypt.Length != 32){
|
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;
|
signingPublicKey = sign;
|
||||||
encryptPublicKey = encrypt;
|
encryptPublicKey = encrypt;
|
||||||
@ -25,7 +31,7 @@ namespace chestcrypto{
|
|||||||
public DoublePublicKey(byte[] joinedKey){
|
public DoublePublicKey(byte[] joinedKey){
|
||||||
// Construct double key from one bytearray
|
// Construct double key from one bytearray
|
||||||
if (joinedKey.Length != 64){
|
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++){
|
for (int i = 0; i < joinedKey.Length; i++){
|
||||||
if (i < 32){
|
if (i < 32){
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using chestcrypto;
|
using chestcrypto;
|
||||||
using chestcrypto.identity;
|
using chestcrypto.identity;
|
||||||
|
using chestcrypto.exceptions;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace keyring{
|
namespace keyring{
|
||||||
@ -9,16 +10,29 @@ namespace keyring{
|
|||||||
private string storageFile = null;
|
private string storageFile = null;
|
||||||
private List<Identity> identities = new List<Identity>();
|
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(string storageFile){
|
||||||
|
|
||||||
}
|
}
|
||||||
public KeyRing(){}
|
public KeyRing(){}
|
||||||
|
|
||||||
public List<byte[]> getIdentityPublicKeys(){
|
public List<byte[]> getIdentityPublicKeys(){
|
||||||
List<byte[]> pubKeys;
|
List<byte[]> pubKeys = new List<byte[]>();
|
||||||
identities.ForEach(delegate(Identity identity){
|
identities.ForEach(delegate(Identity identity){
|
||||||
pubKeys.Add(identity.getDoublePublicKey().getRawDouble());
|
pubKeys.Add(identity.getDoublePublicKey().getRawDouble());
|
||||||
});
|
});
|
||||||
|
return pubKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPublicKey(DoublePublicKey key){
|
public void addPublicKey(DoublePublicKey key){
|
||||||
@ -26,7 +40,11 @@ namespace keyring{
|
|||||||
|
|
||||||
|
|
||||||
Identity newIdentity = new Identity(key);
|
Identity newIdentity = new Identity(key);
|
||||||
|
if (identityExists(newIdentity)){
|
||||||
|
throw new DuplicateIdentityException("An identity with that public key already exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
identities.Add(newIdentity);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user