Isbn10 Class
Definition
- Namespace
- Bodu.IO.Hashing.CheckDigits
- Assembly
- Bodu.IO.Hashing.dll
- Package
- Bodu.IO.Hashing 1.0.0
- Source
- Isbn10.cs
Computes the check character of a 10-character International Standard Book Number using the ISBN-10 weighted modulus-11 algorithm. This class cannot be inherited.
public sealed class Isbn10 : AlphanumericCheckDigitAlgorithm
- Inheritance
-
Isbn10
- Inherited Members
- Extension Methods
Examples
// Two contrasting bodies - the second exercises the 'X' sentinel.
char check = Isbn10.Compute("030640615"); // '2'
char checkX = Isbn10.Compute("043942089"); // 'X' (sentinel for value ten)
// Full-sequence validation.
bool ok = Isbn10.IsValid("043942089X"); // true
// Streaming use when the body is built up incrementally.
var algo = new Isbn10();
algo.Append("030640615");
char d = algo.GetCurrentCheckDigit(); // '2'
Remarks
ISBN-10, in use prior to the global migration to ISBN-13 in 2007, applies weights 10, 9, 8, 7, 6, 5, 4, 3, 2
to the nine body digits from left to right. The weighted sum is taken modulo eleven; the check character is chosen
so that the complete weighted sum over ten positions (including the check at weight one) is a multiple of eleven.
When the required check value is ten, the sentinel 'X' is emitted.
Worked examples. For the body "030640615", the computed check character is '2' (
0306406152). For the body "043942089", the computed check character is 'X' (043942089X),
demonstrating the 'X'-for-ten sentinel.
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
Isbn10()
Initializes a new instance of the Isbn10 class.
public Isbn10()
Fields
BodyLength
The required body length of 9 decimal digits.
public const int BodyLength = 9
Field Value
SequenceLength
The required full-sequence length of 10 characters.
public const int SequenceLength = 10
Field Value
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 ISBN-10 check character for the supplied body of decimal digits without allocating a streaming instance.
public static char Compute(ReadOnlySpan<char> digits)
Parameters
digitsReadOnlySpan<char>The body characters. Each must be an ASCII decimal digit (
'0'to'9').
Returns
- char
The check character as an ASCII digit
'0'to'9', or the sentinel'X'for the value ten.
Remarks
This helper is length-tolerant to support streaming and partial-body use; IsValid(ReadOnlySpan<char>) enforces the strict SequenceLength domain contract for full validation.
Exceptions
- ArgumentOutOfRangeException
Thrown when
digitscontains any character outside the range'0'to'9'.
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 nine-digit body followed by a trailing ISBN-10 check character, is consistent.
public static bool IsValid(ReadOnlySpan<char> valueIncludingCheck)
Parameters
valueIncludingCheckReadOnlySpan<char>The complete ten-character sequence.
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 |