<commit_before>﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cipha.Security.Cryptography.Symmetric;
using System.Security.Cryptography;
using System.Text;

namespace Cipha.Tests.Security.Cryptography.Symmetric
{
    [TestClass]
    public class SymmetricCipherTest
    {
        [TestMethod]
        public void AesBasicCipher1_SameInstance_ComparesOutput()
        {
            string plainText = "Encrypt me but don' forget me.";
            string encryptedText;
            string decryptedText;
            using(var cipher = new SymmetricCipher<AesManaged>("passwd", "<PASSWORD>"))
            {
                encryptedText = cipher.EncryptToString(plainText);
                decryptedText = cipher.DecryptToString(encryptedText);
            }

            Assert.AreEqual(plainText, decryptedText);
        }

        [TestMethod]
        public void AesBasicCipher2_SameInstance_ComparesOutput()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;
            string decryptStr;

            using(var cipher = new SymmetricCipher<AesManaged>("mypasswd", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainTextArr);
                decryptArr = cipher.Decrypt(encryptArr);
            }
            decryptStr = Encoding.UTF8.GetString(decryptArr);

            Assert.AreEqual(plainText, decryptStr);
        }

        [TestMethod]
        public void AesBasicCipher3_SameInstance_ComparesOutput()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;
            string decryptStr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>alt1337bb"))
            {
                encryptArr = cipher.Encrypt(plainText);
                decryptArr = cipher.Decrypt(encryptArr);
            }
            decryptStr = Encoding.UTF8.GetString(decryptArr);

            Assert.AreEqual(plainText, decryptStr);
        }
        [TestMethod]
        public void AesBasicCipher4_SameInstance_ComparesOutput()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            string encryptStr;
            byte[] decryptArr;
            string decryptStr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                encryptStr = cipher.EncryptToString(plainText);
                decryptArr = cipher.Decrypt(encryptStr);
            }
            decryptStr = Encoding.UTF8.GetString(decryptArr);

            Assert.AreEqual(plainText, decryptStr);
        }
        
        [ExpectedException(typeof(CryptographicException))]
        [TestMethod]
        public void AesBasicCipher_MultiInstance_WrongPasswdFail()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2;", "mysalt1337bb"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }
        }

        [TestCategory("SymmetricCipherTests")]
        [ExpectedException(typeof(CryptographicException))]
        [TestMethod]
        public void AesBasicCipher_MultiInstance_WrongSaltFail()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>bb"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }
        }

        [TestMethod]
        public void BasicCipher_AesSimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }
        }
    }
}<commit_msg>Updated tests for symmetriccipher<commit_after>﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cipha.Security.Cryptography.Symmetric;
using System.Security.Cryptography;
using System.Text;

namespace Cipha.Tests.Security.Cryptography.Symmetric
{
    [TestClass]
    public class SymmetricCipherTest
    {
        [TestMethod]
        public void AesBasicCipher1_SameInstance_ComparesOutput()
        {
            string plainText = "Encrypt me but don' forget me.";
            string encryptedText;
            string decryptedText;
            using(var cipher = new SymmetricCipher<AesManaged>("passwd", "<PASSWORD>"))
            {
                encryptedText = cipher.EncryptToString(plainText);
                decryptedText = cipher.DecryptToString(encryptedText);
            }

            Assert.AreEqual(plainText, decryptedText);
        }

        [TestMethod]
        public void AesBasicCipher2_SameInstance_ComparesOutput()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;
            string decryptStr;

            using(var cipher = new SymmetricCipher<AesManaged>("mypasswd", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainTextArr);
                decryptArr = cipher.Decrypt(encryptArr);
            }
            decryptStr = Encoding.UTF8.GetString(decryptArr);

            Assert.AreEqual(plainText, decryptStr);
        }

        [TestMethod]
        public void AesBasicCipher3_SameInstance_ComparesOutput()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;
            string decryptStr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>bb"))
            {
                encryptArr = cipher.Encrypt(plainText);
                decryptArr = cipher.Decrypt(encryptArr);
            }
            decryptStr = Encoding.UTF8.GetString(decryptArr);

            Assert.AreEqual(plainText, decryptStr);
        }
        [TestMethod]
        public void AesBasicCipher4_SameInstance_ComparesOutput()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            string encryptStr;
            byte[] decryptArr;
            string decryptStr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mys<PASSWORD>bb"))
            {
                encryptStr = cipher.EncryptToString(plainText);
                decryptArr = cipher.Decrypt(encryptStr);
            }
            decryptStr = Encoding.UTF8.GetString(decryptArr);

            Assert.AreEqual(plainText, decryptStr);
        }
        
        [ExpectedException(typeof(CryptographicException))]
        [TestMethod]
        public void AesBasicCipher_MultiInstance_WrongPasswdFail()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2;", "mysalt1337bb"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }
        }

        [TestCategory("SymmetricCipherTests")]
        [ExpectedException(typeof(CryptographicException))]
        [TestMethod]
        public void AesBasicCipher_MultiInstance_WrongSaltFail()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb44"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "mysalt1337bb"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }
        }

        [TestMethod]
        public void Constructor_ChangeKeySize_ExpectedKey()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            int keySize = 128;
            int newKeySize;
            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>", keysize: keySize))
            {
                newKeySize = cipher.Algorithm.KeySize;
            }

            Assert.AreEqual(keySize, newKeySize);
        }

        [ExpectedException(typeof(CryptographicException))]
        [TestMethod]
        public void Constructor_ChangeKeySize_FailDueToInvalidKeySize()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            int keySize = 234;
            int newKeySize;
            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>", keysize: keySize))
            {
                newKeySize = cipher.Algorithm.KeySize;
            }

            Assert.AreEqual(keySize, newKeySize);
        }

        [TestMethod]
        public void BasicCipher_AesSimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<AesManaged>("mypasswd2", "<PASSWORD>"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }

            CollectionAssert.AreEqual(plainTextArr, decryptArr);
        }
        [TestMethod]
        public void BasicCipher_RijndaelSimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<RijndaelManaged>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<RijndaelManaged>("mypasswd2", "<PASSWORD>"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }

            CollectionAssert.AreEqual(plainTextArr, decryptArr);
        }
        [TestMethod]
        public void BasicCipher_DESSimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<DESCryptoServiceProvider>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<DESCryptoServiceProvider>("mypasswd2", "<PASSWORD>"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }

            CollectionAssert.AreEqual(plainTextArr, decryptArr);
        }
        [TestMethod]
        public void BasicCipher_RC2SimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<RC2CryptoServiceProvider>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<RC2CryptoServiceProvider>("mypasswd2", "<PASSWORD>"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }

            CollectionAssert.AreEqual(plainTextArr, decryptArr);
        }
        [TestMethod]
        public void BasicCipher_TrippleDESSimpleCryption_ShouldPass()
        {
            string plainText = "Encryption is pretty fun";
            byte[] plainTextArr = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptArr;
            byte[] decryptArr;

            using (var cipher = new SymmetricCipher<TripleDESCryptoServiceProvider>("mypasswd2", "<PASSWORD>"))
            {
                encryptArr = cipher.Encrypt(plainText);
            }

            using (var cipher = new SymmetricCipher<TripleDESCryptoServiceProvider>("mypasswd2", "<PASSWORD>"))
            {
                decryptArr = cipher.Decrypt(encryptArr);
            }

            CollectionAssert.AreEqual(plainTextArr, decryptArr);
        }
    }
}
{'fraction_non_alphanumeric': 0.09299888205701509, 'fraction_numerical': 0.006218557853549469, 'mean_word_length': 1.9874765184721352, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4669984', 'n_tokens_mistral': 4010, 'n_tokens_neox': 3799, 'n_words': 692}