A Python implementation of the Luhn algorithm for validating credit card numbers.
The Luhn algorithm (also known as the modulus 10 or mod 10 algorithm) is a checksum formula used to validate a variety of identification numbers, most commonly credit card numbers. It was designed to protect against accidental errors, not malicious attacks.
- Validates credit card numbers using the Luhn algorithm
- Handles card numbers with spaces and hyphens
- Simple and clean Python implementation
The Luhn algorithm follows these steps:
- Reverse the card number - Start from the rightmost digit
- Sum odd-positioned digits - Add up every other digit (starting with the rightmost)
- Double even-positioned digits - Multiply every other digit by 2
- Handle double-digit results - If doubling results in a number ≥ 10, add the digits together
- Calculate total - Sum all the processed digits
- Validate - If the total modulo 10 equals 0, the card number is valid