public void CompareTwoNullByteArrays()
 {
     byte[] b = new byte[] { };
     Assert.IsFalse(CryptographyUtility.CompareBytes(null, null));
     Assert.IsFalse(CryptographyUtility.CompareBytes(b, null));
     Assert.IsFalse(CryptographyUtility.CompareBytes(null, b));
 }
Exemple #2
0
        private void ImportExportAssert(byte[] plainTextKey, string passphrase)
        {
            this.utility.Export(plainTextKey, Destination, passphrase);
            byte[] importKey = this.utility.Import(Destination, passphrase);

            Assert.IsTrue(CryptographyUtility.CompareBytes(plainTextKey, importKey), "key contents");
        }
        public void VerifyHashAsUnique()
        {
            byte[] hash1 = SaltedHashProvider.CreateHash(this.plainText);
            byte[] hash2 = SaltedHashProvider.CreateHash(this.plainText);

            Assert.IsFalse(CryptographyUtility.CompareBytes(hash1, hash2));
        }
Exemple #4
0
        public void CreateHMACSHA1Key()
        {
            KeyedHashAlgorithmKeyCreator keyCreator = new KeyedHashAlgorithmKeyCreator(typeof(HMACSHA1).AssemblyQualifiedName);

            Assert.AreEqual(64, keyCreator.KeyLength);
            Assert.AreEqual(64, keyCreator.GenerateKey().Length);
            Assert.IsFalse(CryptographyUtility.CompareBytes(keyCreator.GenerateKey(), keyCreator.GenerateKey()));
        }
        public void UniqueSaltedHashes()
        {
            IHashProvider hashProviderWithSalt = SaltedHashProvider;

            byte[] providerHash1 = hashProviderWithSalt.CreateHash(plainText);
            byte[] providerHash2 = hashProviderWithSalt.CreateHash(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(providerHash1, providerHash2), "compare");
        }
        public void Properties()
        {
            byte[] key = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
            KeyedHashAlgorithmProviderData data = new KeyedHashAlgorithmProviderData();

            data.Key = key;
            Assert.IsTrue(CryptographyUtility.CompareBytes(key, data.Key));
        }
        public void CompareBytesFailWithNull()
        {
            byte[] bytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Assert.IsFalse(CryptographyUtility.CompareBytes(bytes, null));
            Assert.IsFalse(CryptographyUtility.CompareBytes(null, bytes));
            Assert.IsFalse(CryptographyUtility.CompareBytes(null, null));
        }
Exemple #8
0
        public void EncryptAndDecryptBytes()
        {
            byte[] encrypted = Cryptographer.EncryptSymmetric(symmInstance, plainTextBytes);
            Assert.IsFalse(CryptographyUtility.CompareBytes(plainTextBytes, encrypted));

            byte[] decrypted = Cryptographer.DecryptSymmetric(symmInstance, encrypted);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainTextBytes, decrypted));
        }
        public void CompareBytesFail()
        {
            byte[] first  = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            byte[] second = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Assert.AreNotEqual(first, second);
            Assert.IsFalse(CryptographyUtility.CompareBytes(first, second));
        }
        public void CompareBytesPass()
        {
            byte[] first  = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            byte[] second = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Assert.AreEqual(first, second);
            Assert.IsTrue(CryptographyUtility.CompareBytes(first, second));
        }
        public void GenerateRandomBytes()
        {
            int rndSize = 16;

            byte[] rnd1 = CryptographyUtility.GetRandomBytes(rndSize);
            byte[] rnd2 = CryptographyUtility.GetRandomBytes(rndSize);

            Assert.IsFalse(CryptographyUtility.CompareBytes(rnd1, rnd2));
        }
Exemple #12
0
 public void TestConstruction()
 {
     byte[] bytes = new byte[] { 1, 2, 3, 4 };
     using (KeyAlgorithmPair pair = new KeyAlgorithmPair(bytes, "foo"))
     {
         Assert.AreEqual("foo", pair.AlgorithmTypeName);
         Assert.IsTrue(CryptographyUtility.CompareBytes(bytes, pair.Key));
     }
 }
        public void EncryptAndDecryptTest(byte[] plainText)
        {
            plainText = CryptographyUtility.GetRandomBytes(plainText.Length);

            byte[] cipherText = DefaultSymmProvider.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText), "encrypted");

            byte[] decryptedText = DefaultSymmProvider.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText), "decrypted");
        }
        public void HashSha1()
        {
            IHashProvider hashProvider = HashProviderHelper.DefaultHashProvider;
            SHA1          sha1         = SHA1Managed.Create();

            byte[] origHash     = sha1.ComputeHash(plainText);
            byte[] providerHash = hashProvider.CreateHash(plainText);

            Assert.IsTrue(CryptographyUtility.CompareBytes(origHash, providerHash));
        }
        public void HashWithSalt()
        {
            IHashProvider hashProviderWithSalt = SaltedHashProvider;
            IHashProvider hashProvider         = DefaultHashProvider;

            byte[] origHash1     = hashProvider.CreateHash(plainText);
            byte[] providerHash1 = hashProviderWithSalt.CreateHash(plainText);

            Assert.IsFalse(CryptographyUtility.CompareBytes(origHash1, providerHash1), "original");
            Assert.IsFalse(CryptographyUtility.CompareBytes(plainText, providerHash1), "plain");
        }
Exemple #16
0
        public void NewProvider()
        {
            DpapiSymmetricCryptoProvider symm = new DpapiSymmetricCryptoProvider();

            CryptographyConfigurationView cryptoConfigurationView = new TestCryptographyConfigurationView();

            symm.Initialize(cryptoConfigurationView);
            byte[] encBytes = symm.Encrypt(plainText);
            byte[] decBytes = symm.Decrypt(encBytes);
            Assert.IsTrue(CryptographyUtility.CompareBytes(decBytes, plainText));
        }
Exemple #17
0
        public void EncryptAndDecryptOneMegabyte()
        {
            byte[] megabyte = new byte[1024 * 1024];
            CryptographyUtility.GetRandomBytes(megabyte);

            byte[] encrypted = Cryptographer.EncryptSymmetric(symmInstance, megabyte);
            Assert.IsFalse(CryptographyUtility.CompareBytes(megabyte, encrypted));

            byte[] decrypted = Cryptographer.DecryptSymmetric(symmInstance, encrypted);

            Assert.IsTrue(CryptographyUtility.CompareBytes(megabyte, decrypted));
        }
        public void EncryptAndDecryptOneByte()
        {
            byte[] onebyte = new byte[1];
            CryptographyUtility.GetRandomBytes(onebyte);

            byte[] encrypted = cryptographyManager.EncryptSymmetric(symmInstance, onebyte);
            Assert.IsFalse(CryptographyUtility.CompareBytes(onebyte, encrypted));

            byte[] decrypted = cryptographyManager.DecryptSymmetric(symmInstance, encrypted);

            Assert.IsTrue(CryptographyUtility.CompareBytes(onebyte, decrypted));
        }
Exemple #19
0
        public void EncryptAndDecryptUserMode()
        {
            DpapiStorageMode   mode  = DpapiStorageMode.User;
            DpapiCryptographer dpapi = new DpapiCryptographer(mode);

            byte[] cipherText = dpapi.Encrypt(this.plainText);

            Assert.IsFalse(CryptographyUtility.CompareBytes(this.plainText, cipherText));

            byte[] decryptedText = dpapi.Decrypt(cipherText);

            Assert.IsTrue(CryptographyUtility.CompareBytes(this.plainText, decryptedText));
        }
Exemple #20
0
        public void EncryptAndDecryptUserMode()
        {
            DataProtectionScope mode  = DataProtectionScope.CurrentUser;
            DpapiCryptographer  dpapi = new DpapiCryptographer(mode);

            byte[] cipherText = dpapi.Encrypt(plainText);

            Assert.IsFalse(CryptographyUtility.CompareBytes(plainText, cipherText));

            byte[] decryptedText = dpapi.Decrypt(cipherText);

            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }
        public void CanCreatePoliciesTo_EncryptAndDecryptStringWithASymmetricAlgorithm()
        {
            Assert.IsInstanceOfType(container.Resolve <ISymmetricCryptoProvider>(symmetricAlgorithm1),
                                    typeof(SymmetricAlgorithmProvider));

            byte[] megabyte = new byte[1024 * 1024];
            CryptographyUtility.GetRandomBytes(megabyte);

            byte[] encrypted = container.Resolve <ISymmetricCryptoProvider>(symmetricAlgorithm1).Encrypt(megabyte);
            Assert.IsFalse(CryptographyUtility.CompareBytes(megabyte, encrypted));

            byte[] decrypted = container.Resolve <ISymmetricCryptoProvider>(symmetricAlgorithm1).Decrypt(encrypted);
            Assert.IsTrue(CryptographyUtility.CompareBytes(megabyte, decrypted));
        }
        public void HashMD5()
        {
            byte[]            plaintext     = new byte[] { 0, 1, 2, 3 };
            HashCryptographer cryptographer = new HashCryptographer(typeof(MD5CryptoServiceProvider));

            byte[] hash1 = cryptographer.ComputeHash(plaintext);

            Assert.IsFalse(CryptographyUtility.CompareBytes(plaintext, hash1));

            MD5 md5 = MD5CryptoServiceProvider.Create();

            byte[] hash2 = md5.ComputeHash(plaintext);

            Assert.IsTrue(CryptographyUtility.CompareBytes(hash1, hash2));
        }
Exemple #23
0
        public void ExportKeyWithNoPassword()
        {
            string passphrase = string.Empty;

            this.utility.Export(this.key, Destination, passphrase);

            string contents = GetFileContents(Destination);

            byte[] contentBytes = Convert.FromBase64String(contents);
            Assert.AreEqual(ImportExportUtility.PlainTextFlag, contentBytes[0], "assert first byte is plain text flag");

            byte[] exportedKey = new byte[contentBytes.Length - 1];
            Buffer.BlockCopy(contentBytes, 1, exportedKey, 0, contentBytes.Length - 1);
            Assert.IsTrue(CryptographyUtility.CompareBytes(this.key, exportedKey), "key contents");
        }
Exemple #24
0
        public Task <bool> IsUserNameAvailable(string userName)
        {
            /* Before comparing or evaluating the uniqueness of a "userName" or
             * "password" attribute, service providers MUST use the preparation,
             * enforcement, and comparison of internationalized strings (PRECIS)
             * preparation and comparison rules described in Sections 3 and 4,
             * respectively, of [RFC7613], which is based on the PRECIS framework
             * specification [RFC7564]. */

            var userNameBytes = Encoding.UTF8.GetBytes(userName);

            return(Task.FromResult(
                       _Users
                       .Values
                       .All(u => !CryptographyUtility.CompareBytes(Encoding.UTF8.GetBytes(u.UserName), userNameBytes))));
        }
        private byte[] CheckPassword(byte[] contentBytes, string passphrase)
        {
            byte[] passHash = GetPassphraseBytes(passphrase);

            byte[] passHashTest = new byte[PassHashLength];
            Buffer.BlockCopy(contentBytes, FlagLength, passHashTest, 0, PassHashLength);

            bool compare = CryptographyUtility.CompareBytes(passHash, passHashTest);

            if (!compare)
            {
                throw new CryptographicException(SR.DecryptPasswordCheckFailure);
            }

            return(passHash);
        }
        public void HashHMACSHA1()
        {
            byte[]            plaintext     = new byte[] { 0, 1, 2, 3 };
            HashCryptographer cryptographer = new HashCryptographer(typeof(HMACSHA1), key);

            byte[] hash1 = cryptographer.ComputeHash(plaintext);

            Assert.IsFalse(CryptographyUtility.CompareBytes(plaintext, hash1));

            KeyedHashAlgorithm hmacsha1 = HMACSHA1.Create();

            hmacsha1.Key = key.DecryptedKey;
            byte[] hash2 = hmacsha1.ComputeHash(plaintext);

            Assert.IsTrue(CryptographyUtility.CompareBytes(hash1, hash2));
        }
Exemple #27
0
        public void EncryptAndDecryptWithTypeUsingProtectedKey()
        {
            byte[] key = new byte[16];
            CryptographyUtility.GetRandomBytes(key);
            ProtectedKey protectedKey = ProtectedKey.CreateFromPlaintextKey(key, DataProtectionScope.LocalMachine);

            SymmetricCryptographer symm = new SymmetricCryptographer(typeof(RijndaelManaged), protectedKey);

            byte[] plainText = new byte[12];
            CryptographyUtility.GetRandomBytes(plainText);

            byte[] cipherText = symm.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText));

            byte[] decryptedText = symm.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }
Exemple #28
0
        /// <summary>
        /// Verifies the hash of the <paramref name="plainTextPassword" /> matches the specified <paramref name="passwordHash" />.
        /// </summary>
        /// <param name="plainTextPassword">The password to verify.</param>
        /// <param name="passwordHash">The password hash.</param>
        /// <returns>Whether or not the password is the same.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// plainTextPassword
        /// or
        /// passwordHash
        /// </exception>
        public bool VerifyHash(string plainTextPassword, string passwordHash)
        {
            if (string.IsNullOrWhiteSpace(plainTextPassword))
            {
                throw new ArgumentNullException("plainTextPassword");
            }

            if (string.IsNullOrWhiteSpace(passwordHash))
            {
                throw new ArgumentNullException("passwordHash");
            }

            var passwordHashBytes        = passwordHash.ToBytesFromHexadecimal();
            var saltBytes                = CryptographyUtility.GetBytes(passwordHashBytes, SaltLength);
            var saltedPlainTextHashBytes = CreateHashBytes(new UTF8Encoding(false).GetBytes(plainTextPassword), saltBytes);

            return(CryptographyUtility.CompareBytes(passwordHashBytes, saltedPlainTextHashBytes));
        }
Exemple #29
0
        public void EncryptAndDecryptWithType()
        {
            string alg = typeof(RijndaelManaged).AssemblyQualifiedName;

            byte[] key = new byte[16];
            CryptographyUtility.GetRandomBytes(key);

            SymmetricCryptographer symm = new SymmetricCryptographer(alg, key);

            byte[] plainText = new byte[12];
            CryptographyUtility.GetRandomBytes(plainText);

            byte[] cipherText = symm.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText));

            byte[] decryptedText = symm.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }
Exemple #30
0
        public void EncryptAndDecryptWithAlgorithm()
        {
            SymmetricAlgorithm alg = RijndaelManaged.Create();

            byte[] key = new byte[16];
            CryptographyUtility.GetRandomBytes(key);

            SymmetricCryptographer symm = new SymmetricCryptographer(alg, key);

            byte[] plainText = new byte[12];
            CryptographyUtility.GetRandomBytes(plainText);

            byte[] cipherText = symm.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText));

            byte[] decryptedText = symm.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }