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
- WindowsEditBoxRange.cs
- ReadOnlyState.cs
- XmlNodeChangedEventArgs.cs
- DataBindingsDialog.cs
- XPathAxisIterator.cs
- WindowsAltTab.cs
- FastPropertyAccessor.cs
- TypeHelpers.cs
- X509ScopedServiceCertificateElementCollection.cs
- PageAsyncTask.cs
- EDesignUtil.cs
- EntitySqlQueryCacheEntry.cs
- _IPv4Address.cs
- ResourcesBuildProvider.cs
- RuleValidation.cs
- InsufficientExecutionStackException.cs
- DataGridTablesFactory.cs
- ProcessModelInfo.cs
- DetailsViewDeleteEventArgs.cs
- InheritanceAttribute.cs
- PkcsUtils.cs
- SubpageParagraph.cs
- Application.cs
- Utils.cs
- ValidationErrorCollection.cs
- RtfToXamlLexer.cs
- RoutedCommand.cs
- ResourcePermissionBaseEntry.cs
- ProfileManager.cs
- Rule.cs
- Win32.cs
- BrowserCapabilitiesFactory.cs
- XmlSchemaExporter.cs
- AssemblySettingAttributes.cs
- ToolStripContentPanel.cs
- DetailsViewInsertedEventArgs.cs
- MemberInfoSerializationHolder.cs
- TdsValueSetter.cs
- FixedSOMFixedBlock.cs
- IsolatedStorageFileStream.cs
- ServiceDocument.cs
- CatalogPartChrome.cs
- ActivationServices.cs
- WebPartEditorApplyVerb.cs
- DataSourceHelper.cs
- UserPreferenceChangingEventArgs.cs
- Model3DCollection.cs
- EdmError.cs
- COM2IProvidePropertyBuilderHandler.cs
- DBDataPermissionAttribute.cs
- Module.cs
- Compiler.cs
- RangeContentEnumerator.cs
- DataGridViewCheckBoxCell.cs
- SynchronizedInputAdaptor.cs
- DataGridItemEventArgs.cs
- ParagraphResult.cs
- TableRow.cs
- SelectedGridItemChangedEvent.cs
- PlatformNotSupportedException.cs
- ResolveCriteria11.cs
- activationcontext.cs
- SystemColors.cs
- MimeMapping.cs
- DebugController.cs
- LogicalTreeHelper.cs
- DeferredTextReference.cs
- SessionEndingCancelEventArgs.cs
- UIEndRequest.cs
- ReceiveMessageRecord.cs
- TextSchema.cs
- WindowsSysHeader.cs
- FixedSOMLineRanges.cs
- DataRowCollection.cs
- CodeGenerator.cs
- RelationshipConverter.cs
- ImageIndexConverter.cs
- BamlBinaryWriter.cs
- XpsFixedPageReaderWriter.cs
- DataSourceConverter.cs
- ISO2022Encoding.cs
- PolyBezierSegmentFigureLogic.cs
- InputMethod.cs
- MimeTypePropertyAttribute.cs
- ItemChangedEventArgs.cs
- SqlTransaction.cs
- PointConverter.cs
- ConfigurationSettings.cs
- Errors.cs
- ParamArrayAttribute.cs
- ObjectDataProvider.cs
- Activity.cs
- CustomErrorCollection.cs
- ListViewItemMouseHoverEvent.cs
- QuerySetOp.cs
- HttpModuleCollection.cs
- streamingZipPartStream.cs
- TreeIterators.cs
- Pen.cs
- CodeMemberMethod.cs