Table of Contents

Bernstein Class

Definition

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

Computes a 32-bit non-cryptographic hash using Daniel J. Bernstein's djb2 algorithm, optionally using the XOR-modified variant. This class cannot be inherited.

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

Remarks

The default algorithm computes hash = (hash * 33) + c for each input byte c. Setting UseModifiedAlgorithm selects the XOR-modified form, hash = (hash * 33) ^ c, which may give better distribution in some hash-table workloads.

Both InitialValue and UseModifiedAlgorithm are reconfigurable only while the algorithm has not yet consumed input. Reset() returns the instance to the reconfigurable state.

When to choose Bernstein. djb2 is the canonical "C-style" hash for short string keys - language symbol tables, environment-variable maps, and small associative containers. Pick it when interoperating with code that has standardized on djb2 (Perl, Python's older string hash, Tcl variable tables, etc.) or when the seed/variant flexibility is useful. Empirically the XOR-modified form (djb2a, UseModifiedAlgorithm set to true) gives slightly better avalanche than the default additive form. For new code without an interop constraint, Fnv1a32 is a closely related but better-distributing default; MurmurHash3_32 is preferable for inputs longer than a few dozen bytes.

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

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;

// Default djb2 with the canonical 5381 seed.
var djb2 = new Bernstein();
byte[] digest = djb2.ComputeHash(System.Text.Encoding.UTF8.GetBytes("symbol"));

// XOR-modified djb2a, generally better distribution.
var djb2a = new Bernstein { UseModifiedAlgorithm = true };
byte[] digestA = djb2a.ComputeHash(System.Text.Encoding.UTF8.GetBytes("symbol"));

Constructors

Bernstein()

Initializes a new instance of the Bernstein class with the canonical djb2 seed ( DefaultInitialValue) and the original addition form of the algorithm.

public Bernstein()

Bernstein(uint, bool)

Initializes a new instance of the Bernstein class using the specified initial seed value and algorithm variant.

public Bernstein(uint initialValue, bool useModifiedAlgorithm)

Parameters

initialValue uint

The initial seed applied to the running hash accumulator.

useModifiedAlgorithm bool

true to use the XOR-modified form hash = (hash * 33) ^ c; false to use the original hash = (hash * 33) + c.

Fields

DefaultInitialValue

The default initial value used to seed the hash algorithm.

public const uint DefaultInitialValue = 5381

Field Value

uint

Properties

InitialValue

Gets or sets the initial seed value applied to the running hash accumulator.

public uint InitialValue { get; set; }

Property Value

uint

The initial hash seed. Defaults to DefaultInitialValue.

Exceptions

CryptographicUnexpectedOperationException

The algorithm has already consumed input and cannot be reconfigured until Reset() is invoked.

UseModifiedAlgorithm

Gets or sets a value indicating whether the XOR-modified form of the algorithm is in use.

public bool UseModifiedAlgorithm { get; set; }

Property Value

bool

true when each update performs (hash * 33) ^ c; false when it performs (hash * 33) + c.

Exceptions

CryptographicUnexpectedOperationException

The algorithm has already consumed input and cannot be reconfigured until Reset() is invoked.

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