Table of Contents

Luhn Class

Definition

Namespace
Bodu.IO.Hashing.CheckDigits
Assembly
Bodu.IO.Hashing.dll
Package
Bodu.IO.Hashing 1.0.0
Source
Luhn.cs

Computes the check digit of a decimal string using the Luhn (mod 10) algorithm. This class cannot be inherited.

public sealed class Luhn : CheckDigitAlgorithm
Inheritance
Luhn
Inherited Members
Extension Methods

Examples

// Single-call computation against an in-memory body.
char check = Luhn.Compute("7992739871");   // '3'

// Full-sequence validation.
bool ok = Luhn.IsValid("79927398713");     // true

// Streaming use when the body is built up incrementally.
var algo = new Luhn();
algo.Append("7992739871");
char d = algo.GetCurrentCheckDigit();      // '3'

Remarks

The Luhn algorithm - sometimes called the modulus 10 or mod 10 algorithm - was designed by Hans Peter Luhn at IBM in 1954 and entered the public domain shortly afterwards. It is the check-digit scheme used by most credit card numbers, many national identification numbers, IMEI numbers, and numerous other serial encodings.

Working right-to-left over the sequence, every second digit (starting with the digit immediately to the left of the check digit) is doubled. When doubling produces a two-digit value, the digits are summed - equivalently one subtracts nine. The check digit is the value that makes the total sum divisible by ten.

Luhn catches all single-digit substitution errors and all adjacent-digit transpositions except the transposition 09 ↔ 90, which preserves the digit sum. It does not reliably detect other mutations such as twin errors (aa → bb).

Worked example. For the body "7992739871", the computed check digit is '3', and the resulting sequence "79927398713" is therefore valid under Luhn.

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

Luhn()

Initializes a new instance of the Luhn class.

public Luhn()

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

body ReadOnlySpan<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 body contains any character outside the algorithm's accepted input alphabet.

Compute(ReadOnlySpan<char>)

Computes the Luhn check digit for the supplied body of decimal digits without allocating a streaming instance.

public static char Compute(ReadOnlySpan<char> body)

Parameters

body ReadOnlySpan<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 body contains 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 body followed by a trailing Luhn check digit, is consistent - that is, whether the digit sum evaluates to a multiple of ten.

public static bool IsValid(ReadOnlySpan<char> digitsIncludingCheck)

Parameters

digitsIncludingCheck ReadOnlySpan<char>

The complete sequence including the trailing check digit.

Returns

bool

true if the sequence evaluates as valid under Luhn; otherwise, false - including the case where digitsIncludingCheck is empty or contains a character outside the range '0' to '9'.

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

ProductVersions
.NET8, 10