Code:
/ DotNET / DotNET / 8.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
- WebPartMenu.cs
- IpcClientManager.cs
- ProcessingInstructionAction.cs
- DoWorkEventArgs.cs
- DocumentViewerHelper.cs
- ReferenceSchema.cs
- TypeSemantics.cs
- SingleConverter.cs
- BufferedMessageData.cs
- ButtonField.cs
- Win32MouseDevice.cs
- TextPointerBase.cs
- FileSystemInfo.cs
- SpeakCompletedEventArgs.cs
- Crc32.cs
- GeometryHitTestParameters.cs
- InternalConfigHost.cs
- CommandHelper.cs
- NetStream.cs
- MouseButtonEventArgs.cs
- Queue.cs
- PageAsyncTaskManager.cs
- UserPersonalizationStateInfo.cs
- XpsImageSerializationService.cs
- DataGridViewColumnCollection.cs
- Package.cs
- DesignerAdRotatorAdapter.cs
- HttpListener.cs
- DefaultPrintController.cs
- FormatConvertedBitmap.cs
- TransformerConfigurationWizardBase.cs
- WebPartVerbCollection.cs
- FlowNode.cs
- CharacterBuffer.cs
- Facet.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- NullableFloatSumAggregationOperator.cs
- TextRunProperties.cs
- XmlDeclaration.cs
- WindowsTitleBar.cs
- ISO2022Encoding.cs
- Constants.cs
- UnsafeNativeMethods.cs
- TypedElement.cs
- Ray3DHitTestResult.cs
- ZipIOFileItemStream.cs
- MouseGestureConverter.cs
- SystemIPGlobalStatistics.cs
- TouchesOverProperty.cs
- PrimaryKeyTypeConverter.cs
- GACMembershipCondition.cs
- KeyGestureConverter.cs
- ModifyActivitiesPropertyDescriptor.cs
- HtmlTableRowCollection.cs
- HiddenField.cs
- ButtonStandardAdapter.cs
- cache.cs
- EntityDataSourceDesignerHelper.cs
- MdiWindowListStrip.cs
- StylusButtonCollection.cs
- CacheMemory.cs
- FileSystemInfo.cs
- Underline.cs
- Stroke.cs
- DummyDataSource.cs
- FormCollection.cs
- Formatter.cs
- precedingquery.cs
- Camera.cs
- Vector3DAnimationUsingKeyFrames.cs
- SHA1Managed.cs
- DesignerActionVerbItem.cs
- DatatypeImplementation.cs
- ConnectionPoint.cs
- TextElementCollection.cs
- CodeAttributeArgument.cs
- AsyncInvokeContext.cs
- SafeRegistryKey.cs
- QilPatternFactory.cs
- AttributeEmitter.cs
- TextDpi.cs
- Mappings.cs
- AppSecurityManager.cs
- ChtmlTextWriter.cs
- ToolStripMenuItemDesigner.cs
- PointConverter.cs
- TypeSystem.cs
- DetailsViewDeletedEventArgs.cs
- SafeCoTaskMem.cs
- FreezableOperations.cs
- HTTPNotFoundHandler.cs
- XmlTypeMapping.cs
- RecordConverter.cs
- AlphaSortedEnumConverter.cs
- CodeSnippetStatement.cs
- TextPattern.cs
- ExpressionBuilder.cs
- SmtpException.cs
- CookielessHelper.cs
- SecurityManager.cs