Skip to content

Add string.GetHashCodeWeak - #137828

Open
Manish-1kumar wants to merge 3 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/string-gethashcodeweak
Open

Manish-1kumar wants to merge 3 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/string-gethashcodeweak

Conversation

@Manish-1kumar

@Manish-1kumar Manish-1kumar commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Implements the API approved in #77679:

public static int GetHashCodeWeak(ReadOnlySpan<char> value);
public static int GetHashCodeWeak(ReadOnlySpan<char> value, StringComparison comparisonType);

Behavior

  • Ordinal uses the existing non-randomized ordinal hash, and OrdinalIgnoreCase uses the existing non-randomized ordinal-ignore-case hash. These are the fast hashes used internally by Dictionary<string, ...> before it switches to randomized hashing.
  • As discussed in API review, the result is XOR'ed with a per-process random seed, so the hash code isn't mistaken for a stable hash. The seed is generated on first use, and is separate from the Marvin seed, so weak hash codes don't reveal anything about the randomized hashing used by string.GetHashCode.
  • Other StringComparison values throw ArgumentException (only Ordinal and OrdinalIgnoreCase in v1, per the review).
  • The docs say the hash is fast but collision-prone and doesn't protect against crafted inputs, and that values can differ between processes and must not be persisted.

Testing

  • New tests cover consistency within a process, equivalence of the overloads and of span/string inputs, empty inputs, case-insensitive equality for OrdinalIgnoreCase, and unsupported comparison types.
  • GetHashCodeWeak (both overloads) is added to the existing cross-process test, which checks that the hash code for the same string differs between two processes.
  • CoreLib, the System.Runtime ref (including API compat) and System.Runtime.Tests build cleanly. The new tests were not run locally, since that requires a full local CoreCLR build.

Fixes #77679

Per API review, XOR the hash code with a per-process random seed so that
it isn't mistaken for a stable hash. The seed is separate from the Marvin
seed and is generated on first use. Update the docs to describe the weak,
process-specific nature of the hash, and restore the BOM in
String.Comparison.cs. Cover GetHashCodeWeak in the cross-process test.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 18, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Pass the per-process seed into shared span helpers that GetNonRandomizedHashCode
and GetNonRandomizedHashCodeOrdinalIgnoreCase also use with their existing
constant seed, and drop the unnecessary Debug.Fail.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: string.GetHashCodeNonRandomized

2 participants