Table of Contents

Pjw32 Class

Definition

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

Computes a 32-bit non-cryptographic hash using Peter J. Weinberger's PJW shift-and-fold algorithm (as described in the "Dragon Book"). This class cannot be inherited.

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

Remarks

For each input byte, the hash is shifted left by 4 bits and the byte added; any overflow into the top 4 bits is then XOR-folded back into the low-order bits, producing a well-distributed hash for identifier and symbol-table use.

When to choose PJW32. PJW is the symbol-table hash from Aho/Sethi/Ullman's "Compilers: Principles, Techniques, and Tools" (the "Dragon Book") and the precursor to the ELF hash family (Elf64) - pick it when interoperating with compiler-generated symbol tables or reproducing a digest from an algorithm based on the Dragon Book formulation. For general-purpose hash-table keying prefer Fnv1a32 (closely related but better-distributing) 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; 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;

var pjw = new Pjw32();
byte[] digest = pjw.ComputeHash(System.Text.Encoding.ASCII.GetBytes("symbol_name"));

Constructors

Pjw32()

Initializes a new instance of the Pjw32 class.

public Pjw32()

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