Bodu.IO.Hashing Namespace
- Package
-
Bodu.IO.Hashing 1.0.0
Purpose
Bodu.IO.Hashing is a focused library of non-cryptographic hashes, checksums, and check digits built on the BCL NonCryptographicHashAlgorithm contract. It covers three subfamilies: fingerprints (FNV, CityHash, MurmurHash3, Pearson, classic string hashes), checksums (the full CRC RevEng catalogue at widths 1-64 bits, Fletcher 16/32/64, Adler 32/32C/64), and check digits (Luhn, Damm, Verhoeff, EAN/GTIN/UPC, ISIN, ABA routing, plus the alphanumeric and multi-character identifier schemes such as ISBN, IBAN, LEI, SEDOL, and CUSIP).
Reach for this library when you need a fast, deterministic checksum for error detection, file integrity, framing, fingerprinting, or human-typed identifier validation - and when you want the result to drop straight into any API that accepts NonCryptographicHashAlgorithm. If you need cryptographic integrity (against an active attacker, not just noise on the wire), see Bodu.Security.Cryptography instead.
BCL note.
XxHash32,XxHash64,XxHash3, andXxHash128already ship inSystem.IO.Hashingfrom .NET 6 onwards - Bodu does not duplicate them. Use the BCL types directly when you want xxHash.
Static documentation
- Bodu.IO.Hashing introduction - namespaces, headline types, scenarios.
- Bodu.IO.Hashing getting started - install and minimal samples for each subfamily.
- Bodu.IO.Hashing guides - per-algorithm walk-throughs.
- Bodu.Security.Cryptography introduction - the sibling library, for keyed and cryptographic hashes with a formal adversary model.
Key types
Fingerprints - Bodu.IO.Hashing
- Fnv1a32 / Fnv1a64, Fnv132 / Fnv164 - constant-memory streaming FNV; prefer
Fnv1a*for better avalanche. - CityHash32 / CityHash64 / CityHash128 - SIMD-friendly hashes optimized for long inputs.
- MurmurHash3_32 / MurmurHash3_128 - seeded high-avalanche fingerprints.
- Pearson - table-driven hash with output widths from 8 to 2048 bits in 8-bit steps; five built-in permutation tables via Pearson.PearsonTableType.
- Bernstein, BKDR, SDBM, JSHash, Elf64, ApHash, Pjw32, SuperFastHash - classic string hashes from compilers and early web tooling.
- BlockNonCryptographicHashAlgorithm - abstract base for buffered block-oriented algorithms.
- IResumableHashAlgorithm - optional contract that reverse-finalizes a stored digest and continues appending; implemented by Crc, the Fnv family, the Fletcher family, and the Adler family (Adler32Base / Adler64Base).
Checksums - Bodu.IO.Hashing.Checksums
- Crc - the single CRC engine. Configured with a CrcStandard, it handles widths from 1 to 64 bits, honors polynomial, initial value, input / output reflection, and final XOR, and ships with a shared lookup-table cache.
- CrcStandard - an immutable parameter set: name, width, polynomial, initial value, reflect-in, reflect-out, XOR-out. Exposes common standards as named properties (
CRC32_ISOHDLC,CRC32_ISCSI,CRC16_MODBUS,CRC64_XZ, …) and providesFromName/TryFromNameover canonical names and published aliases. - CrcStandards - an enum covering every canonical CRC RevEng entry (112 standards as of the last catalogue fetch).
- CrcLookupTableCache - thread-safe cache of 256-entry lookup tables, keyed by (width, polynomial, reflect-in), shared process-wide through GlobalCache.
- CrcLookupTableBuilder - builds a lookup table from parameters; used by the cache on first miss.
- Fletcher16 / Fletcher32 / Fletcher64 - twin-accumulator position-dependent checksums.
- Adler32 / Adler32C / Adler64 - Adler-32 (canonical zlib), Adler-32C (SIMD), Adler-64; over the shared Adler32Base / Adler64Base abstract bases.
Check digits - Bodu.IO.Hashing.CheckDigits
- Luhn, Damm, Verhoeff - decimal-alphabet single-character check-digit algorithms with progressively wider error coverage.
- Ean8, Ean13, Gtin14, UpcA - retail / shipping barcodes.
- Isin, AbaRoutingNumber - securities and bank-routing identifiers.
- Iban, Isbn10, Isbn13, Sedol, Cusip, Lei, Iso7064Mod11_2, Iso7064Mod97_10 - alphanumeric and multi-character identifier schemes.
- CheckValueAlgorithm - the abstract root of the hierarchy; its derivatives CheckDigitAlgorithm, AlphanumericCheckDigitAlgorithm, and MultiCharCheckDigitAlgorithm are the extension points for custom decimal, alphanumeric, and multi-character schemes.
Extensions - Bodu.IO.Hashing.Extensions
- NonCryptographicHashAlgorithmExtensions -
AppendData,AppendDataAsync,ComputeHash,ComputeHashAsync,VerifyHash,VerifyHashAsync,TryVerifyHash,TryVerifyHashAsync.
Example
using System.Text;
using Bodu.IO.Hashing;
using Bodu.IO.Hashing.Checksums;
byte[] data = Encoding.UTF8.GetBytes("the quick brown fox");
// CRC-32/ISO-HDLC - the canonical zlib / PNG / Ethernet CRC.
using var crc = new Crc(CrcStandard.CRC32_ISOHDLC);
crc.Append(data);
string crc32 = Convert.ToHexString(crc.GetCurrentHash());
// Fletcher-32 - position-dependent, drops into anything that takes a NonCryptographicHashAlgorithm.
using var fletcher = new Fletcher32();
fletcher.Append(data);
string fl32 = Convert.ToHexString(fletcher.GetCurrentHash());
// Resume a CRC from a previously stored digest and keep hashing.
byte[] previous = crc.GetCurrentHash();
byte[] combined = crc.ComputeHashFrom(previous, Encoding.UTF8.GetBytes(" jumps over"));
Notes
- Not cryptographically secure. Every algorithm here is designed for error detection and hash-table distribution, not authentication. An attacker who can choose the input can trivially forge the output. Pair with a MAC or signature if integrity against an adversary matters - see SipHash64 for a keyed short-input hash, or
System.Security.Cryptography.SHA256for a full cryptographic digest. - Shared lookup tables. Crc instances with identical (width, polynomial, reflect-in) triples share a single 256-entry lookup table through GlobalCache. Constructing a hundred
Crc(CrcStandard.CRC32_ISOHDLC)instances allocates one table, not a hundred. - Non-destructive
GetCurrentHash. CallingNonCryptographicHashAlgorithm.GetCurrentHashsnapshots the accumulator and applies the final reflect / XOR / width-mask on the copy, so in-progress hashing is not disturbed. Call it as many times as you like. - Resumable. Crc, the FNV family, the Fletcher family, and the Adler family implement IResumableHashAlgorithm - reverse-finalize a stored digest, append further data, re-finalize. Handy for chunked streams where re-reading earlier bytes is expensive.
- Determinism and portability. All algorithms produce identical byte-for-byte output across platforms and architectures for the same input and configuration.
- See also: the Using CRC and Using Fletcher guides, the full CRC catalogue, and the Bodu.IO.Hashing introduction.
Namespaces
Classes
- ApHash
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.
- BKDR
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.
- Bernstein
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.
- BlockNonCryptographicHashAlgorithm
Base class for non-cryptographic hash algorithms whose internal state advances one fixed-size block at a time - handles residual buffering, block alignment, total-length tracking, snapshot-based
GetCurrentHash, and optional final-block padding so that derived implementations only need to express the per-block compression step.
- CityHash
Base class for the
CityHashfamily of non-cryptographic hash algorithms developed by Google. See the CityHash reference repository for the specification.
- CityHash128
Computes a 128-bit (16-byte) non-cryptographic hash using the
CityHash128variant by Google. This class cannot be inherited.
- CityHash32
Computes a 32-bit (4-byte) non-cryptographic hash using the
CityHash32variant by Google. This class cannot be inherited.
- CityHash64
Computes a 64-bit (8-byte) non-cryptographic hash using the
CityHash64variant by Google. This class cannot be inherited.
- Elf64
Computes a 64-bit non-cryptographic hash using the ELF (Executable and Linkable Format) hash algorithm originally used in UNIX System V object files. This class cannot be inherited.
- Fnv
Provides a base class for the Fowler-Noll-Vo (FNV) hash family, supporting both the FNV-1 and FNV-1a variants at 32-bit and 64-bit widths.
- Fnv132
Computes the hash for the input data using the
FNV-132-bit hash algorithm. This class cannot be inherited.
- Fnv164
Computes the hash for the input data using the
FNV-164-bit hash algorithm. This class cannot be inherited.
- Fnv1a32
Computes the hash for the input data using the
FNV-1a32-bit hash algorithm. This class cannot be inherited.
- Fnv1a64
Computes the hash for the input data using the
FNV-1a64-bit hash algorithm. This class cannot be inherited.
- HashingStream
Provides a pass-through Stream that feeds every byte read from or written to an inner stream into a NonCryptographicHashAlgorithm, so a digest can be computed while data is being transferred.
- JSHash
Computes a 32-bit non-cryptographic hash using Justin Sobel's JSHash bitwise mixing function. This class cannot be inherited.
- MurmurHash3
Base class for the
MurmurHash3family of non-cryptographic hash algorithms by Austin Appleby. See the SMHasher reference repository for the specification.
- MurmurHash3_128
Computes a 128-bit (16-byte) non-cryptographic hash using the
MurmurHash3_x64_128variant by Austin Appleby, optimized for 64-bit platforms. This class cannot be inherited.
- MurmurHash3_32
Computes a 32-bit (4-byte) non-cryptographic hash using the
MurmurHash3_x86_32variant by Austin Appleby. This class cannot be inherited.
- Pearson
Computes the hash for the input data using the
Pearsonhash algorithm. This variant applies a non-cryptographic permutation-based transformation using a 256-byte lookup table to produce compact hash values. This class cannot be inherited.
- Pjw32
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.
- SDBM
Computes a 32-bit non-cryptographic hash using the SDBM algorithm popularized by the public-domain NDBM database library. This class cannot be inherited.
- SuperFastHash
Computes a 32-bit non-cryptographic hash using Paul Hsieh's
SuperFastHashalgorithm, intended for hash-table keying. This class cannot be inherited.
Interfaces
- IResumableHashAlgorithm
Marks a non-cryptographic hash algorithm whose internal state can be reconstructed from a previously emitted digest, so additional input can be folded into an existing hash without re-reading the bytes that produced it.
Enums
- Pearson.PearsonTableType
Defines the available permutation table presets that can be used with the Pearson hashing algorithm.