Browse Source

Add support for cryptografic PRNG in netstandard1.6

Use a different way to initialize the PRNG creation for the .NET
framework as described on this SO answer:
https://stackoverflow.com/a/38644970

Add System.Security.Cryptography.Algorithms NuGet on netstandard1.6.
build
Ignas Anikevicius 9 years ago
parent
commit
8b8824dca0
  1. 7
      src/Numerics/Numerics-2017.csproj
  2. 12
      src/Numerics/Random/CryptoRandomSource.cs
  3. 6
      src/Numerics/Random/RandomSeed.cs

7
src/Numerics/Numerics-2017.csproj

@ -22,10 +22,9 @@
<Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Runtime.Serialization" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'"> <ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
<PackageReference Include="System.Runtime"> <PackageReference Include="System.Runtime" Version="4.3.0" />
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" /> <PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" /> <PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

12
src/Numerics/Random/CryptoRandomSource.cs

@ -27,7 +27,7 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
#if !PORTABLE && !NETSTANDARD1_6 #if !PORTABLE
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -51,7 +51,7 @@ namespace MathNet.Numerics.Random
/// <see cref="Control.ThreadSafeRandomNumberGenerators"/> to set whether the instance is thread safe.</remarks> /// <see cref="Control.ThreadSafeRandomNumberGenerators"/> to set whether the instance is thread safe.</remarks>
public CryptoRandomSource() public CryptoRandomSource()
{ {
_crypto = new RNGCryptoServiceProvider(); _crypto = RandomNumberGenerator.Create();
} }
/// <summary> /// <summary>
@ -71,7 +71,7 @@ namespace MathNet.Numerics.Random
/// <param name="threadSafe">if set to <c>true</c> , the class is thread safe.</param> /// <param name="threadSafe">if set to <c>true</c> , the class is thread safe.</param>
public CryptoRandomSource(bool threadSafe) : base(threadSafe) public CryptoRandomSource(bool threadSafe) : base(threadSafe)
{ {
_crypto = new RNGCryptoServiceProvider(); _crypto = RandomNumberGenerator.Create();
} }
/// <summary> /// <summary>
@ -135,12 +135,12 @@ namespace MathNet.Numerics.Random
var bytes = new byte[values.Length*4]; var bytes = new byte[values.Length*4];
#if !NET35 #if !NET35
using (var rnd = new RNGCryptoServiceProvider()) using (var rnd = RandomNumberGenerator.Create())
{ {
rnd.GetBytes(bytes); rnd.GetBytes(bytes);
} }
#else #else
var rnd = new RNGCryptoServiceProvider(); var rnd = RandomNumberGenerator.Create();
rnd.GetBytes(bytes); rnd.GetBytes(bytes);
#endif #endif
@ -168,7 +168,7 @@ namespace MathNet.Numerics.Random
/// <remarks>Supports being called in parallel from multiple threads.</remarks> /// <remarks>Supports being called in parallel from multiple threads.</remarks>
public static IEnumerable<double> DoubleSequence() public static IEnumerable<double> DoubleSequence()
{ {
var rnd = new RNGCryptoServiceProvider(); var rnd = RandomNumberGenerator.Create();
var buffer = new byte[1024*4]; var buffer = new byte[1024*4];
while (true) while (true)

6
src/Numerics/Random/RandomSeed.cs

@ -6,10 +6,10 @@ namespace MathNet.Numerics.Random
{ {
static readonly object Lock = new object(); static readonly object Lock = new object();
#if PORTABLE || NETSTANDARD1_6 #if PORTABLE
static readonly System.Random MasterRng = new System.Random(); static readonly System.Random MasterRng = new System.Random();
#else #else
static readonly System.Security.Cryptography.RandomNumberGenerator MasterRng = new System.Security.Cryptography.RNGCryptoServiceProvider(); static readonly System.Security.Cryptography.RandomNumberGenerator MasterRng = System.Security.Cryptography.RandomNumberGenerator.Create();
#endif #endif
/// <summary> /// <summary>
@ -41,7 +41,7 @@ namespace MathNet.Numerics.Random
{ {
lock (Lock) lock (Lock)
{ {
#if PORTABLE || NETSTANDARD1_6 #if PORTABLE
return MasterRng.NextFullRangeInt32() ^ Environment.TickCount ^ System.Guid.NewGuid().GetHashCode(); return MasterRng.NextFullRangeInt32() ^ Environment.TickCount ^ System.Guid.NewGuid().GetHashCode();
#else #else
var bytes = new byte[4]; var bytes = new byte[4];

Loading…
Cancel
Save