introduce

The purpose of this article is to explain how to write a simple credit card validator in Python. The algorithm used to verify the card number is called the Luhn algorithm

About Luhn Algorithm

The Luhn algorithm was developed in 1954 by German computer scientist Hans Peter Luhn. The algorithm, also known as the "modulo 10 algorithm," is a checksum formula used to verify various identification numbers, including credit card numbers.

Most credit cards and many government identification numbers use this algorithm as an easy way to distinguish a valid number from a mistyped or otherwise incorrect number. It is not designed to be a cryptographically secure hash function; rather, it was created to detect accidental errors, not to defend against malicious attacks

How the algorithm works

  1. The Luhn algorithm starts with the last bit called the check digit. Then move left from that check digit ( ← ), doubling the value of each digit at the even index
  2. If the result of this doubling operation is greater than 9 (for example, 6 × 2 = 12), then subtract 9 from the result (for example, 12: 12 - 9 = 3), or equivalently add the number of the result (for example, 12: 1 + 2 = 3 )
  3. Now sum all numbers (including check digit)
  4. If the total is divisible by 10, the number is valid; otherwise it is invalid

Implementing Luhn's Algorithm in Python

The solution below will take a string parameter called " credit_number " which represents the credit card number that will be verified. The pseudocode below will help explain the steps taken by each line of code

  1. Change string to list data type
  2. Delete the last digit (check digit)
  3. Invert the remaining numbers
  4. Even exponent with two digits
  5. Subtract 9 if it exceeds 9
  6. Add the check digit back to the list
  7. Add all the numbers
  8. If the sum is divisible by 10, then it is valid; otherwise, it is invalid
def validate_credit_card(card_number: str) -> bool:
    """This function validates a credit card number."""
    # 1. Change datatype to list[int]
    card_number = [int(num) for num in card_number]

    # 2. Remove the last digit:
    checkDigit = card_number.pop(-1)

    # 3. Reverse the remaining digits:
    card_number.reverse()

    # 4. Double digits at even indices
    card_number = [num * 2 if idx % 2 == 0
                   else num for idx, num in enumerate(card_number)]

    # 5. Subtract 9 at even indices if digit is over 9
    # (or you can add the digits)
    card_number = [num - 9 if idx % 2 == 0 and num > 9
                   else num for idx, num in enumerate(card_number)]

    # 6. Add the checkDigit back to the list:
    card_number.append(checkDigit)

    # 7. Sum all digits:
    checkSum = sum(card_number)

    # 8. If checkSum is divisible by 10, it is valid.
    return checkSum % 10 == 0


if __name__ == '__main__':
    #American Express
    print(validate_credit_card('378282246310005')) # True
    print(validate_credit_card('371449635398431')) # True
    # American Express Corporate
    print(validate_credit_card('378734493671000')) # True
    # Australian BankCard
    print(validate_credit_card('5610591081018250')) # True
    # Diners Club
    print(validate_credit_card('30569309025904')) # True
    print(validate_credit_card('38520000023237')) # True
    #Discover
    print(validate_credit_card('6011111111111117')) # True
    print(validate_credit_card('6011000990139424')) # True
    #MasterCard
    print(validate_credit_card('5555555555554444')) # True
    print(validate_credit_card('5105105105105100')) # True
    #Visa
    print(validate_credit_card('4111111111111111')) # True
    print(validate_credit_card('4012888888881881')) # True

    # Invalid Credit Card Number
    print(validate_credit_card('7762888103111881')) # False
    print(validate_credit_card('37612555227841800')) # False
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部