CheckValueAlgorithm Class
Definition
- Namespace
- Bodu.IO.Hashing.CheckDigits
- Assembly
- Bodu.IO.Hashing.dll
- Package
- Bodu.IO.Hashing 1.0.0
- Source
- CheckValueAlgorithm.cs
Represents the common root of the check-digit algorithm family, unifying the streaming surface shared by CheckDigitAlgorithm (decimal bodies, single check digit), AlphanumericCheckDigitAlgorithm (alphanumeric bodies, single check character), and MultiCharCheckDigitAlgorithm (multi-character check codes).
public abstract class CheckValueAlgorithm
- Inheritance
-
CheckValueAlgorithm
- Derived
- Inherited Members
- Extension Methods
Remarks
Every algorithm in the family absorbs body characters through Append(ReadOnlySpan<char>), restarts through Reset(), and exposes its current check value non-destructively. This root makes that shared contract polymorphic: GetCurrentCheckValue() returns the check value as a string of length CheckLength regardless of which branch of the family the concrete algorithm belongs to, so callers can validate heterogeneous identifier formats through a single reference type.
The intermediate base classes retain their more precise result surfaces - GetCurrentCheckDigit() returning a
char on the single-character branches and GetCurrentCheckDigits(Span{char}) on the
multi-character branch - and implement GetCurrentCheckValue() by delegation.
Instances are not thread-safe. Each thread that needs a running check should construct its own instance.
important
Check-digit algorithms are error-detection primitives, not cryptographic functions. They must not be used for password hashing, digital signatures, or integrity validation in security-sensitive applications.
Constructors
CheckValueAlgorithm()
Initializes a new instance of the CheckValueAlgorithm class.
protected CheckValueAlgorithm()
Properties
AlgorithmName
Gets the canonical name of the algorithm, suitable for diagnostic output and logging.
public abstract string AlgorithmName { get; }
Property Value
- string
A short, stable identifier such as
"Luhn","IBAN", or"ISO 7064 MOD 97-10".
CheckLength
Gets the number of characters in the check value this algorithm emits.
public virtual int CheckLength { get; }
Property Value
- int
A positive integer;
1for the single-character families, or the fixed code length (for example2for ISO 7064 MOD 97-10) on the multi-character branch.
Methods
Append(char)
Absorbs a single character into the running check-value state.
public void Append(char ch)
Parameters
chcharThe character to append. Must belong to the algorithm's accepted input alphabet.
Exceptions
- ArgumentOutOfRangeException
Thrown when
chis outside the algorithm's accepted input alphabet.
Append(ReadOnlySpan<char>)
Absorbs the supplied characters into the running check-value state.
public abstract 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.
GetCurrentCheckValue()
Returns the check value computed for the body absorbed since the last Reset() (or since construction) as a newly allocated string.
public abstract string GetCurrentCheckValue()
Returns
- string
A string of length CheckLength containing the check value.
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. The single-character branches also expose the result as a
char via GetCurrentCheckDigit(), which avoids the string allocation.
Reset()
Resets the algorithm to its initial state, discarding any characters previously absorbed.
public abstract void Reset()
Remarks
Equivalent in behavior to constructing a fresh instance of the same concrete type.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |