CheckDigitAlgorithm Class
Definition
- Namespace
- Bodu.IO.Hashing.CheckDigits
- Assembly
- Bodu.IO.Hashing.dll
- Package
- Bodu.IO.Hashing 1.0.0
- Source
- CheckDigitAlgorithm.cs
Represents the abstract base class from which all decimal check-digit algorithms in this library derive.
public abstract class CheckDigitAlgorithm : CheckValueAlgorithm
- Inheritance
-
CheckDigitAlgorithm
- Derived
- Inherited Members
- Extension Methods
Examples
// Use a concrete derivative through the abstract surface.
CheckDigitAlgorithm algo = new Luhn();
algo.Append("7992739871");
char check = algo.GetCurrentCheckDigit(); // '3'
// Reset between independent computations.
algo.Reset();
algo.Append('1');
algo.Append("7893729977");
Remarks
A check-digit algorithm consumes a sequence of decimal digits ('0' to '9') and yields a single
trailing digit that, when appended to the body, allows the concatenated sequence to be validated - matching the
domain of card numbers, national identifiers, and serial codes on which these algorithms are typically applied.
This is a deliberately separate family from the byte-stream oriented NonCryptographicHashAlgorithm, and it does not derive from it. The two serve different purposes: a non-cryptographic hash produces a fixed-length opaque byte digest over arbitrary input, whereas a check digit performs error detection over a constrained ASCII text alphabet and emits a single char result. Modeling one as the other would either narrow the hash contract or reduce the check character to an encoding artifact, so the families are kept distinct by design.
The streaming surface - Append(ReadOnlySpan<char>),
Reset(), and GetCurrentCheckDigit() - will nonetheless feel familiar
to anyone who has used a hash algorithm: Append(ReadOnlySpan<char>) accumulates
input, Reset() restarts the computation, and reading the current check digit is
non-destructive and idempotent. That resemblance is incidental convenience for the reader's intuition, not a shared
contract. On an empty body every built-in algorithm in this library returns the digit '0'; concrete
implementations document any exception.
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
CheckDigitAlgorithm()
Initializes a new instance of the CheckDigitAlgorithm class.
protected CheckDigitAlgorithm()
Methods
GetCurrentCheckDigit()
Returns the check digit computed for the body absorbed since the last Reset() (or since construction).
public abstract char GetCurrentCheckDigit()
Returns
- char
The check digit as an ASCII character in the range
'0'to'9'. For an empty body all built-in algorithms return'0'.
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.
GetCurrentCheckValue()
Returns the check value computed for the body absorbed since the last Reset() (or since construction) as a newly allocated string.
public override sealed 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.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |