Table of Contents

ApHash Class

Definition

Namespace
Bodu.IO.Hashing
Assembly
Bodu.IO.Hashing.dll
Package
Bodu.IO.Hashing 1.0.0
Source
ApHash.cs

Computes a 32-bit non-cryptographic hash using Arash Partow's APHash algorithm, which alternates XOR mixing patterns based on input byte-index parity. This class cannot be inherited.

public sealed class ApHash : NonCryptographicHashAlgorithm
Inheritance
ApHash
Inherited Members
Extension Methods

Remarks

APHash seeds its state with the constant 0xAAAAAAAA and combines each input byte using one of two XOR/shift mixes depending on whether the byte's position is even or odd. It is intended for hash-table lookups and similar non-cryptographic use cases.

When to choose APHash. APHash is one of the simplest position-aware mixes in the lib - pick it when a small, dependency-free 32-bit hash is enough and the performance budget per byte is tight. For better avalanche on hash-table keys prefer Fnv1a32 or MurmurHash3_32; for content fingerprinting prefer CityHash. APHash, JSHash, SDBM, and Pjw32 are siblings in the same low-cost niche and produce comparable distribution; the choice between them is usually driven by interop with an existing system that standardizes on one.

Output and lifecycle. Produces a 32-bit (4-byte) digest in little-endian byte order. GetCurrentHash() is non-destructive; instances are not thread-safe and should be allocated per consumer.

important

This algorithm is not cryptographically secure and should not be used for password hashing, digital signatures, or integrity validation in security-sensitive applications.

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

var ap = new ApHash();
byte[] digest = ap.ComputeHash(System.Text.Encoding.UTF8.GetBytes("dictionary-key"));

Constructors

ApHash()

Initializes a new instance of the ApHash class with a 32-bit hash size.

public ApHash()

Methods

Append(ReadOnlySpan<byte>)

When overridden in a derived class, appends the contents of source to the data already processed for the current hash computation.

public override void Append(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The data to process.

GetCurrentHashCore(Span<byte>)

When overridden in a derived class, writes the computed hash value to destination without modifying accumulated state.

protected override void GetCurrentHashCore(Span<byte> destination)

Parameters

destination Span<byte>

The buffer that receives the computed hash value.

Reset()

When overridden in a derived class, resets the hash computation to the initial state.

public override void Reset()

Applies to

ProductVersions
.NET8, 10