-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRandomNumberGeneratorExtensions.cs
More file actions
284 lines (257 loc) · 12 KB
/
Copy pathRandomNumberGeneratorExtensions.cs
File metadata and controls
284 lines (257 loc) · 12 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System.Collections;
using System.Numerics;
using System.Security.Cryptography;
namespace BytecodeApi.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="RandomNumberGenerator" /> objects.
/// </summary>
public static class RandomNumberGeneratorExtensions
{
private static readonly RandomNumberGenerator _Shared = RandomNumberGenerator.Create();
extension(RandomNumberGenerator)
{
/// <summary>
/// Provides a thread-safe <see cref="RandomNumberGenerator" /> instance that may be used concurrently from any thread.
/// </summary>
public static RandomNumberGenerator Shared => _Shared;
}
extension(RandomNumberGenerator randomNumberGenerator)
{
/// <summary>
/// Creates a <see cref="byte" />[] with a specified length and fills all elements with a cryptographically strong random sequence of bytes.
/// </summary>
/// <param name="count">The size of the returned <see cref="byte" />[].</param>
/// <returns>
/// A new <see cref="byte" />[] with a specified length, filled with a cryptographically strong random sequence of bytes.
/// </returns>
public byte[] GetBytes(int count)
{
Check.ArgumentNull(randomNumberGenerator);
Check.ArgumentOutOfRangeEx.GreaterEqual0(count);
byte[] data = new byte[count];
randomNumberGenerator.GetBytes(data);
return data;
}
/// <summary>
/// Creates a <see cref="byte" />[] with a specified length and fills all elements with a cryptographically strong random sequence of nonzero bytes.
/// </summary>
/// <param name="count">The size of the returned <see cref="byte" />[].</param>
/// <returns>
/// A new <see cref="byte" />[] with a specified length, filled with a cryptographically strong random sequence of nonzero bytes.
/// </returns>
public byte[] GetNonZeroBytes(int count)
{
Check.ArgumentNull(randomNumberGenerator);
Check.ArgumentOutOfRangeEx.GreaterEqual0(count);
byte[] data = new byte[count];
randomNumberGenerator.GetNonZeroBytes(data);
return data;
}
/// <summary>
/// Generates a cryptographically strong random <see cref="bool" /> value that is either <see langword="false" /> or <see langword="true" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="bool" /> value that is either <see langword="false" /> or <see langword="true" />.
/// </returns>
public bool GetBoolean()
{
Check.ArgumentNull(randomNumberGenerator);
return (randomNumberGenerator.GetByte() & 1) == 1;
}
/// <summary>
/// Generates a cryptographically strong random <see cref="byte" /> value that is greater than or equal to 0, and less than or equal to <see cref="byte.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="byte" /> value that is greater than or equal to 0, and less than or equal to <see cref="byte.MaxValue" />.
/// </returns>
public byte GetByte()
{
Check.ArgumentNull(randomNumberGenerator);
return randomNumberGenerator.GetBytes(1)[0];
}
/// <summary>
/// Generates a cryptographically strong random nonzero <see cref="byte" /> value that is less than or equal to <see cref="byte.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random nonzero <see cref="byte" /> value that is less than or equal to <see cref="byte.MaxValue" />.
/// </returns>
public byte GetNonZeroByte()
{
Check.ArgumentNull(randomNumberGenerator);
return randomNumberGenerator.GetNonZeroBytes(1)[0];
}
/// <summary>
/// Generates a cryptographically strong random <see cref="sbyte" /> value that is greater than or equal to <see cref="sbyte.MinValue" />, and less than or equal to <see cref="sbyte.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="sbyte" /> value that is greater than or equal to <see cref="sbyte.MinValue" />, and less than or equal to <see cref="sbyte.MaxValue" />.
/// </returns>
public sbyte GetSByte()
{
Check.ArgumentNull(randomNumberGenerator);
return (sbyte)(randomNumberGenerator.GetByte() - 128);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="char" /> value that is greater than or equal to '\0', and less than or equal to <see cref="char.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="char" /> value that is greater than or equal to '\0', and less than or equal to <see cref="char.MaxValue" />.
/// </returns>
public char GetChar()
{
Check.ArgumentNull(randomNumberGenerator);
return BitConverter.ToChar(randomNumberGenerator.GetBytes(2), 0);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="int" /> value that is greater than or equal to <see cref="int.MinValue" />, and less than or equal to <see cref="int.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="int" /> value that is greater than or equal to <see cref="int.MinValue" />, and less than or equal to <see cref="int.MaxValue" />.
/// </returns>
public int GetInt32()
{
Check.ArgumentNull(randomNumberGenerator);
return BitConverter.ToInt32(randomNumberGenerator.GetBytes(4), 0);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="int" /> value that is less than the specified maximum.
/// </summary>
/// <param name="maxValue">The exclusive upper bound of the random number to be generated. <paramref name="maxValue" /> must be greater than or equal to 0.</param>
/// <returns>
/// A cryptographically strong random <see cref="int" /> value that is less than the specified maximum.
/// </returns>
public int GetInt32(int maxValue)
{
Check.ArgumentNull(randomNumberGenerator);
Check.ArgumentOutOfRangeEx.GreaterEqual0(maxValue);
byte[] bytes = randomNumberGenerator.GetBytes(4);
BitOperations.SetBit(bytes, 31, false);
return (int)((long)BitConverter.ToInt32(bytes, 0) * maxValue / int.MaxValue);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="int" /> value that is less than the specified maximum.
/// </summary>
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number to be generated. <paramref name="maxValue" /> must be greater than or equal to 0.</param>
/// <returns>
/// A cryptographically strong random <see cref="int" /> value that is less than the specified maximum.
/// </returns>
public int GetInt32(int minValue, int maxValue)
{
Check.ArgumentNull(randomNumberGenerator);
Check.ArgumentOutOfRangeEx.GreaterEqual0(maxValue);
Check.ArgumentOutOfRangeEx.GreaterEqualValue(maxValue, minValue);
return minValue + randomNumberGenerator.GetInt32(maxValue - minValue);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="uint" /> value that is greater than or equal to 0, and less than or equal to <see cref="uint.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="uint" /> value that is greater than or equal to 0, and less than or equal to <see cref="uint.MaxValue" />.
/// </returns>
public uint GetUInt32()
{
Check.ArgumentNull(randomNumberGenerator);
return BitConverter.ToUInt32(randomNumberGenerator.GetBytes(4), 0);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="long" /> value that is greater than or equal to <see cref="long.MinValue" />, and less than or equal to <see cref="long.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="long" /> value that is greater than or equal to <see cref="long.MinValue" />, and less than or equal to <see cref="long.MaxValue" />.
/// </returns>
public long GetInt64()
{
Check.ArgumentNull(randomNumberGenerator);
return BitConverter.ToInt64(randomNumberGenerator.GetBytes(8), 0);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="ulong" /> value that is greater than or equal to 0UL, and less than or equal to <see cref="ulong.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="ulong" /> value that is greater than or equal to 0UL, and less than or equal to <see cref="ulong.MaxValue" />.
/// </returns>
public ulong GetUInt64()
{
Check.ArgumentNull(randomNumberGenerator);
return BitConverter.ToUInt64(randomNumberGenerator.GetBytes(8), 0);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="short" /> value that is greater than or equal to <see cref="short.MinValue" />, and less than or equal to <see cref="short.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="short" /> value that is greater than or equal to <see cref="short.MinValue" />, and less than or equal to <see cref="short.MaxValue" />.
/// </returns>
public short GetInt16()
{
Check.ArgumentNull(randomNumberGenerator);
return BitConverter.ToInt16(randomNumberGenerator.GetBytes(2), 0);
}
/// <summary>
/// Generates a cryptographically strong random <see cref="ushort" /> value that is greater than or equal to 0, and less than or equal to <see cref="ushort.MaxValue" />.
/// </summary>
/// <returns>
/// A cryptographically strong random <see cref="ushort" /> value that is greater than or equal to 0, and less than or equal to <see cref="ushort.MaxValue" />.
/// </returns>
public ushort GetUInt16()
{
Check.ArgumentNull(randomNumberGenerator);
return BitConverter.ToUInt16(randomNumberGenerator.GetBytes(2), 0);
}
/// <summary>
/// Creates a <see cref="BitArray" /> with a specified length and fills all elements with a cryptographically strong random sequence of <see cref="bool" /> values.
/// </summary>
/// <param name="count">The size of the returned <see cref="BitArray" />.</param>
/// <returns>
/// A new <see cref="BitArray" /> with a specified length, filled with a cryptographically strong random sequence of <see cref="bool" /> values.
/// </returns>
public BitArray GetBits(int count)
{
Check.ArgumentNull(randomNumberGenerator);
Check.ArgumentOutOfRangeEx.GreaterEqual0(count);
BitArray bits = new(count);
byte[] buffer = new byte[16];
int bufferPosition = int.MaxValue;
for (int i = 0; i < count; i++)
{
if (bufferPosition >= buffer.Length << 3)
{
randomNumberGenerator.GetBytes(buffer);
bufferPosition = 0;
}
bits[i] = (buffer[bufferPosition >> 3] & 1 << (bufferPosition & 7)) > 0;
bufferPosition++;
}
return bits;
}
/// <summary>
/// Returns a random <see cref="object" /> of the specified type from <paramref name="list" />, selected based on a cryptographically strong random index.
/// </summary>
/// <typeparam name="T">The element type of <paramref name="list" />.</typeparam>
/// <param name="list">A <see cref="IList{T}" /> of the specified type.</param>
/// <returns>
/// A random <see cref="object" /> of the specified type from <paramref name="list" />.
/// </returns>
public T GetObject<T>(IList<T> list)
{
Check.ArgumentNull(randomNumberGenerator);
Check.ArgumentNull(list);
Check.ArgumentEx.ArrayElementsRequired(list);
return list[randomNumberGenerator.GetInt32(list.Count)];
}
/// <summary>
/// Returns a random value of the specified <see langword="enum" /> type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="Enum" /> to be returned.</typeparam>
/// <returns>
/// A random value of the specified <see langword="enum" /> type.
/// </returns>
public T GetEnumValue<T>() where T : struct, Enum
{
Check.ArgumentNull(randomNumberGenerator);
T[] values = Enum.GetValues<T>();
if (values.None()) throw Throw.Argument(nameof(T), "Enum does not have values.");
return randomNumberGenerator.GetObject(values);
}
}
}