Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DinisCruz/5420818 to your computer and use it in GitHub Desktop.
Save DinisCruz/5420818 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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