Implemented DoublePrivateKey
This commit is contained in:
parent
7b789a0910
commit
562bba216e
76
tests/doublekeyPrivateTest.cs
Normal file
76
tests/doublekeyPrivateTest.cs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
using NUnit.Framework;
|
||||||
|
using chestcrypto;
|
||||||
|
using System;
|
||||||
|
using Sodium;
|
||||||
|
|
||||||
|
namespace DoubleKeyPrivateTests
|
||||||
|
{
|
||||||
|
public class Tests
|
||||||
|
{
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestDoublePrivateKeyThrowsOnBadLoad()
|
||||||
|
{
|
||||||
|
byte[] invalid = {0};
|
||||||
|
byte[] invalid2 = new byte[33];
|
||||||
|
for (int i = 0; i < invalid2.Length; i++){
|
||||||
|
invalid2[i] = 1;
|
||||||
|
}
|
||||||
|
bool success = false;
|
||||||
|
try{
|
||||||
|
new chestcrypto.DoublePrivateKey(invalid);
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
catch (chestcrypto.InvalidDoubleKeyException){
|
||||||
|
Console.WriteLine("Throws properly for too small array size");
|
||||||
|
}
|
||||||
|
if (success){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
new chestcrypto.DoublePrivateKey(invalid2);
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
catch (chestcrypto.InvalidDoubleKeyException){
|
||||||
|
Console.WriteLine("Throws properly for too large array size");
|
||||||
|
}
|
||||||
|
if (success){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestDoubleKeyLoad()
|
||||||
|
{
|
||||||
|
// Test that the combined key loader loads both constructors with the same results
|
||||||
|
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);
|
||||||
|
DoublePrivateKey parameterLoad = new chestcrypto.DoublePrivateKey(signingKey, encryptionKey);
|
||||||
|
if (combinedLoad.getRawDouble().Length != parameterLoad.getRawDouble().Length){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
if (combinedLoad.getRawDouble().Length != 96){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < combinedLoad.getRawDouble().Length; i++){
|
||||||
|
if (combinedLoad.getRawDouble()[i] != parameterLoad.getRawDouble()[i]){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
if (combinedLoad.getRawDouble()[i] != combinedKey[i]){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ using chestcrypto;
|
|||||||
using System;
|
using System;
|
||||||
using Sodium;
|
using Sodium;
|
||||||
|
|
||||||
namespace DoubleKeyTests
|
namespace DoubleKeyPublicTests
|
||||||
{
|
{
|
||||||
public class Tests
|
public class Tests
|
||||||
{
|
{
|
||||||
@ -13,7 +13,39 @@ namespace DoubleKeyTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestDoublePublicKey()
|
public void TestDoublePublicKeyThrowsOnBadLoad()
|
||||||
|
{
|
||||||
|
byte[] invalid = {0};
|
||||||
|
byte[] invalid2 = new byte[33];
|
||||||
|
for (int i = 0; i < invalid2.Length; i++){
|
||||||
|
invalid2[i] = 1;
|
||||||
|
}
|
||||||
|
bool success = false;
|
||||||
|
try{
|
||||||
|
new chestcrypto.DoublePublicKey(invalid);
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
catch (chestcrypto.InvalidDoubleKeyException){
|
||||||
|
Console.WriteLine("Throws properly for too small array size");
|
||||||
|
}
|
||||||
|
if (success){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
new chestcrypto.DoublePublicKey(invalid2);
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
catch (chestcrypto.InvalidDoubleKeyException){
|
||||||
|
Console.WriteLine("Throws properly for too large array size");
|
||||||
|
}
|
||||||
|
if (success){
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestDoublePublicKeyLoad()
|
||||||
{
|
{
|
||||||
// Test that the combined key loader loads both constructors with the same results
|
// Test that the combined key loader loads both constructors with the same results
|
||||||
byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey;
|
byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey;
|
@ -2,13 +2,38 @@ namespace chestcrypto{
|
|||||||
|
|
||||||
public class DoublePrivateKey{
|
public class DoublePrivateKey{
|
||||||
|
|
||||||
private byte[] signingprivateKey;
|
private byte[] signingPrivateKey = new byte[64];
|
||||||
public byte[] encryptprivateKey;
|
private byte[] encryptPrivateKey = new byte[32];
|
||||||
|
|
||||||
|
private string signingPrivateKeyString;
|
||||||
|
private string encryptPrivateKeyString;
|
||||||
|
|
||||||
|
public byte[] getRawDouble(){
|
||||||
|
return ByteCombiner.Combine(signingPrivateKey, encryptPrivateKey);
|
||||||
|
}
|
||||||
|
|
||||||
public DoublePrivateKey(byte[] sign, byte[] encrypt){
|
public DoublePrivateKey(byte[] sign, byte[] encrypt){
|
||||||
signingprivateKey = sign;
|
if (sign.Length != 64){
|
||||||
encryptprivateKey = encrypt;
|
throw new 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.");
|
||||||
|
}
|
||||||
|
signingPrivateKey = sign;
|
||||||
|
encryptPrivateKey = encrypt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoublePrivateKey(byte[] combinedKey){
|
||||||
|
if (combinedKey.Length != 96){
|
||||||
|
throw new InvalidDoubleKeyException("Invalid key length, must be 96 bytes in length");
|
||||||
|
}
|
||||||
|
for (int i = 0; i < combinedKey.Length; i++){
|
||||||
|
if (i < 64){
|
||||||
|
signingPrivateKey[i] = combinedKey[i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
encryptPrivateKey[i - 64] = combinedKey[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,13 +14,18 @@ namespace chestcrypto{
|
|||||||
|
|
||||||
|
|
||||||
public DoublePublicKey(byte[] sign, byte[] encrypt){
|
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");
|
||||||
|
}
|
||||||
signingPublicKey = sign;
|
signingPublicKey = sign;
|
||||||
encryptPublicKey = encrypt;
|
encryptPublicKey = encrypt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DoublePublicKey(byte[] joinedKey){
|
public DoublePublicKey(byte[] joinedKey){
|
||||||
|
// Construct double key from one bytearray
|
||||||
if (joinedKey.Length != 64){
|
if (joinedKey.Length != 64){
|
||||||
throw new InvalidDoubleKeyException();
|
throw new 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){
|
||||||
|
Loading…
Reference in New Issue
Block a user