128 lines
4.1 KiB
C#
128 lines
4.1 KiB
C#
using System;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace System.Security.Cryptography
|
|
{
|
|
/// <summary>Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class cannot be inherited.</summary>
|
|
// Token: 0x0200051A RID: 1306
|
|
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator
|
|
{
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.RNGCryptoServiceProvider" /> class.</summary>
|
|
// Token: 0x06002FFA RID: 12282 RVA: 0x0009CFB4 File Offset: 0x0009B1B4
|
|
public RNGCryptoServiceProvider()
|
|
{
|
|
this._handle = RNGCryptoServiceProvider.RngInitialize(null);
|
|
this.Check();
|
|
}
|
|
|
|
// Token: 0x06002FFB RID: 12283 RVA: 0x0009CFD0 File Offset: 0x0009B1D0
|
|
static RNGCryptoServiceProvider()
|
|
{
|
|
if (RNGCryptoServiceProvider.RngOpen())
|
|
{
|
|
RNGCryptoServiceProvider._lock = new object();
|
|
}
|
|
}
|
|
|
|
// Token: 0x06002FFC RID: 12284 RVA: 0x0009CFE8 File Offset: 0x0009B1E8
|
|
private void Check()
|
|
{
|
|
if (this._handle == IntPtr.Zero)
|
|
{
|
|
throw new CryptographicException(Locale.GetText("Couldn't access random source."));
|
|
}
|
|
}
|
|
|
|
// Token: 0x06002FFD RID: 12285
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
private static extern bool RngOpen();
|
|
|
|
// Token: 0x06002FFE RID: 12286
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
private static extern IntPtr RngInitialize(byte[] seed);
|
|
|
|
// Token: 0x06002FFF RID: 12287
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
private static extern IntPtr RngGetBytes(IntPtr handle, byte[] data);
|
|
|
|
// Token: 0x06003000 RID: 12288
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
private static extern void RngClose(IntPtr handle);
|
|
|
|
/// <summary>Fills an array of bytes with a cryptographically strong sequence of random values.</summary>
|
|
/// <param name="data">The array to fill with a cryptographically strong sequence of random values. </param>
|
|
/// <exception cref="T:System.Security.Cryptography.CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="data" /> is null.</exception>
|
|
// Token: 0x06003001 RID: 12289 RVA: 0x0009D010 File Offset: 0x0009B210
|
|
public override void GetBytes(byte[] data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
throw new ArgumentNullException("data");
|
|
}
|
|
if (RNGCryptoServiceProvider._lock == null)
|
|
{
|
|
this._handle = RNGCryptoServiceProvider.RngGetBytes(this._handle, data);
|
|
}
|
|
else
|
|
{
|
|
object @lock = RNGCryptoServiceProvider._lock;
|
|
lock (@lock)
|
|
{
|
|
this._handle = RNGCryptoServiceProvider.RngGetBytes(this._handle, data);
|
|
}
|
|
}
|
|
this.Check();
|
|
}
|
|
|
|
/// <summary>Fills an array of bytes with a cryptographically strong sequence of random nonzero values.</summary>
|
|
/// <param name="data">The array to fill with a cryptographically strong sequence of random nonzero values. </param>
|
|
/// <exception cref="T:System.Security.Cryptography.CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="data" /> is null.</exception>
|
|
// Token: 0x06003002 RID: 12290 RVA: 0x0009D09C File Offset: 0x0009B29C
|
|
public override void GetNonZeroBytes(byte[] data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
throw new ArgumentNullException("data");
|
|
}
|
|
byte[] array = new byte[data.Length * 2];
|
|
int i = 0;
|
|
while (i < data.Length)
|
|
{
|
|
this._handle = RNGCryptoServiceProvider.RngGetBytes(this._handle, array);
|
|
this.Check();
|
|
for (int j = 0; j < array.Length; j++)
|
|
{
|
|
if (i == data.Length)
|
|
{
|
|
break;
|
|
}
|
|
if (array[j] != 0)
|
|
{
|
|
data[i++] = array[j];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Token: 0x06003003 RID: 12291 RVA: 0x0009D124 File Offset: 0x0009B324
|
|
~RNGCryptoServiceProvider()
|
|
{
|
|
if (this._handle != IntPtr.Zero)
|
|
{
|
|
RNGCryptoServiceProvider.RngClose(this._handle);
|
|
this._handle = IntPtr.Zero;
|
|
}
|
|
}
|
|
|
|
// Token: 0x04001390 RID: 5008
|
|
private static object _lock;
|
|
|
|
// Token: 0x04001391 RID: 5009
|
|
private IntPtr _handle;
|
|
}
|
|
}
|