Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2013 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4444790 to your computer and use it in GitHub Desktop.
Save anonymous/4444790 to your computer and use it in GitHub Desktop.
Comparison of the time difference between a cryptographically safe RNG and the standard RNG.
using System;
using System.Diagnostics;
using System.Security.Cryptography;
namespace CrytoTest
{
class Program
{
private static Random m_rng = new Random();
private static RNGCryptoServiceProvider m_crng = new RNGCryptoServiceProvider();
static void Main( string[] args )
{
int runs = 1000000;
Stopwatch sw = Stopwatch.StartNew();
RunCryto( runs );
sw.Stop();
Console.WriteLine(sw.Elapsed.ToString());
sw.Restart();
RunNormal( runs );
sw.Stop();
Console.WriteLine( sw.Elapsed.ToString() );
Console.Read();
}
private static void RunNormal( int i )
{
byte[] br = new byte[10];
for ( int j = 0; j < i; j++ )
m_rng.NextBytes( br );
}
private static void RunCryto( int i )
{
byte[] br = new byte[10];
for ( int j = 0; j < i; j++ )
m_crng.GetBytes( br );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment