Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / Xml / System / Xml / Core / SecureStringHasher.cs / 1 / SecureStringHasher.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //----------------------------------------------------------------------------- using System; using System.Collections.Generic; namespace System.Xml { // SecureStringHasher is a hash code provider for strings. The hash codes calculation starts with a seed (hasCodeRandomizer) which is usually // different for each instance of SecureStringHasher. Since the hash code depend on the seed, the chance of hashtable DoS attack in case when // someone passes in lots of strings that hash to the same hash code is greatly reduced. // The SecureStringHasher implements IEqualityComparer for strings and therefore can be used in generic IDictionary. internal class SecureStringHasher : IEqualityComparer{ int hashCodeRandomizer; public SecureStringHasher() { this.hashCodeRandomizer = Environment.TickCount; } public SecureStringHasher( int hashCodeRandomizer ) { this.hashCodeRandomizer = hashCodeRandomizer; } public int Compare( String x, String y ) { return String.Compare(x, y, StringComparison.Ordinal); } public bool Equals( String x, String y ) { return String.Equals( x, y, StringComparison.Ordinal ); } public int GetHashCode( String key ) { int hashCode = hashCodeRandomizer; // use key.Length to eliminate the rangecheck for ( int i = 0; i < key.Length; i++ ) { hashCode += ( hashCode << 7 ) ^ key[i]; } // mix it a bit more hashCode -= hashCode >> 17; hashCode -= hashCode >> 11; hashCode -= hashCode >> 5; return hashCode; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //----------------------------------------------------------------------------- using System; using System.Collections.Generic; namespace System.Xml { // SecureStringHasher is a hash code provider for strings. The hash codes calculation starts with a seed (hasCodeRandomizer) which is usually // different for each instance of SecureStringHasher. Since the hash code depend on the seed, the chance of hashtable DoS attack in case when // someone passes in lots of strings that hash to the same hash code is greatly reduced. // The SecureStringHasher implements IEqualityComparer for strings and therefore can be used in generic IDictionary. internal class SecureStringHasher : IEqualityComparer{ int hashCodeRandomizer; public SecureStringHasher() { this.hashCodeRandomizer = Environment.TickCount; } public SecureStringHasher( int hashCodeRandomizer ) { this.hashCodeRandomizer = hashCodeRandomizer; } public int Compare( String x, String y ) { return String.Compare(x, y, StringComparison.Ordinal); } public bool Equals( String x, String y ) { return String.Equals( x, y, StringComparison.Ordinal ); } public int GetHashCode( String key ) { int hashCode = hashCodeRandomizer; // use key.Length to eliminate the rangecheck for ( int i = 0; i < key.Length; i++ ) { hashCode += ( hashCode << 7 ) ^ key[i]; } // mix it a bit more hashCode -= hashCode >> 17; hashCode -= hashCode >> 11; hashCode -= hashCode >> 5; return hashCode; } } } // 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
- FormatSettings.cs
- StatusBarPanelClickEvent.cs
- ObjectMemberMapping.cs
- FixedTextSelectionProcessor.cs
- StaticFileHandler.cs
- GACIdentityPermission.cs
- Encoder.cs
- SafeCryptContextHandle.cs
- EntityAdapter.cs
- XamlTemplateSerializer.cs
- DeviceContext2.cs
- AutomationPropertyChangedEventArgs.cs
- TabletCollection.cs
- ScriptingWebServicesSectionGroup.cs
- PrintPreviewDialog.cs
- dataobject.cs
- BindingValueChangedEventArgs.cs
- Validator.cs
- SessionEndingEventArgs.cs
- DetailsViewDeleteEventArgs.cs
- MethodBuilderInstantiation.cs
- RewritingPass.cs
- ColumnResult.cs
- COM2ExtendedBrowsingHandler.cs
- QueryExecutionOption.cs
- DataGridLength.cs
- AssemblyBuilderData.cs
- VirtualizingPanel.cs
- DataFormats.cs
- ObjectAssociationEndMapping.cs
- AttachedPropertyBrowsableForTypeAttribute.cs
- AppearanceEditorPart.cs
- securitycriticaldata.cs
- DataSourceHelper.cs
- TextRangeProviderWrapper.cs
- CorrelationInitializer.cs
- InvokePatternIdentifiers.cs
- StringSorter.cs
- ValidationHelpers.cs
- ResolveMatchesApril2005.cs
- TdsEnums.cs
- HostVisual.cs
- RSAPKCS1KeyExchangeFormatter.cs
- XmlSerializableServices.cs
- ControlParameter.cs
- TypeConverterValueSerializer.cs
- InOutArgument.cs
- FilterEventArgs.cs
- BamlResourceDeserializer.cs
- XsdBuilder.cs
- SynchronizedPool.cs
- ProfileServiceManager.cs
- SmtpDigestAuthenticationModule.cs
- InvalidOperationException.cs
- PropertyValueUIItem.cs
- AuthenticationException.cs
- __Filters.cs
- InputLangChangeRequestEvent.cs
- FormsAuthentication.cs
- path.cs
- InheritanceAttribute.cs
- EntityAdapter.cs
- EndEvent.cs
- View.cs
- ADMembershipProvider.cs
- ScriptIgnoreAttribute.cs
- UriTemplateLiteralPathSegment.cs
- HashMembershipCondition.cs
- TextEndOfLine.cs
- EntityDataSourceView.cs
- AdvancedBindingPropertyDescriptor.cs
- CheckPair.cs
- Part.cs
- PageFunction.cs
- TreeViewItem.cs
- AnimationClockResource.cs
- Guid.cs
- ValueQuery.cs
- ComponentManagerBroker.cs
- CharEnumerator.cs
- SafeRightsManagementSessionHandle.cs
- CompilationRelaxations.cs
- TableLayoutPanel.cs
- NetworkStream.cs
- StringComparer.cs
- SqlErrorCollection.cs
- ClockController.cs
- CompositeControl.cs
- ConstraintConverter.cs
- DataSourceControl.cs
- PropertyEntry.cs
- SQLBytes.cs
- HtmlElementErrorEventArgs.cs
- SafeNativeMethodsOther.cs
- LazyTextWriterCreator.cs
- CultureTable.cs
- Terminate.cs
- UInt16.cs
- ObservableCollection.cs
- NCryptNative.cs