Code39Mod43 Class
Definition
- Namespace
- Bodu.IO.Hashing.CheckDigits
- Assembly
- Bodu.IO.Hashing.dll
- Package
- Bodu.IO.Hashing 1.0.0
- Source
- Code39Mod43.cs
Computes the check character of a Code 39 (also known as Code 3 of 9) symbol string using the modulo 43
scheme. This class cannot be inherited.
public sealed class Code39Mod43 : AlphanumericCheckDigitAlgorithm
- Inheritance
-
Code39Mod43
- Inherited Members
- Extension Methods
Examples
// Single-call computation against an in-memory body.
char check = Code39Mod43.Compute("CODE39"); // 'W'
// Full-sequence validation.
bool ok = Code39Mod43.IsValid("CODE39W"); // true
// Streaming use when the body is built up incrementally.
var algo = new Code39Mod43();
algo.Append("CODE39");
char d = algo.GetCurrentCheckDigit(); // 'W'
Remarks
The Code 39 modulo-43 check character is the optional self-checking symbol defined by the Code 39 barcode symbology.
Each body character is mapped to its ordinal value in the forty-three-symbol Code 39 alphabet - '0'-
'9' as 0-9, 'A'-'Z' as 10-35, and the seven symbols '-', '.', space, '$',
'/', '+', and '%' as 36-42. The values are summed, reduced modulo 43, and the result is mapped
back to the alphabet to produce a single trailing check character.
The scheme detects all single-character substitution errors. Because the underlying combiner is a commutative sum, it does not detect every adjacent transposition; payloads that require transposition coverage should prefer Verhoeff or Damm over a decimal body.
Worked example. For the body "CODE39" the character values are 12, 24, 13, 14, 3, and 9, summing to
75; 75 mod 43 is 32, which maps to 'W'. The resulting string "CODE39W" 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
Code39Mod43()
Initializes a new instance of the Code39Mod43 class.
public Code39Mod43()
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 Code 39 modulo-43 check character 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 Code 39 alphabet.
Returns
- char
The check character drawn from the Code 39 alphabet.
Exceptions
- ArgumentOutOfRangeException
Thrown when
bodycontains any character outside the Code 39 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 Code 39 check character, is consistent.
public static bool IsValid(ReadOnlySpan<char> valueIncludingCheck)
Parameters
valueIncludingCheckReadOnlySpan<char>The complete sequence including the trailing check character.
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 |