added generate test

This commit is contained in:
Kevin Froman 2020-05-30 18:00:49 -05:00
parent 3cb4da4004
commit abe1452200
4 changed files with 89 additions and 6 deletions

View File

@ -1,8 +1,9 @@
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading; using System.Text;
using chestcrypto.session; using chestcrypto.session;
using chestcrypto.session.crypto;
using chestcrypto.exceptions; using chestcrypto.exceptions;
using Sodium; using Sodium;
@ -22,13 +23,13 @@ namespace sessionTestEncrypt
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey; byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey; byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey; byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte message = "" byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend");
Session session = new Session(privateK, publicK, true, 5); Session session = new Session(privateK, publicK, true, 5);
SessionCrypto sessionCrypto = new SessionCrypto(session);
session.setMinimumKeyExpireSeconds(1); session.setMinimumKeyExpireSeconds(1);
session.setMessageDelay((long) 1); session.setMessageDelay((long) 1);
session.addPublic(newK, getFutureTime(9)); session.addPublic(newK, getFutureTime(9));
sessionCrypto.encrypt() session.generatePrivate();
byte[] encrypted = SessionCrypto.encrypt(session, message);
} }
} }

View File

@ -0,0 +1,76 @@
using NUnit.Framework;
using System;
using chestcrypto.session;
using chestcrypto.exceptions;
using System.Threading;
using Sodium;
namespace sessionTestGenerate
{
public class Tests
{
[SetUp]
public void Setup()
{
}
public long getFutureTime(int seconds){return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds;}
[Test]
public void TestGenerateTime(){
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey;
Session session = new Session(privateK, publicK, true, 5);
session.setMinimumKeyExpireSeconds(2);
session.setMessageDelay(1);
try{
session.getLatestPrivateKey();
}
catch(NoSessionKeyAvailable){
goto next;
}
Assert.Fail();
next:
session.generatePrivate(2);
try{
session.getLatestPrivateKey();
}
catch(NoSessionKeyAvailable){
Assert.Fail();
}
Thread.Sleep(1000);
try{
session.getLatestPrivateKey();
}
catch(System.ArgumentOutOfRangeException){
return;
}
Assert.Fail();
}
[Test]
public void TestGenerate(){
byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey;
byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey;
byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey;
Session session = new Session(privateK, publicK, true, 5);
try{
session.getLatestPrivateKey();
}
catch(NoSessionKeyAvailable){
goto next;
}
Assert.Fail();
next:
session.generatePrivate();
try{
session.getLatestPrivateKey();
}
catch(NoSessionKeyAvailable){
Assert.Fail();
}
}
}
}

View File

@ -5,9 +5,9 @@ using chestcrypto;
namespace chestcrypto.session.crypto{ namespace chestcrypto.session.crypto{
internal class SessionEncrypt{ internal class SessionCrypto{
public static byte[] Encrypt(Session activeSession, byte[] message){ public static byte[] encrypt(Session activeSession, byte[] message){
byte[] publicKey = activeSession.getLatestPublicKey(); byte[] publicKey = activeSession.getLatestPublicKey();
byte[] privateKey = activeSession.getLatestPrivateKey(); byte[] privateKey = activeSession.getLatestPrivateKey();
return Curve25519.encrypt(privateKey, publicKey, message); return Curve25519.encrypt(privateKey, publicKey, message);

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System; using System;
using Sodium;
using chestcrypto.exceptions; using chestcrypto.exceptions;
namespace chestcrypto{ namespace chestcrypto{
@ -102,6 +103,11 @@ namespace chestcrypto{
ourPrivateKeys.Add((timestamp, privateKey)); ourPrivateKeys.Add((timestamp, privateKey));
} }
public void generatePrivate(int secsToExpire = 1200){
long ts = (long) secsToExpire + getEpoch();
addPrivate(PublicKeyBox.GenerateKeyPair().PrivateKey, ts);
}
public void cleanPublic(){ public void cleanPublic(){
long epoch = getEpoch(); long epoch = getEpoch();
bool expired((long, byte[]) k){ bool expired((long, byte[]) k){