Saturday, 2 November 2013

Find valid credit card number by mod-10 algorithm in python



Mod-10 algorithm is for find true credit card number  just like as maestro card, master card and American, visa card etc..

Mod-10 algorithm is given below in python. Easily you can use mod-10 algorithm in your script.
Import os
def cardLuhnChecksumIsValid(card_number):
    """ checks to make sure that the card passes a luhn mod-10 checksum """
    sum = 0
    num_digits = len(card_number)
    oddeven = num_digits & 1

    for count in range(0, num_digits):
        digit = int(card_number[count])
        if not (( count & 1 ) ^ oddeven ):
            digit = digit * 2
        if digit > 9:
            digit = digit - 9

        sum = sum + digit
    return ( (sum % 10) == 0 )
x = cardLuhnChecksumIsValid('5400974053274081')
if x:
 print “this card is valid!”
else:
 print “this card is invalid!”
 
Find valid CAS number 

def checkCAS(cas):
    '''Check if a string is a valid CAS number.

    Arguments:
    cas -- string to check
    '''
    nums = cas[::-1].replace('-', '') # all digits in reverse order
    checksum = int(nums[0]) # first digit is the checksum
    som = 0
    # Checksum method from: http://nl.wikipedia.org/wiki/CAS-nummer
    for n, d in enumerate(nums[1:]):
        som += (n+1)*int(d)
    return som % 10 == checksum

x= checkCAS(‘number’)
if x:
 print “this card is valid!”
else:
 print “this card is invalid!”

No comments:

Post a Comment