Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / 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
- Wizard.cs
- ContainerControl.cs
- DesignBinding.cs
- NumericUpDownAccelerationCollection.cs
- HierarchicalDataSourceControl.cs
- XmlSchemaImport.cs
- ProfilePropertyNameValidator.cs
- ToolTip.cs
- TypedReference.cs
- UrlParameterWriter.cs
- AttributeCollection.cs
- HashMembershipCondition.cs
- SqlBuilder.cs
- FormsAuthenticationUserCollection.cs
- ISAPIWorkerRequest.cs
- ACE.cs
- TempEnvironment.cs
- IdentityVerifier.cs
- MarkupCompilePass2.cs
- EmissiveMaterial.cs
- EntryPointNotFoundException.cs
- PersonalizationStateQuery.cs
- DrawListViewColumnHeaderEventArgs.cs
- TextDecorationUnitValidation.cs
- TraceData.cs
- WSSecurityOneDotOneSendSecurityHeader.cs
- DataServiceEntityAttribute.cs
- DataPagerField.cs
- RectAnimationBase.cs
- DesignerOptionService.cs
- PropertyIDSet.cs
- WebPartZoneCollection.cs
- OutOfProcStateClientManager.cs
- Win32Exception.cs
- X509CertificateTrustedIssuerElementCollection.cs
- SemaphoreSecurity.cs
- GlyphElement.cs
- SynthesizerStateChangedEventArgs.cs
- StorageMappingFragment.cs
- TextRangeEditTables.cs
- Symbol.cs
- IIS7UserPrincipal.cs
- BasicHttpSecurityMode.cs
- PeerObject.cs
- FontDriver.cs
- updatecommandorderer.cs
- Timer.cs
- TextElementEditingBehaviorAttribute.cs
- DbDeleteCommandTree.cs
- SQLString.cs
- SqlFactory.cs
- CompilerError.cs
- CounterSampleCalculator.cs
- KeyGesture.cs
- CallbackTimeoutsElement.cs
- DataGridViewComboBoxCell.cs
- UrlPath.cs
- ImageDesigner.cs
- ValueQuery.cs
- TimeoutValidationAttribute.cs
- ResolveDuplex11AsyncResult.cs
- AsyncContentLoadedEventArgs.cs
- HtmlWindowCollection.cs
- IndexOutOfRangeException.cs
- XmlnsCompatibleWithAttribute.cs
- DesignerSerializerAttribute.cs
- NetMsmqSecurityElement.cs
- ExternalException.cs
- SmtpTransport.cs
- KeyEventArgs.cs
- XMLDiffLoader.cs
- SQLChars.cs
- SurrogateSelector.cs
- ToolStripContainerActionList.cs
- XmlElement.cs
- MsmqBindingFilter.cs
- HttpCapabilitiesSectionHandler.cs
- XmlDocument.cs
- RelatedImageListAttribute.cs
- ObjRef.cs
- WebServiceClientProxyGenerator.cs
- BezierSegment.cs
- Span.cs
- PageThemeBuildProvider.cs
- MimeParameters.cs
- UidManager.cs
- SQLMoneyStorage.cs
- IndexedGlyphRun.cs
- KeyedCollection.cs
- EncryptedKeyIdentifierClause.cs
- ButtonStandardAdapter.cs
- Material.cs
- Property.cs
- AlternationConverter.cs
- CompressStream.cs
- TabControlAutomationPeer.cs
- SimpleTypesSurrogate.cs
- SimpleTextLine.cs
- DropSource.cs
- SiteMapDataSourceView.cs