Git Product home page Git Product logo

luhn-checksum's Introduction

luhn-checksum

A simple function that calculates the Luhn checksum for a given number

To use the program online, input your number at https://www.jdoodle.com/embed/v0/5iAC?arg=0 to get the checksum.

The checksum function uses the Luhn's algorithm created by Hans Peter Luhn, a German computer scientist at IBM, to calculate the checksum as follows:

Step 1 - Double the value of every other digit from right to left (for example, see https://en.wikipedia.org/wiki/Luhn_algorithm#Example_for_computing_check_digit)

Step 2 - If any result of Step 1 has two digits (e.g., 18 because of the digit is 9*2), then add the two digits to get the final result (i.e., 1+8 = 9)

Step 3 - Get the sum by adding all results of the previous step to the remaining digits in the given number

Step 4 - The checksum is (10 - (sum mod 10)) mod 10 where the n mod 10 operation gives the remainder after the number n is divided by 10 (i.e., the value of checksum must be a digit between 0 and 9)

The Luhn's algorithm was designed to detect mistyped numbers, and one of its applications is to check whether a credit card number is valid before triggering the payment process with further verification (see https://www.ibm.com/docs/en/order-management-sw/9.3.0?topic=cpms-handling-credit-cards).

Note: A correct checksum means the given number is valid, but it doesn't mean an account (e.g., bank account) or an identity (e.g., ID card) associated with the given number exists.

To use the checksum function, simply pass a number without the check digit to the function (e.g., checksum("7992739871") for the example from the above mentioned Wikipedia article, or checksum("462474823324978") for the example of a Visa credit card number 4624 7482 3324 9780 from the above mentioned IBM documentation), and the function will return the check digit of the given number. The check digit usually is the last digit of the card number or the number in brackets.

Two more credit card number examples from https://www.ibm.com/docs/en/sga?topic=patterns-credit-card-number:

checksum("546046280340313") returning 2 for a MasterCard credit card of number 5460 4628 0340 3132

checksum("492990365278615") returning 8 for a Visa credit card of number 4929 9036 5278 6158

By default, the function will double the value of every other digit from right to left. In case there is a need to double the value of every other digit from left to right, set double_position="odd" (e.g., checksum("7992739871",double_position="odd")) because doubling from left means doubling the odd position (i.e., 1st, 3rd, 5th, etc.) digits. Use double_position="odd"and double_position="even" to directly select doubling the odd position digits and doubling the even position digits respectively.

References:

https://en.wikipedia.org/wiki/Luhn_algorithm

https://www.ibm.com/docs/en/order-management-sw/9.3.0?topic=cpms-handling-credit-cards

https://www.ibm.com/docs/en/sga?topic=patterns-credit-card-number

luhn-checksum's People

Contributors

cocolaberica avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.