Table of Contents

UpcA Class

Definition

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

Computes the check digit of a 12-digit Universal Product Code (UPC-A) barcode using the UPC-A weighted modulus-10 algorithm. This class cannot be inherited.

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

Examples

// Single-call computation against the 11-digit body.
char check = UpcA.Compute("03600029145");   // '2'

// Full-sequence validation.
bool ok = UpcA.IsValid("036000291452");     // true

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

Remarks

UPC-A shares its weight pattern with EAN-13 and ISBN-13; a UPC-A value is exactly an EAN-13 whose country prefix is a leading zero. The static helpers on this type enforce a strict 11-digit body length (12-digit full sequence); the streaming surface is length-agnostic.

Worked example. For the body "03600029145", the computed check digit is '2', and the resulting UPC-A "036000291452" 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

UpcA()

Initializes a new instance of the UpcA class.

public UpcA()

Fields

BodyLength

The required body length of 11 decimal digits.

public const int BodyLength = 11

Field Value

int

SequenceLength

The required full-sequence length of 12 decimal digits.

public const int SequenceLength = 12

Field Value

int

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 UPC-A 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'.

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 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 an eleven-digit body followed by a trailing UPC-A check digit, is consistent.

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 is exactly SequenceLength digits and evaluates as valid under UPC-A; otherwise, false - including the case where digitsIncludingCheck is empty.

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