A Python implementation of the Luhn Algorithm for validating credit card numbers and other identification numbers.
The Luhn Algorithm is a simple checksum formula used to validate a variety of identification numbers, especially credit card numbers. This implementation helps developers verify the validity of such numbers in their applications, providing a foundation for understanding basic cryptographic validation techniques.
- Validate credit card numbers using the Luhn checksum
- Handle numbers with hyphens and spaces
- Simple command-line execution
- Easy integration into Python projects
- Clear validation feedback
-
Clone the repository:
git clone https://github.com/fahadelahikhan/Luhn-Algorithm.git cd Luhn-Algorithm -
Run the application:
python Luhn-Algorithm.py
# Import the validation function
from luhn import verify_card_number
# Test a valid card number
card_number = '4111-1111-4555-1141'
is_valid = verify_card_number(card_number)
print(f'Card is {"VALID" if is_valid else "INVALID"}') # Output: VALID
# Test an invalid card number
invalid_card = '1234-5678-9012-3456'
is_valid = verify_card_number(invalid_card)
print(f'Card is {"VALID" if is_valid else "INVALID"}') # Output: INVALID# Validate a Discover card number
discover_card = '6011-1111-1111-1117'
validation_result = verify_card_number(discover_card)
print(validation_result) # Output: True
# Validate an American Express number
amex_card = '3782-822463-10005'
validation_result = verify_card_number(amex_card)
print(validation_result) # Output: TrueThe Luhn Algorithm works by:
- Reversing the input number
- Doubling every second digit
- Summing the digits of these doubled values (if >=10, sum the individual digits)
- Summing all the undoubled digits
- Checking if the total modulo 10 is equal to 0
Distributed under the MIT License. See LICENSE for details.
Note: This implementation is for educational purposes and basic validation. For production use, consider additional security measures and validation checks beyond the Luhn Algorithm.