diff --git a/tests/session/testSessionEncrypt.cs b/tests/session/testSessionEncrypt.cs index 863a333..c66dd57 100644 --- a/tests/session/testSessionEncrypt.cs +++ b/tests/session/testSessionEncrypt.cs @@ -1,8 +1,9 @@ using NUnit.Framework; using System; using System.Linq; -using System.Threading; +using System.Text; using chestcrypto.session; +using chestcrypto.session.crypto; using chestcrypto.exceptions; using Sodium; @@ -22,13 +23,13 @@ namespace sessionTestEncrypt byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey; byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey; byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey; - byte message = "" + byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend"); Session session = new Session(privateK, publicK, true, 5); - SessionCrypto sessionCrypto = new SessionCrypto(session); session.setMinimumKeyExpireSeconds(1); session.setMessageDelay((long) 1); session.addPublic(newK, getFutureTime(9)); - sessionCrypto.encrypt() + session.generatePrivate(); + byte[] encrypted = SessionCrypto.encrypt(session, message); } } diff --git a/tests/session/testSessionGenerate.cs b/tests/session/testSessionGenerate.cs new file mode 100644 index 0000000..73089dc --- /dev/null +++ b/tests/session/testSessionGenerate.cs @@ -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(); + } + } + + } +} \ No newline at end of file diff --git a/treasurechest/chestcrypto/session/crypto/encrypt.cs b/treasurechest/chestcrypto/session/crypto/encrypt.cs index c0d636e..122ddf7 100644 --- a/treasurechest/chestcrypto/session/crypto/encrypt.cs +++ b/treasurechest/chestcrypto/session/crypto/encrypt.cs @@ -5,9 +5,9 @@ using chestcrypto; 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[] privateKey = activeSession.getLatestPrivateKey(); return Curve25519.encrypt(privateKey, publicKey, message); diff --git a/treasurechest/chestcrypto/session/session.cs b/treasurechest/chestcrypto/session/session.cs index 55e7998..fadecd4 100644 --- a/treasurechest/chestcrypto/session/session.cs +++ b/treasurechest/chestcrypto/session/session.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System; +using Sodium; using chestcrypto.exceptions; namespace chestcrypto{ @@ -102,6 +103,11 @@ namespace chestcrypto{ ourPrivateKeys.Add((timestamp, privateKey)); } + public void generatePrivate(int secsToExpire = 1200){ + long ts = (long) secsToExpire + getEpoch(); + addPrivate(PublicKeyBox.GenerateKeyPair().PrivateKey, ts); + } + public void cleanPublic(){ long epoch = getEpoch(); bool expired((long, byte[]) k){