jaytwo.StableHashing 0.1.0-beta-20260407094328

This is a prerelease version of jaytwo.StableHashing.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package jaytwo.StableHashing --version 0.1.0-beta-20260407094328
                    
NuGet\Install-Package jaytwo.StableHashing -Version 0.1.0-beta-20260407094328
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="jaytwo.StableHashing" Version="0.1.0-beta-20260407094328" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="jaytwo.StableHashing" Version="0.1.0-beta-20260407094328" />
                    
Directory.Packages.props
<PackageReference Include="jaytwo.StableHashing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add jaytwo.StableHashing --version 0.1.0-beta-20260407094328
                    
#r "nuget: jaytwo.StableHashing, 0.1.0-beta-20260407094328"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package jaytwo.StableHashing@0.1.0-beta-20260407094328
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=jaytwo.StableHashing&version=0.1.0-beta-20260407094328&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=jaytwo.StableHashing&version=0.1.0-beta-20260407094328&prerelease
                    
Install as a Cake Tool

jaytwo.StableHashing

NuGet Version NuGet Downloads License: MIT

Deterministic FNV-1a hashing for .NET - produces consistent hash values across runs, processes, machines, and .NET versions.

View source on GitHub

Features

  • 32-bit and 64-bit FNV-1a hash implementations
  • Public StableHash, Fnv1a32, and Fnv1a64 APIs
  • Optional HashAlgorithm implementations for 32-bit and 64-bit FNV-1a
  • Accepts string and byte[] inputs on all targets, plus ReadOnlySpan<byte> on net6.0 and net8.0
  • Returns hashes as uint/ulong, signed int/long, lowercase hex strings, or big-endian byte arrays
  • Optional forcePositive mode for signed integer output (clears the high bit)
  • UTF-8 encoding by default for strings; custom Encoding overloads available
  • Targets net45, netstandard2.0, net6.0, and net8.0

Background

.NET's built-in GetHashCode() is randomized per-process by default (since .NET Core), making it unsuitable for scenarios that require consistent hash values over time - such as sharding, bucketing, caching keys, or content fingerprinting. FNV-1a is a well-known non-cryptographic hash function with a fixed algorithm, so the same input always produces the same output regardless of runtime, platform, or process.

Cryptographic vs. non-cryptographic hashing

These terms describe different design goals, and choosing the wrong one for the job is a common mistake:

Cryptographic hash functions (SHA-256, SHA-512, bcrypt, Argon2, etc.) are designed to be computationally hard to reverse or forge. They are the right choice when security properties matter - verifying passwords, signing data, detecting tampering, or any scenario where an adversary might try to craft a collision.

Non-cryptographic hash functions (FNV-1a, MurmurHash, xxHash, etc.) are designed to be fast and produce well-distributed values. They make no security guarantees - a determined attacker can construct inputs that collide. They are the right choice when you need consistent, stable, high-performance hashing for internal purposes: sharding data across nodes, consistent bucketing, cache key generation, or stable identifiers derived from content.

StableHash falls squarely in the second category. If you are hashing passwords, generating MACs, or doing anything where an adversary controls the input and a collision would be harmful, use a cryptographic primitive instead.

Installation

Add the NuGet package:

PM> Install-Package jaytwo.StableHashing

Usage

The main entry point is the StableHash class.

Hash a string to an integer

using jaytwo.StableHashing;

// 32-bit unsigned
uint hash32 = StableHash.HashToUInt32("hello");        // 0x4F9F2CAB

// 64-bit unsigned
ulong hash64 = StableHash.HashToUInt64("hello");       // 0xA430D84680AABD0B

// Signed 32-bit (may be negative)
int signed32 = StableHash.HashToInt32("hello");

// Signed 32-bit, guaranteed non-negative (high bit cleared)
int positive32 = StableHash.HashToInt32("hello", forcePositive: true);

// Signed 64-bit, guaranteed non-negative
long positive64 = StableHash.HashToInt64("hello", forcePositive: true);

Hash to a hex string

string hex32 = StableHash.HashToHex32("hello");   // "4f9f2cab"
string hex64 = StableHash.HashToHex64("hello");   // "a430d84680aabd0b"

Use the raw FNV helpers

using jaytwo.StableHashing;

uint hash32 = Fnv1a32.HashData("hello");
byte[] bytes32 = Fnv1a32.HashDataAsBytes("hello");   // big-endian

ulong hash64 = Fnv1a64.HashData("hello");
byte[] bytes64 = Fnv1a64.HashDataAsBytes("hello");   // big-endian

Use the HashAlgorithm wrappers

using jaytwo.StableHashing;

byte[] data = SomeMethod();

using var algorithm = new Fnv1a64HashAlgorithm();
byte[] hash = algorithm.ComputeHash(data);   // 8-byte big-endian output

Hash bytes

byte[] data = SomeMethod();

uint hash = StableHash.HashToUInt32(data);
string hex = StableHash.HashToHex64(data.AsSpan());

Custom encoding

using System.Text;

uint hash = StableHash.HashToUInt32("hello", Encoding.Unicode);

Notes

  • FNV-1a is not a cryptographic hash - see Cryptographic vs. non-cryptographic hashing above. Do not use it for passwords, signatures, or any security-sensitive purpose.
  • The forcePositive option clears the most significant bit before casting to a signed type. This narrows the output range slightly (31 or 63 effective bits) but avoids negative values in contexts that require non-negative integers (e.g., database IDs, modulo bucketing).
  • ReadOnlySpan<byte> overloads are available on net6.0 and net8.0. byte[] and string overloads are available on every target.
  • Fnv1a32HashAlgorithm returns 4-byte big-endian output and Fnv1a64HashAlgorithm returns 8-byte big-endian output.

Made with ♥ by Jake - Licensed under the MIT License

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on jaytwo.StableHashing:

Package Downloads
jaytwo.Ergonomics.Ado

Transparent ergonomic extensions for base ADO.NET objects.

jaytwo.Ergonomics.Logging

Ergonomic structured logging builders for Microsoft.Extensions.Logging.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0-beta-20260927074007 150 9/27/2026
0.1.0-beta-20260915080614 110 9/15/2026
0.1.0-beta-20260407094328 98 4/7/2026
0.1.0-beta-20260407092806 80 4/7/2026