Table of Contents

BKDR Class

Definition

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

Computes a 32-bit non-cryptographic hash using the BKDR polynomial rolling algorithm from Kernighan and Ritchie's "The C Programming Language". This class cannot be inherited.

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

Remarks

For each input byte c the hash is updated as hash = (hash * seed) + c. The Seed multiplier must be one of the supported values (31, 131, 1313, 13131, 131313, 1313131, 13131313, 131313131, 1313131313) and can be reassigned only while the algorithm has not yet consumed any input.

When to choose BKDR. BKDR is the polynomial rolling hash from K&R's "The C Programming Language" - a textbook choice for symbol tables, lexer keyword maps, and other small-string keying tasks. Pick it when interoperating with code that already uses the K&R formulation, or when the input is a short identifier and avalanche quality matters less than predictable behavior. For modern hash-table workloads prefer Fnv1a32 (better avalanche on the same per-byte cost) or MurmurHash3_32 (much better distribution on inputs longer than ~16 bytes).

Output and lifecycle. Produces a 32-bit (4-byte) digest in little-endian byte order. GetCurrentHash() is non-destructive; Reset() returns the instance to its reconfigurable, pre-input state. 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 seed (131); appropriate for short identifier keys.
var bkdr = new BKDR();
byte[] digest = bkdr.ComputeHash(System.Text.Encoding.UTF8.GetBytes("Identifier"));

Constructors

BKDR()

Initializes a new instance of the BKDR class with the default seed of 131.

public BKDR()

BKDR(uint)

Initializes a new instance of the BKDR class using the specified seed multiplier.

public BKDR(uint seed)

Parameters

seed uint

The seed multiplier applied to each byte. Must be one of the supported seed constants.

Exceptions

ArgumentException

seed is not one of the supported values (31, 131, 1313, 13131, 131313, 1313131, 13131313, 131313131, 1313131313).

Fields

DefaultSeed

Represents the default seed value used by the BKDR hash algorithm.

public const uint DefaultSeed = 131

Field Value

uint

Properties

Seed

Gets or sets the seed multiplier applied on each byte update.

public uint Seed { get; set; }

Property Value

uint

The seed value. Must be one of the supported seed constants.

Exceptions

ArgumentException

The assigned value is not one of the supported seed values.

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