/// <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 TestInvalidCertCreateCall()
        {
            var config = new CryptoConfig();

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

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

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

            Assert.Throws <ArgumentException>(
                () => certBuilder.SignInput(base64String));
        }
        /// <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 TestValidDsa256Cert()
        {
            var config = new CryptoConfig
            {
                IsIntegrityActive = true,
                Integrity         = Integrity.Dsa,
                IntegrityOptions  = IntegrityOptions.Sha256
            };

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

            certBuilder.GenerateCerts();
            config = certBuilder.SignInput(base64String);

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

            Assert.True(result);
        }