blob: dcd3baa1c2e9a8e172d3ec3f98072704efcddc61 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
using System;
using System.Security.Cryptography;
namespace Org.BouncyCastle.Crypto.Prng
{
/// <summary>
/// Uses RandomNumberGenerator.Create() to get randomness generator
/// </summary>
public sealed class CryptoApiRandomGenerator
: IRandomGenerator, IDisposable
{
private readonly RandomNumberGenerator m_randomNumberGenerator;
public CryptoApiRandomGenerator()
: this(RandomNumberGenerator.Create())
{
}
public CryptoApiRandomGenerator(RandomNumberGenerator randomNumberGenerator)
{
m_randomNumberGenerator = randomNumberGenerator ??
throw new ArgumentNullException(nameof(randomNumberGenerator));
}
#region IRandomGenerator Members
public void AddSeedMaterial(byte[] seed)
{
// We don't care about the seed
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public void AddSeedMaterial(ReadOnlySpan<byte> inSeed)
{
// We don't care about the seed
}
#endif
public void AddSeedMaterial(long seed)
{
// We don't care about the seed
}
public void NextBytes(byte[] bytes)
{
m_randomNumberGenerator.GetBytes(bytes);
}
public void NextBytes(byte[] bytes, int start, int len)
{
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_0_OR_GREATER
m_randomNumberGenerator.GetBytes(bytes, start, len);
#else
if (start < 0)
throw new ArgumentException("Start offset cannot be negative", "start");
if (bytes.Length < (start + len))
throw new ArgumentException("Byte array too small for requested offset and length");
if (bytes.Length == len && start == 0)
{
NextBytes(bytes);
}
else
{
byte[] tmpBuf = new byte[len];
NextBytes(tmpBuf);
Array.Copy(tmpBuf, 0, bytes, start, len);
}
#endif
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public void NextBytes(Span<byte> bytes)
{
m_randomNumberGenerator.GetBytes(bytes);
}
#endif
#endregion
#region IDisposable Members
public void Dispose()
{
m_randomNumberGenerator.Dispose();
}
#endregion
}
}
|