Exemplo n.º 1
0
        public void Create_Message_Encrypt_Decrypt_Verify()
        {
            // Arrange:
            var          repository = new RequestRepository();
            var          request    = CryptoKey.CreateRequest(DateTime.Now);
            var          keys       = AsymmetricCryptoProvider.GenerateKeys();
            var          crypto     = new AsymmetricCryptoProvider();
            const string message    = "secret message";

            string encryptedPassword;
            string hash;
            string encryptedMessage = crypto.EncryptMessageWithKey(message, keys.PublicKey, out encryptedPassword,
                                                                   out hash);

            request.Messages.Add(new Message
            {
                MessageData = encryptedMessage, EncryptionKey = encryptedPassword, MessageHash = hash
            });

            // Act:
            repository.AddRequest(request);
            var retrieved        = repository.GetRequest(request.KeyToken);
            var retrievedMessage = retrieved.Messages.First();

            string messageDecryptionKey;

            var decryptedMessage = crypto.DecryptMessageWithKey(keys.PrivateKey, retrievedMessage.MessageData,
                                                                retrievedMessage.EncryptionKey,
                                                                retrievedMessage.MessageHash, out messageDecryptionKey);

            // Assert:
            Assert.AreEqual(message, decryptedMessage);
        }
Exemplo n.º 2
0
        public void Can_Create_AndRetrieveKeys()
        {
            var repository = new RequestRepository();
            var request    = CryptoKey.CreateRequest(DateTime.Now);

            repository.AddRequest(request);
            var retrieved = repository.GetRequest(request.KeyToken);

            Assert.AreEqual(request.KeyToken, retrieved.KeyToken);
        }
Exemplo n.º 3
0
        public void Can_Create_AndRetrieveKeysWithNotifications()
        {
            var repository = new RequestRepository();
            var request    = CryptoKey.CreateRequest(DateTime.Now);

            const string email = "*****@*****.**";

            request.Notifications.Add(new Notification {
                Email = email
            });

            repository.AddRequest(request);
            var retrieved = repository.GetRequest(request.KeyToken);

            Assert.AreEqual(email, retrieved.Notifications.First().Email);
        }
Exemplo n.º 4
0
        public void Create_Request_AttachMessage_Decrypt_Verify()
        {
            // Arrange:
            var          repository = new RequestRepository();
            var          request    = CryptoKey.CreateRequest(DateTime.Now);
            const string message    = "secret message";

            // Act:
            repository.AddRequest(request);
            repository.AttachMessageToRequest(request.KeyToken, message);

            var decryptedMessages = repository.GetDecryptedMessagesWithPrivateKey(request.KeyToken,
                                                                                  request.GetPrivateKey);

            // Assert:
            Assert.IsTrue(decryptedMessages.Count > 0);
            Assert.AreEqual(message, decryptedMessages.First().MessageData);
        }
Exemplo n.º 5
0
        public void Can_Create_AndRetrieveKeysWithMessages()
        {
            // Arrange:
            var          repository = new RequestRepository();
            var          request    = CryptoKey.CreateRequest(DateTime.Now);
            const string message    = "secret message";

            request.Messages.Add(new Message {
                MessageData = message
            });

            // Act:
            repository.AddRequest(request);
            var retrieved        = repository.GetRequest(request.KeyToken);
            var retrievedMessage = retrieved.Messages.First().MessageData;

            // Assert:
            Assert.AreEqual(message, retrievedMessage);
        }
Exemplo n.º 6
0
        public void Cannot_decrypt_locked_request()
        {
            // Arrange:
            var          repository = new RequestRepository();
            var          request    = CryptoKey.CreateRequest(DateTime.Now.AddDays(1));
            const string message    = "secret message";

            // Act:
            repository.AddRequest(request);
            repository.AttachMessageToRequest(request.KeyToken, message);

            try
            {
                var decryptedMessages = repository.GetDecryptedMessagesWithPrivateKey(request.KeyToken,
                                                                                      request.GetPrivateKey);
                Assert.Fail();
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 7
0
        public void Can_Create_AndRetrieveEncryptedMessages()
        {
            // Arrange:
            var repository = new RequestRepository();
            var request    = CryptoKey.CreateRequest(DateTime.Now);

            const string message = "secret message";

            //request.Messages.Add(new Message { MessageData = message });

            // Act:
            repository.AddRequest(request);
            repository.AttachMessageToRequest(request.KeyToken, message);

            repository = new RequestRepository();

            var messages = repository.GetEncryptedMessages(request.KeyToken, request.PrivateKeyHash);

            // Assert:
            Assert.IsTrue(messages.Count() == 1);
        }
Exemplo n.º 8
0
        public void CannotGetKeyBeforeReleaseDate()
        {
            var request = CryptoKey.CreateRequest(DateTime.Now.AddDays(1));

            Assert.IsNull(request.GetPrivateKey);
        }