-
-
Save chew-z/0e8dad328d985708996a to your computer and use it in GitHub Desktop.
generates random Visa card number passing Luhn check (in Pythonista)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
# generates random Visa card number passing Luhn check | |
import clipboard | |
import numpy as np | |
from decimal import Decimal | |
def split_cc(ccnum): | |
cclist = list(str(ccnum)) | |
cclist.insert(4, ' ') | |
cclist.insert(9, ' ') | |
cclist.insert(14, ' ') | |
return ''.join(cclist) | |
def luhn(ccnum): # works with leading zeros | |
# checks if ccnum satisfies Luhn algoritm [doesn't matter how long ccnum is] | |
ccdec = Decimal(ccnum).as_tuple().digits # converte ccnum to tuple of digits | |
# followoing if statement is used only for partial numbers with leading zeros | |
if len(ccdec) > 4 and len(ccdec) <> 16: | |
return False | |
if len(ccdec) == 3: | |
ccdec = tuple([0]+list(ccdec)) | |
elif len(ccdec) == 2: | |
ccdec = tuple([0, 0]+list(ccdec)) | |
elif len(ccdec) == 1: | |
ccdec = tuple([0, 0, 0]+list(ccdec)) | |
elif len(ccdec) == 0: | |
ccdec = tuple([0, 0, 0, 0]) | |
# now use Luhn algoritm | |
cint = 0 | |
i = 0 | |
for c in ccdec: | |
if i%2 == 0: | |
t = c*2 | |
if t > 9: #if two digit number | |
t = t%10 + 1 #same as above but simpler; max = 9*2 = 18 | |
else: | |
t = c | |
cint += t * 10**i | |
i += 1 | |
k = sum(Decimal(cint).as_tuple().digits) # sum decimal digits | |
return k%10 == 0 | |
i = 0 | |
cc = "" #credit card number string | |
while i < 4: #generate 4 groups of 4 digits | |
# this 4x4 approach is quicker | |
# if each group satisfies the Luhn condition then the whole number (usually) will... | |
# anyway we will run luhn() check on whole 16 digits at the end | |
q = 1111 # start loop with False condition | |
while luhn(q) <> True: #loop until you find 4 digits that satisfy Luhn | |
if i > 0: | |
q = np.random.randint(9999) | |
c = str(q) | |
while len(c) < 4: #if less then 4 digits add leading zeros to string representation | |
c = str(0) + c | |
q = int(c) | |
else: # leading 4 digits | |
q = np.random.randint(999) | |
c = str(q) | |
while len(c) < 3: #if less then 3 digits add leading zeros to string representation | |
c = str(0) + c | |
c = str(4) + c #and start visa number with 4 | |
q = int(c) | |
cc = cc + c | |
i += 1 # done. go and find next group | |
print "You new Visa number is: \n" + split_cc(long(cc)) | |
clipboard.set(cc) | |
print "Credit card number copied to Clipboard" | |
print cc[:6] + 'X' * 6 + cc[-4:] | |
print "Visa check result is: " + str( luhn(long(cc)) ) #check if the the whole number is valid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment