Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 0 ๐Ÿ“– 5 min read

Routing number and account number checksums: which bank numbers have a check digit, and how to validate them

A US routing number (ABA) has a check digit. Multiply its nine digits by 3, 7, 1, 3, 7, 1, 3, 7, 1, add the products, and the total must be a multiple of 10. A US bank account number has no standard check digit, so the o

Routing number and account number checksums: which bank numbers have a check digit, and how to validate them

A US routing number (ABA) has a check digit. Multiply its nine digits by 3, 7, 1, 3, 7, 1, 3, 7, 1, add the products, and the total must be a multiple of 10. A US bank account number has no standard check digit, so the only way to check one is to ask the bank, for example with a micro-deposit or an account-verification API. The same split shows up worldwide. IBANs carry two check digits (mod 97), and so does Mexico's 18-digit CLABE (3-7-1 again). Most other domestic account numbers either have no public checksum, or rely on bank-specific rules, like UK modulus checking. I tested the 3-7-1 check on 2,000 random valid routing numbers. It caught every single wrong digit (162,000 of 162,000) but only 88.7% of adjacent swaps. It misses any swap of two digits that are 5 apart, like 0 and 5.

The routing number 021000021 multiplied by the weights 3, 7, 1 repeating gives products 0, 14, 1, 0, 0, 0, 0, 14, 1, which add up to 30, a multiple of 10, so it is valid. Tested on 2,000 random valid numbers, every single wrong digit is caught and 88.7% of adjacent swaps.

The whole check fits on one line: weights 3, 7, 1, and the total must end in 0.

Which bank numbers have a check digit?

Number Where Length Check What a pass means
ABA routing number United States 9 digits weights 3-7-1, total mod 10 the bank identifier was typed correctly
Bank account number United States varies none standard nothing: only the bank can confirm it
IBAN 80+ countries 15โ€“34 ISO 7064 mod 97-10 country, bank and account typed correctly
CLABE Mexico 18 digits weights 3-7-1, total mod 10 bank, branch and account typed correctly
Sort code + account number United Kingdom 6 + 8 modulus checking, rules per sort code range the pair is plausible for that bank
IFSC India 11 chars none (the fifth character is always 0) nothing beyond the format
Card number (PAN) everywhere 12โ€“19 Luhn the card number was typed correctly

The pattern: an identifier that many systems share, like a bank code, a cross-border account or a card, gets a checksum. A purely domestic account number usually doesn't. In Pakistan, for example, the account number inside the IBAN has no published checksum, but the 24-character PK IBAN around it does. So validate the IBAN, not the bare account number. Luhn and IBAN check digits covers those two algorithms in detail.

How do you validate a routing number in Python?

routing.py

def aba_ok(rtn: str) -> bool:
    """US ABA routing number: 9 digits, weights 3,7,1 repeating, total a multiple of 10."""
    if len(rtn) != 9 or not rtn.isdigit():
        return False
    return sum(int(d) * w for d, w in zip(rtn, [3, 7, 1] * 3)) % 10 == 0

def aba_check_digit(first8: str) -> int:
    """The ninth digit that makes the first eight valid."""
    s = sum(int(d) * w for d, w in zip(first8, [3, 7, 1, 3, 7, 1, 3, 7]))
    return (10 - s % 10) % 10

assert aba_ok("021000021") and aba_ok("011000015")
assert aba_check_digit("02100002") == 1
assert not aba_ok("021000012")  # last two digits swapped

The four real routing numbers I tried (021000021, 011000015, 026009593 and 111000025) all pass. Strip spaces and dashes before checking. Don't strip leading zeros: routing numbers often start with 0, and storing one as an integer silently turns 021000021 into an 8-digit number that fails.

The first two digits also mean something. 01 to 12 is the Federal Reserve district of the bank, and thrift institutions use the district plus 20 (21 to 32). A number with a valid check digit but a prefix like 50 is still suspicious, though a prefix check doesn't make it real either.

What does the 3-7-1 check miss?

A check digit is only worth what it catches. I generated 2,000 random valid routing numbers and tried every possible single-digit change and every swap of two neighbouring digits:

detection.py

import random

def make_valid() -> str:
    first8 = "".join(random.choice("0123456789") for _ in range(8))
    return first8 + str(aba_check_digit(first8))

random.seed(1)
single = caught = swaps = swaps_caught = 0
for _ in range(2000):
    n = make_valid()
    for i in range(9):
        for d in "0123456789":
            if d != n[i]:
                single += 1
                caught += not aba_ok(n[:i] + d + n[i + 1:])
    for i in range(8):
        if n[i] != n[i + 1]:
            swaps += 1
            swaps_caught += not aba_ok(n[:i] + n[i + 1] + n[i] + n[i + 2:])
# single-digit errors caught: 162000 of 162000 (100%)
# adjacent swaps caught:      12750 of 14372 (88.7%)

Every single wrong digit is caught, because 3, 7 and 1 are all coprime with 10. Changing one digit always changes the total's last digit. Swaps of digits 5 apart are not. Swapping neighbours changes the total by (a โˆ’ b) ร— (difference of their weights). The weight differences are 4, 6 and 2, all even, so when a โˆ’ b is 5 the change is a multiple of 10 and the check can't see it. That's 10 of the 90 possible ordered digit pairs, 0โ†”5, 1โ†”6, 2โ†”7, 3โ†”8 and 4โ†”9, at every position.

Passports have the same weakness. The MRZ check digit uses weights 7, 3, 1, and the same calculation shows it misses the same 10 pairs. Luhn misses only the 09โ†”90 swap, and IBAN's mod 97 catches every single-character error and every adjacent swap.

How do you validate a Mexican CLABE?

A CLABE is 18 digits: a 3-digit bank code, a 3-digit branch (plaza) code, an 11-digit account number and a check digit. The check is the same 3-7-1 sum. Weight the first 17 digits 3, 7, 1 repeating, and the check digit is whatever brings the total to a multiple of 10:

clabe.py

def clabe_ok(clabe: str) -> bool:
    if len(clabe) != 18 or not clabe.isdigit():
        return False
    s = sum(int(d) * w for d, w in zip(clabe[:17], [3, 7, 1] * 6))
    return (10 - s % 10) % 10 == int(clabe[17])

assert clabe_ok("002010077777777771")
assert clabe_ok("032180000118359719")
assert not clabe_ok("002010077777777772")

Because the 18th position falls on weight 1, this is identical to "the 3-7-1 total of all 18 digits is a multiple of 10". So the routing-number function works with a different length, and it has the same blind spot for swaps of digits 5 apart.

Does a valid check digit mean the account is real?

No. A check digit proves the number was typed consistently, nothing more. Roughly 1 in 10 random 9-digit strings passes the routing check, and a CLABE can pass for an account that was closed years ago. To know an account exists and belongs to the person, you need the bank: a micro-deposit, an open-banking account check, or a name-match service where the country has one. The check digit's job is to reject typos before you spend a verification call or a failed payment on them. That's the same role as in CNIC validation, where there isn't even a check digit to lean on.

To try any of these without code, the IBAN, routing number and card checker runs the same checks in your browser. It accepts a 9-digit routing number, an 18-digit CLABE, an IBAN or a card number.

Summary

  • US routing numbers have a 3-7-1 check digit. US account numbers have none, so only the bank can confirm them.
  • IBANs (mod 97) and CLABEs (3-7-1) carry check digits. Most domestic account numbers don't, and the UK uses bank-specific modulus rules.
  • The 3-7-1 check catches every single wrong digit but misses adjacent swaps of digits 5 apart: 88.7% of swaps caught in my test. MRZ's 7-3-1 has the same gap.
  • Keep routing numbers as strings: a leading zero matters.
  • A passing check digit means "no typo detected", not "this account exists".

More on validating identity and payment data: KYC and identity verification engineering.

Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore ยท Original article

๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.