Crockford32 Class
Definition
- Namespace
- Bodu.IO.Hashing.CheckDigits
- Assembly
- Bodu.IO.Hashing.dll
- Package
- Bodu.IO.Hashing 1.0.0
- Source
- Crockford32.cs
Computes the check symbol of a Crockford Base32 encoded value using the modulo 37 scheme defined by Douglas
Crockford. This class cannot be inherited.
public sealed class Crockford32 : AlphanumericCheckDigitAlgorithm
- Inheritance
-
Crockford32
- Inherited Members
- Extension Methods
Examples
// Single-call computation against an in-memory body.
char check = Crockford32.Compute("16J"); // 'D'
// Full-sequence validation.
bool ok = Crockford32.IsValid("16JD"); // true
// Streaming use when the body is built up incrementally.
var algo = new Crockford32();
algo.Append("16J");
char d = algo.GetCurrentCheckDigit(); // 'D'
Remarks
Crockford's Base32 specification defines an optional trailing check symbol equal to the encoded integer value taken
modulo 37. The body is decoded as a Base32 number using the thirty-two-symbol Crockford alphabet ('0'-
'9' and 'A'-'Z' excluding 'I', 'L', 'O', and 'U'); decoding is
case-insensitive and treats 'I'/'L' as 1 and 'O' as 0. The running value is
reduced modulo 37 by Horner's method, so arbitrarily long inputs are processed without arbitrary-precision
arithmetic.
Because the modulus 37 is prime and exceeds the alphabet size, five additional check-only symbols represent the
values 32 through 36: '*', '~', '$', '=', and 'U'. This is the check scheme
commonly paired with ULID and other Crockford Base32 identifiers.
Worked example. The body "16J" decodes to 1234; 1234 mod 37 is 13, which maps to 'D'. The
resulting string "16JD" is therefore valid.
important
This algorithm is not cryptographically secure and should not be used for password hashing, digital signatures, or integrity validation in security-sensitive applications.
Constructors
Crockford32()
Initializes a new instance of the Crockford32 class.
public Crockford32()
Properties
AlgorithmName
Gets the canonical name of the algorithm, suitable for diagnostic output and logging.
public override string AlgorithmName { get; }
Property Value
- string
A short, stable identifier such as
"Luhn","IBAN", or"ISO 7064 MOD 97-10".
InputAlphabet
Gets the subset of ASCII from which this algorithm accepts body characters.
public override CheckDigitInputAlphabet InputAlphabet { get; }
Property Value
- CheckDigitInputAlphabet
The declared input alphabet.
OutputAlphabet
Gets the subset of ASCII from which this algorithm may emit its check character.
public override CheckDigitOutputAlphabet OutputAlphabet { get; }
Property Value
- CheckDigitOutputAlphabet
The declared output alphabet.
Methods
Append(ReadOnlySpan<char>)
Absorbs the supplied characters into the running check-value state.
public override void Append(ReadOnlySpan<char> body)
Parameters
bodyReadOnlySpan<char>The characters to append. Each element must belong to the algorithm's accepted input alphabet. An empty span is a permitted no-op.
Exceptions
- ArgumentOutOfRangeException
Thrown when
bodycontains any character outside the algorithm's accepted input alphabet.
Compute(ReadOnlySpan<char>)
Computes the Crockford Base32 check symbol for the supplied body without allocating a streaming instance.
public static char Compute(ReadOnlySpan<char> body)
Parameters
bodyReadOnlySpan<char>The body characters. Each must belong to the Crockford Base32 alphabet.
Returns
- char
The check symbol drawn from the Crockford Base32 check alphabet.
Exceptions
- ArgumentOutOfRangeException
Thrown when
bodycontains any character outside the Crockford Base32 alphabet.
GetCurrentCheckDigit()
Returns the check character computed for the body absorbed since the last Reset() (or since construction).
public override char GetCurrentCheckDigit()
Returns
- char
The check character as an ASCII character drawn from OutputAlphabet.
Remarks
This call is non-destructive; the running state is unaffected and the method may be invoked any number of times with identical results between appends.
IsValid(ReadOnlySpan<char>)
Determines whether the supplied sequence, comprising a body followed by a trailing Crockford Base32 check symbol, is consistent.
public static bool IsValid(ReadOnlySpan<char> valueIncludingCheck)
Parameters
valueIncludingCheckReadOnlySpan<char>The complete sequence including the trailing check symbol.
Returns
Reset()
Resets the algorithm to its initial state, discarding any characters previously absorbed.
public override void Reset()
Remarks
Equivalent in behavior to constructing a fresh instance of the same concrete type.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |