/// <summary>
        /// Triggers processing of CryptoConfig object.
        /// Passes Config object to processing class constructors.
        /// </summary>
        /// <param name="plainText">string coming from MainView's TextEditor.</param>
        /// <param name="config">CryptoConfig object holding all config and cipher parameters.</param>
        /// <returns>updated CryptoConfig object</returns>
        public static CryptoConfig ProcessConfigOnSave(string plainText, CryptoConfig config)
        {
            if (config.IsEncryptActive)
            {
                if (config.IsPbeActive)
                {
                    var pbeBuilder = CryptoFactory.CreatePbe(config);
                    config = pbeBuilder.GenerateKeyBytes();
                }

                var cipherBuilder = CryptoFactory.CreateCipher(config);
                config = cipherBuilder.EncryptTextToBytes(plainText);
            }

            if (!config.IsIntegrityActive)
            {
                return(config);
            }

            if (config.Integrity == Integrity.Dsa)
            {
                var certBuilder = CryptoFactory.CreateCert(config);
                certBuilder.GenerateCerts();
                config = certBuilder.SignInput(config.Cipher);
            }
            else
            {
                var digestBuilder = CryptoFactory.CreateDigest(config);
                config = digestBuilder.SignInput(config.Cipher);
            }

            return(config);
        }
        public void TestInvalidDigestCreateCall()
        {
            var config = new CryptoConfig {
            };

            Assert.Throws <ArgumentException>(
                () => CryptoFactory.CreateDigest(config));
        }
        public void TestInvalidCertCreateConfig()
        {
            var config = new CryptoConfig
            {
                IsIntegrityActive = true,
                Integrity         = Integrity.Dsa
            };

            Assert.Throws <ArgumentException>(
                () => CryptoFactory.CreateDigest(config));
        }
        /// <summary>
        /// Triggers processing of CryptoConfig object.
        /// Passes Config object to processing class constructors.
        /// Also verifies integrity of cipher if used.
        /// </summary>
        /// <param name="config">config state after loading all files from disk.</param>
        /// <returns>decrypted cipher in string representation.</returns>
        public static string ProcessConfigOnLoad(CryptoConfig config)
        {
            if (config.IsIntegrityActive)
            {
                if (config.Integrity == Integrity.Dsa)
                {
                    var certBuilder = CryptoFactory.CreateCert(config);
                    certBuilder.GenerateCerts();
                    config = certBuilder.SignInput(config.Cipher);

                    var result = certBuilder.VerifySign(config.Signature, config.Cipher);
                    Console.WriteLine($"Signature verified: {result}");
                }
                else
                {
                    var certBuilder = CryptoFactory.CreateDigest(config);
                    config = certBuilder.SignInput(config.Cipher);

                    var result = certBuilder.VerifySign(config.Signature, config.Cipher);
                    Console.WriteLine($"Digest verified: {result}");
                }
            }

            if (config.IsPbeActive)
            {
                var pbeBuilder = CryptoFactory.CreatePbe(config);
                config = pbeBuilder.GenerateKeyBytes();
            }

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            try
            {
                return(cipherBuilder.DecryptBytesToText(Convert.FromBase64String(config.Cipher)));
            }
            catch (Org.BouncyCastle.Crypto.InvalidCipherTextException e)
            {
                Console.WriteLine(e);
                return("GCM mac error.");
            }
            catch (FormatException e)
            {
                Console.WriteLine(e);
                return("Format error.");
            }
        }
        public void TestValidAesCmac()
        {
            var config = new CryptoConfig
            {
                IsIntegrityActive = true,
                Integrity         = Integrity.Digest,
                IntegrityOptions  = IntegrityOptions.AesCmac
            };

            var cipherBuilder = CryptoFactory.CreateDigest(config);
            var bytes         = Encoding.UTF8.GetBytes("Hallo Welt");
            var base64String  = Convert.ToBase64String(bytes);

            config = cipherBuilder.SignInput(base64String);

            var result = cipherBuilder.VerifySign(config.Signature, base64String);

            Assert.True(result);
        }