Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
int GUIDS_TO_CREATE = 1000000; //10000000;
//The code below was converted from https://gist.github.com/kofisarfo/5420710
//public static class CSPRNG {
//public static byte[] GetBytes(int entropy)
Func<int, byte[]> GetBytes =
(entropy)=>{
if (entropy < 8) {
throw new ArgumentOutOfRangeException("entropy",
"Entropy must be 64 bits or greater to be cryptographically secure.");
}
using (RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider()) {
byte[] _obj = new byte[entropy];
_rng.GetBytes(_obj);
return _obj;
}
};
//public static Guid GetGuid() {
Func<Guid> GetGuid =
()=>{
using (RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider()) {
byte[] _obj = new byte[16];
_rng.GetBytes(_obj);
return new Guid(_obj);
}
};
//public static long GetInt64(bool allowNegativeValue = false)
Func<bool, long> GetInt64 =
(allowNegativeValue)=>{
using (RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider())
{
byte[] _obj = new byte[8];
_rng.GetBytes(_obj);
long _num = BitConverter.ToInt64(_obj, 0);
if (!allowNegativeValue) {
_num = (_num & ~(1L << 63));
}
return _num;
}
};
// private static void RunGuidCreation(Stopwatch watch)
Action<Stopwatch> RunGuidCreation =
(watch) =>{
Console.WriteLine("==== Starting RunGuidCreation".lineBeforeAndAfter());
var guids = new List<Guid>();
for (var i = 0; i < GUIDS_TO_CREATE; i++)
{
guids.Add(Guid.NewGuid());
}
Console.WriteLine("RunGuidCreation for {0}: {1:n0} ms".format(GUIDS_TO_CREATE, watch.ElapsedMilliseconds));
};
// private static void RunCryptoGuidCreation(Stopwatch watch) {
Action<Stopwatch>RunCryptoGuidCreation=
(watch)=>{
Console.WriteLine("==== Starting RunCryptoGuidCreation".lineBeforeAndAfter());
var guids = new List<Guid>();
for (var i = 0; i < GUIDS_TO_CREATE; i++)
{
//guids.Add(CSPRNG.GetGuid());
guids.Add(GetGuid());
}
Console.WriteLine("RunCryptoGuidCreation: {0}: {1:n0} ms".format(GUIDS_TO_CREATE, watch.ElapsedMilliseconds));
};
//static void Main(string[] args)
Action Main =
()=>{
var watch = Stopwatch.StartNew();
RunGuidCreation(watch);
RunCryptoGuidCreation(watch);
};
//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.Security.Cryptography;
//using System.Threading.Tasks;
//Above is the original script converted
//Below is the capture of console out
"console out".popupWindow().add_ConsoleOut();
Main();
GUIDS_TO_CREATE = 5000;
Main();
int GUIDS_TO_CREATE = 1000000; //10000000;
//The code below was converted from https://gist.github.com/kofisarfo/5420710
//public static class CSPRNG {
//public static byte[] GetBytes(int entropy)
Func<int, byte[]> GetBytes =
(entropy)=>{
if (entropy < 8) {
throw new ArgumentOutOfRangeException("entropy",
"Entropy must be 64 bits or greater to be cryptographically secure.");
}
using (RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider()) {
byte[] _obj = new byte[entropy];
_rng.GetBytes(_obj);
return _obj;
}
};
//public static Guid GetGuid() {
Func<Guid> GetGuid =
()=>{
using (RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider()) {
byte[] _obj = new byte[16];
_rng.GetBytes(_obj);
return new Guid(_obj);
}
};
//public static long GetInt64(bool allowNegativeValue = false)
Func<bool, long> GetInt64 =
(allowNegativeValue)=>{
using (RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider())
{
byte[] _obj = new byte[8];
_rng.GetBytes(_obj);
long _num = BitConverter.ToInt64(_obj, 0);
if (!allowNegativeValue) {
_num = (_num & ~(1L << 63));
}
return _num;
}
};
// private static void RunGuidCreation(Stopwatch watch)
Action<Stopwatch> RunGuidCreation =
(watch) =>{
Console.WriteLine("");//"==== Starting RunGuidCreation".lineBeforeAndAfter());
var guids = new List<Guid>();
for (var i = 0; i < GUIDS_TO_CREATE; i++)
{
guids.Add(Guid.NewGuid());
}
Console.WriteLine("RunGuidCreation for {0}: {1:n0} ms".format(GUIDS_TO_CREATE, watch.ElapsedMilliseconds));
};
// private static void RunCryptoGuidCreation(Stopwatch watch) {
Action<Stopwatch>RunCryptoGuidCreation=
(watch)=>{
//Console.WriteLine("==== Starting RunCryptoGuidCreation".lineBeforeAndAfter());
var guids = new List<Guid>();
for (var i = 0; i < GUIDS_TO_CREATE; i++)
{
//guids.Add(CSPRNG.GetGuid());
guids.Add(GetGuid());
}
Console.WriteLine("RunCryptoGuidCreation: {0}: {1:n0} ms".format(GUIDS_TO_CREATE, watch.ElapsedMilliseconds));
};
//static void Main(string[] args)
Action Main =
()=>{
var watch = Stopwatch.StartNew();
RunGuidCreation(watch);
RunCryptoGuidCreation(watch);
};
//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.Security.Cryptography;
//using System.Threading.Tasks;
//Above is the original script converted
//Below is the capture of console out
"console out".popupWindow().add_ConsoleOut();
GUIDS_TO_CREATE = 1;
Main();
GUIDS_TO_CREATE = 1;
Main();
GUIDS_TO_CREATE = 5;
Main();
GUIDS_TO_CREATE = 25;
Main();
GUIDS_TO_CREATE = 50;
Main();
GUIDS_TO_CREATE = 100;
Main();
GUIDS_TO_CREATE = 200;
Main();
@DinisCruz

This comment has been minimized.

Copy link
Owner Author

commented Apr 19, 2013

here is this script running online: http://csharp-repl.apphb.com/37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.