Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / 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 } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- UnconditionalPolicy.cs
- ToolStripTextBox.cs
- EditCommandColumn.cs
- Compiler.cs
- ToolStripItemClickedEventArgs.cs
- XmlNamedNodeMap.cs
- DebugHandleTracker.cs
- TaiwanLunisolarCalendar.cs
- UnsafeNativeMethods.cs
- _TimerThread.cs
- QueryCursorEventArgs.cs
- BatchWriter.cs
- SortAction.cs
- EventRoute.cs
- ConnectionManagementElementCollection.cs
- Misc.cs
- HtmlInputControl.cs
- WorkflowApplicationUnhandledExceptionEventArgs.cs
- MatrixIndependentAnimationStorage.cs
- UITypeEditors.cs
- ZipIORawDataFileBlock.cs
- ClientData.cs
- WorkflowInspectionServices.cs
- IssuanceLicense.cs
- DataGridClipboardHelper.cs
- OutputWindow.cs
- DocumentOrderQuery.cs
- BackStopAuthenticationModule.cs
- QueryAsyncResult.cs
- HttpModuleCollection.cs
- ConfigsHelper.cs
- BookmarkOptionsHelper.cs
- Comparer.cs
- DataGridViewCellValidatingEventArgs.cs
- StrokeRenderer.cs
- NamespaceQuery.cs
- MetadataArtifactLoaderCompositeFile.cs
- PolygonHotSpot.cs
- UInt64Converter.cs
- WinInetCache.cs
- BinaryObjectInfo.cs
- InlineObject.cs
- HighlightComponent.cs
- EntityViewGenerator.cs
- ConfigurationValue.cs
- RectangleHotSpot.cs
- XPathNode.cs
- __Filters.cs
- HttpHandlerAction.cs
- MailSettingsSection.cs
- SqlTriggerAttribute.cs
- PersistenceMetadataNamespace.cs
- X500Name.cs
- CommandDevice.cs
- TraceSection.cs
- ConsoleTraceListener.cs
- HostedNamedPipeTransportManager.cs
- TakeQueryOptionExpression.cs
- SizeF.cs
- RowBinding.cs
- SimpleBitVector32.cs
- MobileUserControlDesigner.cs
- DrawingGroup.cs
- CodeParameterDeclarationExpressionCollection.cs
- MethodCallConverter.cs
- Encoding.cs
- Message.cs
- DockingAttribute.cs
- UriTemplate.cs
- CqlParser.cs
- XmlMapping.cs
- LoadedEvent.cs
- CqlGenerator.cs
- EditorPartCollection.cs
- FormsAuthenticationModule.cs
- TextElementEnumerator.cs
- ApplicationDirectory.cs
- BufferBuilder.cs
- NTAccount.cs
- QilParameter.cs
- UIElementIsland.cs
- SqlDelegatedTransaction.cs
- TabPanel.cs
- ComponentRenameEvent.cs
- DSASignatureDeformatter.cs
- EntityDataSourceWizardForm.cs
- DesignerActionMethodItem.cs
- ThumbAutomationPeer.cs
- KeySpline.cs
- HMACSHA256.cs
- bindurihelper.cs
- DataComponentNameHandler.cs
- safelinkcollection.cs
- _SpnDictionary.cs
- DataGridViewHeaderCell.cs
- EmptyImpersonationContext.cs
- NavigationFailedEventArgs.cs
- DataBindingsDialog.cs
- DetailsViewUpdatedEventArgs.cs
- KnownTypeAttribute.cs