public void TestDecryptText_DecryptionSuccessful_ReturnsExpectedResult()
        {
            var input = new byte[] { 1, 2, 3 };
            var key = new byte[] { 8, 9, 10 };
            var iv = new byte[] { 4, 5, 6 };

            var inputBase64 = Convert.ToBase64String(input);
            var keyBase64 = Convert.ToBase64String(key);
            var ivBase64 = Convert.ToBase64String(iv);

            var cleanData = new byte[] { 1, 2, 3 };

            var mockedAesWrapper = new Mock<IAesManagedWrapper>();
            mockedAesWrapper.Setup(aes => aes.Decrypt(input, key, iv))
                .Returns(cleanData);

            var target = new NfieldEncryptionUtility(mockedAesWrapper.Object);

            var result = target.DecryptText(inputBase64, keyBase64, ivBase64);

            Assert.Equal(Encoding.UTF8.GetString(cleanData), result);
        }
        public void TestDecryptText_DecryptionSuccessful_ReturnsExpectedResult()
        {
            var input = new byte[] { 1, 2, 3 };
            var key = new byte[] { 8, 9, 10 };
            var iv = new byte[] { 4, 5, 6 };

            var inputBase64 = Convert.ToBase64String(input);
            var keyBase64 = Convert.ToBase64String(key);
            var ivBase64 = Convert.ToBase64String(iv);

            var cleanData = new byte[] { 1, 2, 3 };

            var mockedAesWrapper = new Mock<IAesManagedWrapper>();
            mockedAesWrapper.Setup(aes => aes.Decrypt(input, key, iv))
                .Returns(cleanData);

            var target = new NfieldEncryptionUtility(mockedAesWrapper.Object);

            var result = target.DecryptText(inputBase64, keyBase64, ivBase64);

            Assert.Equal(Encoding.UTF8.GetString(cleanData), result);
        }
        public void TestEncryptText_EncryptionSuccessful_ReturnsExpectedResult()
        {
            const string input = "ABC";
            var key = new byte[] { 8, 9, 10 };

            var bytesInput = Encoding.UTF8.GetBytes(input);
            var keyBase64 = Convert.ToBase64String(key);

            var encryptedData = new byte[] { 1, 2, 3 };
            var iv = new byte[] { 8, 9, 10 };

            var mockedAesWrapper = new Mock<IAesManagedWrapper>();
            mockedAesWrapper.Setup(aes => aes.Encrypt(bytesInput, key, out iv))
                .Returns(encryptedData);

            var target = new NfieldEncryptionUtility(mockedAesWrapper.Object);

            var result = target.EncryptText(input, keyBase64);

            Assert.Equal(Convert.ToBase64String(encryptedData), result.Data);
            Assert.Equal(Convert.ToBase64String(iv), result.InitializationVector);
        }
        public void TestDecryptText_IvIsNull_Throws()
        {
            var target = new NfieldEncryptionUtility(new AesManagedWrapper());

            Assert.Throws <ArgumentNullException>(() => target.DecryptText("data", "key", null));
        }
        public void TestEncryptText_InputIsNull_Throws()
        {
            var target = new NfieldEncryptionUtility(new AesManagedWrapper());

            Assert.Throws <ArgumentNullException>(() => target.EncryptText(null, "key"));
        }
        public void TestEncryptText_EncryptionSuccessful_ReturnsExpectedResult()
        {
            const string input = "ABC";
            var key = new byte[] { 8, 9, 10 };

            var bytesInput = Encoding.UTF8.GetBytes(input);
            var keyBase64 = Convert.ToBase64String(key);

            var encryptedData = new byte[] {1, 2, 3};
            var iv = new byte[] { 8, 9, 10 };

            var mockedAesWrapper = new Mock<IAesManagedWrapper>();
            mockedAesWrapper.Setup(aes => aes.Encrypt(bytesInput, key, out iv))
                .Returns(encryptedData);

            var target = new NfieldEncryptionUtility(mockedAesWrapper.Object);

            var result = target.EncryptText(input, keyBase64);

            Assert.Equal(Convert.ToBase64String(encryptedData), result.Data);
            Assert.Equal(Convert.ToBase64String(iv), result.InitializationVector);
        }
 public void TestDecryptText_KeyIsNull_Throws()
 {
     var target = new NfieldEncryptionUtility(new AesManagedWrapper());
     Assert.Throws<ArgumentNullException>(() => target.DecryptText("data", null, "iv"));
 }
 public void TestEncryptText_InputIsNull_Throws()
 {
     var target = new NfieldEncryptionUtility(new AesManagedWrapper());
     Assert.Throws<ArgumentNullException>(() => target.EncryptText(null, "key"));
 }