Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / 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
- HMAC.cs
- PathParser.cs
- Dump.cs
- StringCollectionMarkupSerializer.cs
- ObjectQueryProvider.cs
- DataGridViewComboBoxColumnDesigner.cs
- HotSpotCollectionEditor.cs
- UserControlParser.cs
- ExpressionCopier.cs
- Identifier.cs
- FusionWrap.cs
- ZipIOFileItemStream.cs
- WinEventHandler.cs
- RunInstallerAttribute.cs
- StringKeyFrameCollection.cs
- SecurityPolicySection.cs
- ClientSettingsStore.cs
- AssertUtility.cs
- InstanceNormalEvent.cs
- Base64Decoder.cs
- DataGridState.cs
- WorkflowService.cs
- Section.cs
- AuthenticationModuleElementCollection.cs
- ClientSettingsProvider.cs
- HtmlSelectionListAdapter.cs
- OdbcEnvironmentHandle.cs
- Tile.cs
- RadioButtonPopupAdapter.cs
- PriorityBinding.cs
- CountdownEvent.cs
- TriggerCollection.cs
- AssemblyUtil.cs
- Avt.cs
- TreeIterator.cs
- WebException.cs
- RouteValueExpressionBuilder.cs
- DataTemplateKey.cs
- BooleanConverter.cs
- TextServicesDisplayAttribute.cs
- FontUnitConverter.cs
- GeneralTransform3D.cs
- ComNativeDescriptor.cs
- EndGetFileNameFromUserRequest.cs
- Evaluator.cs
- DataPagerField.cs
- CryptoSession.cs
- CoTaskMemHandle.cs
- AnimationException.cs
- ComponentCommands.cs
- GetCertificateRequest.cs
- PathTooLongException.cs
- RIPEMD160.cs
- ProxyElement.cs
- ApplicationActivator.cs
- TextSelectionHelper.cs
- WebResourceAttribute.cs
- SecureUICommand.cs
- PriorityQueue.cs
- GeneralTransform3DTo2DTo3D.cs
- Binding.cs
- DesignTimeSiteMapProvider.cs
- GraphicsContainer.cs
- XmlTextReader.cs
- XmlSchemaAnyAttribute.cs
- DataGridPreparingCellForEditEventArgs.cs
- DataViewSetting.cs
- SharedStatics.cs
- DataStreams.cs
- SkewTransform.cs
- StronglyTypedResourceBuilder.cs
- BaseResourcesBuildProvider.cs
- IncrementalHitTester.cs
- DataErrorValidationRule.cs
- CodeDOMUtility.cs
- BoundColumn.cs
- DesignerObject.cs
- PointLightBase.cs
- ItemCheckedEvent.cs
- Identity.cs
- EntityTypeEmitter.cs
- linebase.cs
- Bits.cs
- SudsParser.cs
- AnnotationComponentChooser.cs
- TransactionalPackage.cs
- ToolStripDropDown.cs
- _TransmitFileOverlappedAsyncResult.cs
- ComponentSerializationService.cs
- WindowsGraphics.cs
- RepeatButton.cs
- VerticalAlignConverter.cs
- HttpHandlerActionCollection.cs
- StateMachineWorkflowDesigner.cs
- PresentationSource.cs
- DataView.cs
- DataSourceXmlTextReader.cs
- ParseChildrenAsPropertiesAttribute.cs
- TypeName.cs
- ExpressionHelper.cs