Table of Contents

CityHash32 Class

Definition

Namespace
Bodu.IO.Hashing
Assembly
Bodu.IO.Hashing.dll
Package
Bodu.IO.Hashing 1.0.0
Source
CityHash.32.cs

Computes a 32-bit (4-byte) non-cryptographic hash using the CityHash32 variant by Google. This class cannot be inherited.

public sealed class CityHash32 : CityHash, IDisposable
Inheritance
CityHash32
Implements
Inherited Members
Extension Methods

Remarks

CityHash32 dispatches to one of four internal mixing paths based on input length: a loop over individual bytes for 0-4 bytes; a four-word path for 5-12 bytes; a six-word path for 13-24 bytes; and a full iterative path consuming 20-byte blocks with three interleaved accumulators for 25 or more bytes. All paths converge through the Mur and Mix primitives defined in CityHash.

Parameters at a glance.

  • Output size: 32 bits (4 bytes), little-endian.
  • Variant: CityHash32.
  • Length-dispatched mixing: 0-4, 5-12, 13-24, and 25+ byte paths.
  • Seedless - for seeded variants prefer MurmurHash3_32.

When to choose CityHash32. Pick CityHash32 for 32-bit slot indexes when throughput on long inputs matters - it edges MurmurHash3_32 on 64-bit hosts. For short fixed-length keys Fnv1a32 is simpler and competitive; if a seed is needed (bloom filters, hash-flooding mitigation) prefer MurmurHash3_32.

important

This algorithm is not cryptographically secure and must not be used for password hashing, digital signatures, or any application requiring adversarial collision resistance.

using Bodu.IO.Hashing;
using Bodu.IO.Hashing.Extensions;

var city = new CityHash32();
byte[] digest = city.ComputeHash(System.Text.Encoding.UTF8.GetBytes("session-key"));

Constructors

CityHash32()

Initializes a new instance of the CityHash32 class with a fixed 32-bit (4-byte) hash output size.

public CityHash32()

Methods

ComputeHashCore(ReadOnlySpan<byte>)

Computes the 32-bit CityHash of the provided input span, selecting the optimal mixing path based on input length.

protected override byte[] ComputeHashCore(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The input bytes to hash.

Returns

byte[]

A 4-byte array containing the little-endian encoded 32-bit hash value.

Applies to

ProductVersions
.NET8, 10

See Also