Esempio n. 1
0
        public void CryptoPbkdf2()
        {
            const int KeyLength        = 42;
            IKeyDerivationFunction kdf = new Pbkdf2();

            ICryptoRandomService randomGenerator = CommonMocksAndStubs.CryptoRandomService();

            byte[] salt = randomGenerator.GetRandomBytes(kdf.ExpectedSaltSizeBytes);
            int    cost = kdf.RecommendedCost(KeyDerivationCostType.Low);

            SecureString password = CryptoUtils.StringToSecureString("Das ist kein gutes Passwort");

            byte[] key = kdf.DeriveKeyFromPassword(password, KeyLength, salt, cost);
            Assert.AreEqual(KeyLength, key.Length);

            // Same parameters must result in the same output
            byte[] key2 = kdf.DeriveKeyFromPassword(password, KeyLength, salt, cost);
            Assert.AreEqual(key, key2);
        }
Esempio n. 2
0
        public void MeasureTime()
        {
            IKeyDerivationFunction kdf          = new Pbkdf2();
            ICryptoRandomSource    randomSource = CommonMocksAndStubs.CryptoRandomService();
            SecureString           password     = CryptoUtils.StringToSecureString("candidate");

            byte[] salt       = randomSource.GetRandomBytes(kdf.ExpectedSaltSizeBytes);
            int    iterations = 10000;

            Stopwatch watch = new Stopwatch();

            watch.Start();
            kdf.DeriveKeyFromPassword(password, 32, salt, iterations);
            watch.Stop();
            Console.WriteLine("Pbkdf2 time for {0} iterations: {1}ms", iterations, watch.ElapsedMilliseconds);
        }