public
anonymous / Cryto-RNG vs RNG
Created

Comparison of the time difference between a cryptographically safe RNG and the standard RNG.

  • Download Gist
Cryto-RNG vs RNG
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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 );
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.