What's a good way to generate a random number for a valid credit card?

Viewed 6028

I am developing a set of tools in Java for validating and working with credit cards. So far I have support for:

  • LUHN validation.

  • Date validation (simple expiration).

  • Card code length validation (CVV, CVC, CID) based on the brand (Visa, MasterCard, etc).

  • Credit card number length validation (based on the brand).

  • BIN/IIN validation (against a database of valid numbers).

  • Hiding the digits (425010 * * * * * * 1234)

To make the tool set a bit more complete, I would like to create a credit card random number generator based on the different card brands. This functionality will (hopefully) make my test cases a bit more reliable.

Basically, I would like to be able to generate numbers which are:

  • LUHN valid

  • Valid based on the brand prefixes

  • Valid based on the BIN/IIN prefix numbers

For BIN/IIN valid card numbers, I am thinking of looking up a random BIN/IIN number from the database (based on the brand of course) and then appending the remaining digits using Random. Obviously, that would not be valid most of the time and I will have to increment one of the digits until it passes the LUHN validation.

I can't seem to be able to think of a better way. Perhaps someone could suggest something a little smarter...?

Looking forward to your suggestions! Thanks in advance! :)

3 Answers

Here goes some Groovy to generate LUHN valid Credit Cards.. I'm sure you can re-write it in your own language:

cardPrepend = "400141"
cardLength = "16"

random = new Random()
myString = ""

    (cardLength.toInteger() - cardPrepend.length()).times{
        randomNumber = random.nextInt(9)
        myString = myString + randomNumber.toString()  
    }

    myString = cardPrepend + myString

    myStringMinusLastDigit = myString.substring(0, myString.length()-1)
    mod10Digit = getMod10Digit(myStringMinusLastDigit)
    myString = myStringMinusLastDigit + mod10Digit

    return  """${myString}"""  


//-------------------------------------------------------------------------
def getMod10Digit(String number) {
    int weight = 2;
    int sum    = 0;
    for (int i = number.length() - 1; i >= 0; i--) {
        int digit = weight * Integer.parseInt(number.substring(i,i+1));
        sum = sum + (digit / 10) + (digit % 10);
        weight = (weight == 2) ? 1: 2;
    }
 
    
    return ((10 - (sum % 10)) % 10);
    }
Related