Brasilex.Boleto.Checksum.Mod10 (Brasilex v0.3.0)

Copy Markdown View Source

Implements the Modulo 10 check digit algorithm as defined by FEBRABAN.

Used to validate individual fields in the linha digitável (typeable line) of Brazilian boletos.

Algorithm

  1. Starting from right to left, multiply each digit alternately by 2 and 1
  2. If a product >= 10, sum its individual digits (e.g., 12 -> 1 + 2 = 3)
  3. Sum all resulting digits
  4. The check digit is (10 - (sum mod 10)) mod 10

Summary

Functions

Calculates the Modulo 10 check digit for a string of digits.

Validates that a string ends with the correct Mod10 check digit.

Functions

calculate(digits)

@spec calculate(String.t()) :: non_neg_integer()

Calculates the Modulo 10 check digit for a string of digits.

Examples

iex> Brasilex.Boleto.Checksum.Mod10.calculate("341911012")
1

iex> Brasilex.Boleto.Checksum.Mod10.calculate("3456788005")
8

iex> Brasilex.Boleto.Checksum.Mod10.calculate("0000000000")
0

valid?(digits)

@spec valid?(String.t()) :: boolean()

Validates that a string ends with the correct Mod10 check digit.

The last digit of the string is treated as the check digit, and is validated against the calculated check digit of the preceding digits.

Examples

iex> Brasilex.Boleto.Checksum.Mod10.valid?("3419110121")
true

iex> Brasilex.Boleto.Checksum.Mod10.valid?("3419110129")
false

iex> Brasilex.Boleto.Checksum.Mod10.valid?("5")
false