Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Security / Cryptography / RNGCryptoServiceProvider.cs / 1 / RNGCryptoServiceProvider.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // RNGCryptoServiceProvider.cs // namespace System.Security.Cryptography { using Microsoft.Win32; using System.Runtime.InteropServices; [System.Runtime.InteropServices.ComVisible(true)] public sealed class RNGCryptoServiceProvider : RandomNumberGenerator { #if !FEATURE_PAL SafeProvHandle m_safeProvHandle; #endif // // public constructors // #if !FEATURE_PAL public RNGCryptoServiceProvider() : this((CspParameters) null) {} #else public RNGCryptoServiceProvider() { } #endif #if !FEATURE_PAL public RNGCryptoServiceProvider(string str) : this((CspParameters) null) {} public RNGCryptoServiceProvider(byte[] rgb) : this((CspParameters) null) {} public RNGCryptoServiceProvider(CspParameters cspParams) { if (cspParams != null) m_safeProvHandle = Utils.AcquireProvHandle(cspParams); else m_safeProvHandle = Utils.StaticProvHandle; } #endif // // public methods // public override void GetBytes(byte[] data) { if (data == null) throw new ArgumentNullException("data"); #if !FEATURE_PAL Utils._GetBytes(m_safeProvHandle, data); #else if (!Win32Native.Random(true, data, data.Length)) throw new CryptographicException(Marshal.GetLastWin32Error()); #endif } public override void GetNonZeroBytes(byte[] data) { if (data == null) throw new ArgumentNullException("data"); #if !FEATURE_PAL Utils._GetNonZeroBytes(m_safeProvHandle, data); #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 } } } // 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.InteropServices; [System.Runtime.InteropServices.ComVisible(true)] public sealed class RNGCryptoServiceProvider : RandomNumberGenerator { #if !FEATURE_PAL SafeProvHandle m_safeProvHandle; #endif // // public constructors // #if !FEATURE_PAL public RNGCryptoServiceProvider() : this((CspParameters) null) {} #else public RNGCryptoServiceProvider() { } #endif #if !FEATURE_PAL public RNGCryptoServiceProvider(string str) : this((CspParameters) null) {} public RNGCryptoServiceProvider(byte[] rgb) : this((CspParameters) null) {} public RNGCryptoServiceProvider(CspParameters cspParams) { if (cspParams != null) m_safeProvHandle = Utils.AcquireProvHandle(cspParams); else m_safeProvHandle = Utils.StaticProvHandle; } #endif // // public methods // public override void GetBytes(byte[] data) { if (data == null) throw new ArgumentNullException("data"); #if !FEATURE_PAL Utils._GetBytes(m_safeProvHandle, data); #else if (!Win32Native.Random(true, data, data.Length)) throw new CryptographicException(Marshal.GetLastWin32Error()); #endif } public override void GetNonZeroBytes(byte[] data) { if (data == null) throw new ArgumentNullException("data"); #if !FEATURE_PAL Utils._GetNonZeroBytes(m_safeProvHandle, data); #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 } } } // 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
- RoleManagerModule.cs
- Item.cs
- ProviderSettingsCollection.cs
- ReadOnlyCollectionBuilder.cs
- TextMetrics.cs
- SmtpReplyReader.cs
- DateTimeParse.cs
- NodeFunctions.cs
- KoreanCalendar.cs
- WindowsSecurityToken.cs
- DataSvcMapFileSerializer.cs
- SelectedCellsCollection.cs
- AstNode.cs
- WebPartConnectionsCloseVerb.cs
- HtmlTable.cs
- WebPermission.cs
- PropertyInfoSet.cs
- SyntaxCheck.cs
- SafeFileMappingHandle.cs
- TranslateTransform.cs
- WorkflowRuntimeServiceElementCollection.cs
- ClientApiGenerator.cs
- TrustManagerMoreInformation.cs
- NodeCounter.cs
- LineUtil.cs
- BitmapEffectDrawing.cs
- DynamicMethod.cs
- RegexStringValidator.cs
- SqlConnectionString.cs
- SQLUtility.cs
- FixUpCollection.cs
- BitmapEffectState.cs
- UseManagedPresentationBindingElement.cs
- ObjectCloneHelper.cs
- FormViewInsertEventArgs.cs
- TemplateField.cs
- SessionParameter.cs
- DataTemplateSelector.cs
- SoapElementAttribute.cs
- SamlSubjectStatement.cs
- HttpStaticObjectsCollectionWrapper.cs
- StackBuilderSink.cs
- ColorKeyFrameCollection.cs
- HttpConfigurationSystem.cs
- XmlIgnoreAttribute.cs
- RadioButtonPopupAdapter.cs
- DataGridViewControlCollection.cs
- ProfileGroupSettings.cs
- SpStreamWrapper.cs
- Assert.cs
- JoinTreeSlot.cs
- XmlProcessingInstruction.cs
- CompilerResults.cs
- EntityDataSourceDataSelection.cs
- LOSFormatter.cs
- DoubleAnimationBase.cs
- ReadOnlyDataSource.cs
- PerformanceCounterPermissionEntry.cs
- Timer.cs
- DataGridItem.cs
- ProfileInfo.cs
- HtmlFormWrapper.cs
- GeometryHitTestResult.cs
- SendingRequestEventArgs.cs
- BoundsDrawingContextWalker.cs
- QilDataSource.cs
- BitmapEffect.cs
- ParameterBuilder.cs
- SecurityTokenTypes.cs
- MobileUITypeEditor.cs
- ParentUndoUnit.cs
- Transform3D.cs
- ItemContainerGenerator.cs
- IpcManager.cs
- PropertyFilter.cs
- TemplatedAdorner.cs
- QilLoop.cs
- LinqDataSourceUpdateEventArgs.cs
- PageThemeCodeDomTreeGenerator.cs
- AutomationPattern.cs
- StateDesigner.Helpers.cs
- SecurityCookieModeValidator.cs
- ClientTarget.cs
- ServiceEndpointCollection.cs
- GridViewCancelEditEventArgs.cs
- BitmapEncoder.cs
- XmlSignatureProperties.cs
- UIPropertyMetadata.cs
- SqlHelper.cs
- PrintPreviewDialog.cs
- MimeImporter.cs
- ClientRoleProvider.cs
- TableColumn.cs
- SimpleHandlerBuildProvider.cs
- NativeMethodsCLR.cs
- Stack.cs
- ViewStateModeByIdAttribute.cs
- IssuedTokenParametersEndpointAddressElement.cs
- CancellableEnumerable.cs
- TypefaceMap.cs