Isbn13 Class
Definition
- Namespace
- Bodu.IO.Hashing.CheckDigits
- Assembly
- Bodu.IO.Hashing.dll
- Package
- Bodu.IO.Hashing 1.0.0
- Source
- Isbn13.cs
Computes the check digit of a 13-digit International Standard Book Number using the ISBN-13 weighted modulus-10 algorithm. This class cannot be inherited.
public sealed class Isbn13 : CheckDigitAlgorithm
- Inheritance
-
Isbn13
- Inherited Members
- Extension Methods
Examples
// Single-call computation against the 12-digit body.
char check = Isbn13.Compute("978030640615"); // '7'
// Full-sequence validation.
bool ok = Isbn13.IsValid("9780306406157"); // true
// Streaming use when the body is built up incrementally.
var algo = new Isbn13();
algo.Append("978030640615");
char d = algo.GetCurrentCheckDigit(); // '7'
Remarks
The ISBN-13 check scheme, introduced in 2007 by the International ISBN Agency, alternates weights of 1 and 3 across the twelve body digits - the rightmost data digit receives weight 3 - and the check digit is chosen so that the resulting weighted sum is a multiple of ten.
The same algorithm underpins the EAN-13, UPC-A, GTIN-8, and GTIN-14 barcode families - see Ean13 and its siblings for strict-length variants.
Worked example. For the body "978030640615", the computed check digit is '7', and the resulting
ISBN-13 "9780306406157" 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
Isbn13()
Initializes a new instance of the Isbn13 class.
public Isbn13()
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".
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-13 check digit for the supplied body of decimal digits without allocating a streaming instance.
public static char Compute(ReadOnlySpan<char> body)
Parameters
bodyReadOnlySpan<char>The body characters. Each must be an ASCII decimal digit (
'0'to'9').
Returns
- char
The check digit as an ASCII character in the range
'0'to'9'.
Exceptions
- ArgumentOutOfRangeException
Thrown when
bodycontains any character outside the range'0'to'9'.
GetCurrentCheckDigit()
Returns the check digit computed for the body absorbed since the last Reset() (or since construction).
public override 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.
IsValid(ReadOnlySpan<char>)
Determines whether the supplied sequence, comprising a twelve-digit body followed by a trailing ISBN-13 check digit, is consistent - that is, whether the weighted sum evaluates to a multiple of ten.
public static bool IsValid(ReadOnlySpan<char> digitsIncludingCheck)
Parameters
digitsIncludingCheckReadOnlySpan<char>The complete sequence including the trailing check digit.
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 |