04 June 2012

The checksum number on a credit card


The last digit on a credit card number is the "checksum" number, which uses the above algorithm to validate the card.  The concept is discussed at Wikipedia:
A checksum or hash sum is a fixed-size datum computed from an arbitrary block of digital data for the purpose of detecting accidental errors that may have been introduced during its transmission or storage. The integrity of the data can be checked at any later time by recomputing the checksum and comparing it with the stored one. If the checksums match, the data was almost certainly not altered.
Image cropped from a longer infographic about credit card numbers at Fishki.

5 comments:

  1. yes and no. More detail on the Luhn algorithm from wikipedia:
    The Luhn algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09 to 90 (or vice versa). It will detect 7 of the 10 possible twin errors (it will not detect 22 ↔ 55, 33 ↔ 66 or 44 ↔ 77).

    Still pretty good though.

    ReplyDelete
  2. Another way of saying this is simply that the last digit of the sum is zero. Thus to generate a card number one takes any set of numbers, sets the last one to zero. performs this operation and takes the last digit. subtract this number from 10, and that's the new last digit in the card.

    What this means is that 1 in 9 randomly chosen card numbers passes this test.

    ReplyDelete
    Replies
    1. actually I take that last statement back. It is possible that the probability of the checksum is not uniformly distributed due to that summing of 2 digit numbers. so it's not 1 in 9.

      Delete
  3. You will also find that your bank's ABA (aka Transit Routing Number) has a check digit (the 9th digit is the check digit). Also, many banks also put a check digit in their account numbers too.

    The above fact isn't quit correct. Yes, hashes are used to detect errors. But in this case, and the case of the ABA and account numbers, the check digit is mostly to check for human error.

    ReplyDelete
  4. Here is a python expression to calculate the checksum:
    $ python
    >>> c = "4417123456789113"
    >>> sum((int(s) if (i % 2 == 1) else sum(int(g) for g in str(2* int(s)))) for i,s in enumerate(c))
    70

    ReplyDelete