Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Cryptography / RNGCryptoServiceProvider.cs / 1305376 / RNGCryptoServiceProvider.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
//
// RNGCryptoServiceProvider.cs
//
namespace System.Security.Cryptography {
using Microsoft.Win32;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
#if !FEATURE_CORECLR
[System.Runtime.InteropServices.ComVisible(true)]
#endif // !FEATURE_CORECLR
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator {
#if !FEATURE_CORECLR
[System.Security.SecurityCritical /*auto-generated*/]
SafeProvHandle m_safeProvHandle;
bool m_ownsHandle;
#else // !FEATURE_CORECLR
SafeCspHandle m_cspHandle;
#endif // !FEATURE_CORECLR
//
// public constructors
//
#if !FEATURE_CORECLR
#if !FEATURE_PAL
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider() : this((CspParameters) null) {}
#else // !FEATURE_PAL
public RNGCryptoServiceProvider() { }
#endif // !FEATURE_PAL
#if !FEATURE_PAL
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider(string str) : this((CspParameters) null) {}
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider(byte[] rgb) : this((CspParameters) null) {}
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider(CspParameters cspParams) {
if (cspParams != null) {
m_safeProvHandle = Utils.AcquireProvHandle(cspParams);
m_ownsHandle = true;
}
else {
m_safeProvHandle = Utils.StaticProvHandle;
m_ownsHandle = false;
}
}
[System.Security.SecuritySafeCritical] // auto-generated
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing && m_ownsHandle) {
m_safeProvHandle.Dispose();
}
}
#endif // !FEATURE_PAL
#else // !FEATURE_CORECLR
public RNGCryptoServiceProvider() {
m_cspHandle = CapiNative.AcquireCsp(null,
CapiNative.ProviderNames.MicrosoftEnhanced,
CapiNative.ProviderType.RsaFull,
CapiNative.CryptAcquireContextFlags.VerifyContext);
}
#endif // !FEATURE_CORECLR
//
// public methods
//
#if !FEATURE_CORECLR
[System.Security.SecuritySafeCritical] // auto-generated
public override void GetBytes(byte[] data) {
if (data == null) throw new ArgumentNullException("data");
Contract.EndContractBlock();
#if !FEATURE_PAL
GetBytes(m_safeProvHandle, data, data.Length);
#else
if (!Win32Native.Random(true, data, data.Length))
throw new CryptographicException(Marshal.GetLastWin32Error());
#endif // !FEATURE_PAL
}
[System.Security.SecuritySafeCritical] // auto-generated
public override void GetNonZeroBytes(byte[] data) {
if (data == null)
throw new ArgumentNullException("data");
Contract.EndContractBlock();
#if !FEATURE_PAL
GetNonZeroBytes(m_safeProvHandle, data, data.Length);
#else
GetBytes(data);
int indexOfFirst0Byte = data.Length;
for (int i = 0; i < data.Length; i++) {
if (data[i] == 0) {
indexOfFirst0Byte = i;
break;
}
}
for (int i = indexOfFirst0Byte; i < data.Length; i++) {
if (data[i] != 0) {
data[indexOfFirst0Byte++] = data[i];
}
}
while (indexOfFirst0Byte < data.Length) {
// this should be more than enough to fill the rest in one iteration
byte[] tmp = new byte[2 * (data.Length - indexOfFirst0Byte)];
GetBytes(tmp);
for (int i = 0; i < tmp.Length; i++) {
if (tmp[i] != 0) {
data[indexOfFirst0Byte++] = tmp[i];
if (indexOfFirst0Byte >= data.Length) break;
}
}
}
#endif // !FEATURE_PAL
}
#else // !FEATURE_CORECLR
protected override void Dispose(bool disposing) {
try {
if (disposing) {
if (m_cspHandle != null) {
m_cspHandle.Dispose();
}
}
}
finally {
base.Dispose(disposing);
}
}
public override void GetBytes(byte[] data) {
if (data == null) {
throw new ArgumentNullException("data");
}
Contract.EndContractBlock();
if (data.Length > 0) {
CapiNative.GenerateRandomBytes(m_cspHandle, data);
}
}
#endif // !FEATURE_CORECLR
#if !FEATURE_PAL
[System.Security.SecurityCritical] // auto-generated
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[ResourceExposure(ResourceScope.None)]
private static extern void GetBytes(SafeProvHandle hProv, byte[] randomBytes, int count);
[System.Security.SecurityCritical] // auto-generated
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[ResourceExposure(ResourceScope.None)]
private static extern void GetNonZeroBytes(SafeProvHandle hProv, byte[] randomBytes, int count);
#endif
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
//
// RNGCryptoServiceProvider.cs
//
namespace System.Security.Cryptography {
using Microsoft.Win32;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
#if !FEATURE_CORECLR
[System.Runtime.InteropServices.ComVisible(true)]
#endif // !FEATURE_CORECLR
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator {
#if !FEATURE_CORECLR
[System.Security.SecurityCritical /*auto-generated*/]
SafeProvHandle m_safeProvHandle;
bool m_ownsHandle;
#else // !FEATURE_CORECLR
SafeCspHandle m_cspHandle;
#endif // !FEATURE_CORECLR
//
// public constructors
//
#if !FEATURE_CORECLR
#if !FEATURE_PAL
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider() : this((CspParameters) null) {}
#else // !FEATURE_PAL
public RNGCryptoServiceProvider() { }
#endif // !FEATURE_PAL
#if !FEATURE_PAL
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider(string str) : this((CspParameters) null) {}
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider(byte[] rgb) : this((CspParameters) null) {}
[System.Security.SecuritySafeCritical] // auto-generated
public RNGCryptoServiceProvider(CspParameters cspParams) {
if (cspParams != null) {
m_safeProvHandle = Utils.AcquireProvHandle(cspParams);
m_ownsHandle = true;
}
else {
m_safeProvHandle = Utils.StaticProvHandle;
m_ownsHandle = false;
}
}
[System.Security.SecuritySafeCritical] // auto-generated
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing && m_ownsHandle) {
m_safeProvHandle.Dispose();
}
}
#endif // !FEATURE_PAL
#else // !FEATURE_CORECLR
public RNGCryptoServiceProvider() {
m_cspHandle = CapiNative.AcquireCsp(null,
CapiNative.ProviderNames.MicrosoftEnhanced,
CapiNative.ProviderType.RsaFull,
CapiNative.CryptAcquireContextFlags.VerifyContext);
}
#endif // !FEATURE_CORECLR
//
// public methods
//
#if !FEATURE_CORECLR
[System.Security.SecuritySafeCritical] // auto-generated
public override void GetBytes(byte[] data) {
if (data == null) throw new ArgumentNullException("data");
Contract.EndContractBlock();
#if !FEATURE_PAL
GetBytes(m_safeProvHandle, data, data.Length);
#else
if (!Win32Native.Random(true, data, data.Length))
throw new CryptographicException(Marshal.GetLastWin32Error());
#endif // !FEATURE_PAL
}
[System.Security.SecuritySafeCritical] // auto-generated
public override void GetNonZeroBytes(byte[] data) {
if (data == null)
throw new ArgumentNullException("data");
Contract.EndContractBlock();
#if !FEATURE_PAL
GetNonZeroBytes(m_safeProvHandle, data, data.Length);
#else
GetBytes(data);
int indexOfFirst0Byte = data.Length;
for (int i = 0; i < data.Length; i++) {
if (data[i] == 0) {
indexOfFirst0Byte = i;
break;
}
}
for (int i = indexOfFirst0Byte; i < data.Length; i++) {
if (data[i] != 0) {
data[indexOfFirst0Byte++] = data[i];
}
}
while (indexOfFirst0Byte < data.Length) {
// this should be more than enough to fill the rest in one iteration
byte[] tmp = new byte[2 * (data.Length - indexOfFirst0Byte)];
GetBytes(tmp);
for (int i = 0; i < tmp.Length; i++) {
if (tmp[i] != 0) {
data[indexOfFirst0Byte++] = tmp[i];
if (indexOfFirst0Byte >= data.Length) break;
}
}
}
#endif // !FEATURE_PAL
}
#else // !FEATURE_CORECLR
protected override void Dispose(bool disposing) {
try {
if (disposing) {
if (m_cspHandle != null) {
m_cspHandle.Dispose();
}
}
}
finally {
base.Dispose(disposing);
}
}
public override void GetBytes(byte[] data) {
if (data == null) {
throw new ArgumentNullException("data");
}
Contract.EndContractBlock();
if (data.Length > 0) {
CapiNative.GenerateRandomBytes(m_cspHandle, data);
}
}
#endif // !FEATURE_CORECLR
#if !FEATURE_PAL
[System.Security.SecurityCritical] // auto-generated
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[ResourceExposure(ResourceScope.None)]
private static extern void GetBytes(SafeProvHandle hProv, byte[] randomBytes, int count);
[System.Security.SecurityCritical] // auto-generated
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[ResourceExposure(ResourceScope.None)]
private static extern void GetNonZeroBytes(SafeProvHandle hProv, byte[] randomBytes, int count);
#endif
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Rectangle.cs
- FixedPageAutomationPeer.cs
- VirtualizingPanel.cs
- TraceContextEventArgs.cs
- cache.cs
- PlatformNotSupportedException.cs
- FixedPage.cs
- UserCancellationException.cs
- BitArray.cs
- TrustManagerMoreInformation.cs
- DataTableClearEvent.cs
- HttpCachePolicyWrapper.cs
- SqlComparer.cs
- FormattedTextSymbols.cs
- AcceleratedTokenProvider.cs
- CustomErrorsSectionWrapper.cs
- CommonDialog.cs
- OneToOneMappingSerializer.cs
- SharedTcpTransportManager.cs
- VersionedStream.cs
- PagesChangedEventArgs.cs
- DefaultObjectMappingItemCollection.cs
- MonthChangedEventArgs.cs
- VisualStyleInformation.cs
- ToolbarAUtomationPeer.cs
- WmpBitmapEncoder.cs
- Icon.cs
- GridViewDeletedEventArgs.cs
- MonthCalendarDesigner.cs
- DataKey.cs
- InternalMappingException.cs
- CodeLinePragma.cs
- MexHttpBindingElement.cs
- Int32KeyFrameCollection.cs
- HelpKeywordAttribute.cs
- MappingSource.cs
- SafeEventLogWriteHandle.cs
- Transactions.cs
- ResourceSetExpression.cs
- PreviewPageInfo.cs
- HttpPostProtocolReflector.cs
- ProbeDuplex11AsyncResult.cs
- AsyncCompletedEventArgs.cs
- RefreshPropertiesAttribute.cs
- _LoggingObject.cs
- SqlVersion.cs
- SiblingIterators.cs
- FilterException.cs
- Underline.cs
- DoubleAnimationClockResource.cs
- GroupStyle.cs
- CodeTypeConstructor.cs
- RoleGroup.cs
- ValueQuery.cs
- SoapBinding.cs
- DateTimePicker.cs
- ObjectSecurity.cs
- RegisteredExpandoAttribute.cs
- VariantWrapper.cs
- AspProxy.cs
- MetaTableHelper.cs
- RangeBase.cs
- AttributeTableBuilder.cs
- UIElement3DAutomationPeer.cs
- TableDetailsRow.cs
- FormView.cs
- UIElementParagraph.cs
- SortDescription.cs
- VirtualPathUtility.cs
- DynamicControlParameter.cs
- SoapIgnoreAttribute.cs
- Int32CAMarshaler.cs
- PrintPageEvent.cs
- SecurityRuntime.cs
- ResourceManagerWrapper.cs
- WhiteSpaceTrimStringConverter.cs
- CriticalFinalizerObject.cs
- SmiEventSink_Default.cs
- StrongNamePublicKeyBlob.cs
- infer.cs
- CodeDOMUtility.cs
- Socket.cs
- SqlCacheDependencySection.cs
- ToolStripOverflow.cs
- OutputCacheSection.cs
- PersonalizablePropertyEntry.cs
- AutomationPropertyInfo.cs
- DataBindingList.cs
- Partitioner.cs
- Image.cs
- InstanceLockLostException.cs
- XPathException.cs
- HTMLTagNameToTypeMapper.cs
- ViewKeyConstraint.cs
- InvokeHandlers.cs
- Setter.cs
- PersonalizationStateInfoCollection.cs
- PaintEvent.cs
- DragStartedEventArgs.cs
- DbParameterHelper.cs