treasurechest/tests/session/testSessionEncrypt.cs

41 lines
1.3 KiB
C#
Raw Normal View History

2020-05-30 08:06:08 +00:00
using NUnit.Framework;
using System;
using System.Linq;
2020-05-30 23:00:49 +00:00
using System.Text;
2020-05-30 08:06:08 +00:00
using chestcrypto.session;
2020-05-30 23:00:49 +00:00
using chestcrypto.session.crypto;
2020-05-30 08:06:08 +00:00
using chestcrypto.exceptions;
2020-05-30 23:44:02 +00:00
using chestcrypto;
2020-05-30 08:06:08 +00:00
using Sodium;
namespace sessionTestEncrypt
{
public class Tests
{
[SetUp]
public void Setup()
{
}
public long getFutureTime(int seconds){return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds;}
[Test]
public void TestEncrypt(){
2020-05-30 23:44:02 +00:00
var pair1 = PublicKeyBox.GenerateKeyPair();
byte[] publicK = pair1.PublicKey;
byte[] privateK = pair1.PrivateKey;
var pair = PublicKeyBox.GenerateKeyPair();
byte[] privKey = pair.PrivateKey;
byte[] pubKey = pair.PublicKey;
2020-05-30 23:00:49 +00:00
byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend");
2020-05-30 08:06:08 +00:00
Session session = new Session(privateK, publicK, true, 5);
2020-05-30 23:44:02 +00:00
session.setMinimumKeyExpireSeconds(10);
session.setMessageDelay((long) 25);
session.addPublic(pubKey, getFutureTime(100));
2020-05-30 23:00:49 +00:00
byte[] encrypted = SessionCrypto.encrypt(session, message);
2020-05-30 23:44:02 +00:00
byte[] decrypted = Curve25519.decrypt(privKey, publicK, encrypted);
Assert.AreEqual(decrypted, message);
2020-05-30 08:06:08 +00:00
}
}
}