#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2021 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at .
*/
#endregion License Information (GPL v3)
using System;
using System.Security.Cryptography;
namespace ShareX.HelpersLib
{
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2007/september/net-matters-tales-from-the-cryptorandom
public static class RandomCrypto
{
private static readonly object randomLock = new object();
private static readonly RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
private static byte[] uint32Buffer = new byte[4];
/// Returns a non-negative random integer.
/// A 32-bit signed integer that is greater than or equal to 0 and less than System.Int32.MaxValue.
public static int Next()
{
lock (randomLock)
{
random.GetBytes(uint32Buffer);
return BitConverter.ToInt32(uint32Buffer, 0) & 0x7FFFFFFF;
}
}
/// Returns a non-negative random integer that is less than or equal to .
/// The inclusive upper bound of the random number returned.
/// A 32-bit signed integer that is greater than or equal to 0 and less than or equal to .
public static int Next(int maxValue)
{
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxValue));
}
return Next(0, maxValue);
}
/// Returns a random integer that is within a specified range.
/// The inclusive lower bound of the random number returned.
/// The inclusive upper bound of the random number returned.
/// A 32-bit signed integer that is greater than or equal to and less than or equal to .
public static int Next(int minValue, int maxValue)
{
maxValue++;
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException(nameof(minValue));
}
if (minValue == maxValue)
{
return minValue;
}
long diff = maxValue - minValue;
lock (randomLock)
{
while (true)
{
random.GetBytes(uint32Buffer);
uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
long max = 1 + (long)uint.MaxValue;
long remainder = max % diff;
if (rand < max - remainder)
{
return (int)(minValue + (rand % diff));
}
}
}
}
/// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
/// A double-precision floating point number that is greater than or equal to 0.0, and less than 1.0.
public static double NextDouble()
{
lock (randomLock)
{
random.GetBytes(uint32Buffer);
uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
return rand / (1.0 + uint.MaxValue);
}
}
/// Fills the elements of a specified array of bytes with random numbers.
/// An array of bytes to contain random numbers.
public static void NextBytes(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
lock (randomLock)
{
random.GetBytes(buffer);
}
}
public static T Pick(params T[] array)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (array.Length == 0)
{
throw new ArgumentException(nameof(array));
}
return array[Next(array.Length - 1)];
}
public static void Run(params Action[] actions)
{
Pick(actions)();
}
}
}