Skip to content

anonymous /Cryto-RNG vs RNG

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
Something went wrong with that request. Please try again.