public void CryptoRandomizer_BasicUsage() { CryptoRandomizer randomizer = new CryptoRandomizer(); int blockSize = 20000; int iterations = 50000; for (int i = 0; i < iterations; i++) { // Get some random bytes byte[] bytes = new byte[blockSize]; randomizer.GetBytes(bytes); Assert.IsTrue(bytes.Length == blockSize); // Get Random Int int randomInt = randomizer.Next(); // Not really testible // Get Random Int less than 5 int randomIntLess5 = randomizer.Next(5); Assert.IsTrue(randomIntLess5 < 5); // Get Random Int between 1000 - 2000 int randomIntBetween1000And1500 = randomizer.Next(1000, 2000); Assert.IsTrue(randomIntBetween1000And1500 > 999 && randomIntBetween1000And1500 < 20001); double randomDouble = randomizer.NextDouble(); // Not really testible } }
private string GetRandomDirName(List <DirectoryInfo> directories) { CryptoRandomizer randomizer = new CryptoRandomizer(); int index = randomizer.Next(0, directories.Count); return(directories[index].FullName); }
public void SymmetricEncryptor_HeavyUsage() { CryptoRandomizer random = new CryptoRandomizer(); const int Iterations = 100; const int MaxMemoryBlock = 100000; for (int i = 0; i < Iterations; i++) { int blockSize = random.Next(MaxMemoryBlock); byte[] buffer = new ByteGenerator().GenerateBytes(blockSize); string key = CryptoString.GenerateRandomText(1000); byte[] decryptedBuffer; byte[] encryptedBuffer; using (SymmetricEncryptor encryptor = new SymmetricEncryptor()) { //Encrypt encryptedBuffer = encryptor.EncryptBytes(buffer, key); // Decrypt decryptedBuffer = encryptor.DecryptBytes(encryptedBuffer, key); } // IDispose - Closes and clears the keys in memory // Assert - Check to make sure the bytes are all the same Assert.IsTrue(buffer.SequenceEqual(decryptedBuffer)); } }
/// <summary> /// Hashes a Password and returns a hash fingerprint /// </summary> /// <param name="password"></param> /// <returns></returns> public string PasswordHash(CryptoString password) { Hasher hasher = new Hasher(); string pepper; string hash; // Generate Pepper pepper = new string(Enumerable.Repeat(Options.PepperChars, 1).Select(s => s[random.Next(s.Length)]).ToArray()); // Create your hash hash = hasher.SecureHash(CryptoString.SecureStringToString(password.GetSecureString()) + Options.Salt + pepper, SecureHashDelay); return(hash); }