Table of Contents

Cusip Class

Definition

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

Computes the check digit of a 9-character Committee on Uniform Securities Identification Procedures (CUSIP) identifier using the Luhn-style weighted modulus-10 scheme specified by the American Bankers Association. This class cannot be inherited.

public sealed class Cusip : AlphanumericCheckDigitAlgorithm
Inheritance
Cusip
Inherited Members
Extension Methods

Examples

// Apple Inc. - CUSIP body "03783310".
char check = Cusip.Compute("03783310");   // '0'

// Full-sequence validation.
bool ok = Cusip.IsValid("037833100");     // true

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

Remarks

CUSIP body characters are drawn from the decimal digits ('0'-'9'), uppercase Latin letters ('A'-'Z', with values 10-35), and the historical sentinels '*'=36, '@'=37 and '#'=38. The body is eight characters; the ninth character is the check digit.

Each body character's numeric value is multiplied by a positional weight (1 at even left-to-right indices, 2 at odd indices). The resulting product is split into its tens and units digits, which are summed before being added to the running total. The check digit is chosen so that the total is a multiple of ten.

Worked example. For the body "03783310" - Apple Inc. - the computed check digit is '0', and the resulting CUSIP "037833100" 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

Cusip()

Initializes a new instance of the Cusip class.

public Cusip()

Fields

BodyLength

The required body length of 8 characters.

public const int BodyLength = 8

Field Value

int

SequenceLength

The required full-sequence length of 9 characters.

public const int SequenceLength = 9

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".

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

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 CUSIP check digit for the supplied body without allocating a streaming instance.

public static char Compute(ReadOnlySpan<char> body)

Parameters

body ReadOnlySpan<char>

The body characters. Each must be a valid CUSIP character.

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 CUSIP 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 an eight-character body followed by a single-digit CUSIP check, is consistent.

public static bool IsValid(ReadOnlySpan<char> valueIncludingCheck)

Parameters

valueIncludingCheck ReadOnlySpan<char>

The complete nine-character CUSIP.

Returns

bool

true if the sequence evaluates as valid under CUSIP; otherwise, false - including the case where valueIncludingCheck is empty, the length is wrong, any body character is outside the CUSIP alphabet, or the check character is not a decimal digit.

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