package com.santhosh.util.handlers.cardgenerator;
import java.util.Random;
/**
* Generates the check digit required to make the given card number
* valid (i.e. pass the Luhn check)
*/
public class CardNumberGenerator {
private Random random = new Random(System.currentTimeMillis());
/**
*
* @param bin
* The bank identification number, a set digits at the start of the card
* number, used to identify the bank that is issuing the card.
* @param length
* The total length (i.e. including the BIN) of the card number.
* @return
* A randomly generated, valid, card number.
*/
public String generate(String bin, int length) {
// The number of random digits that we need to generate is equal to the
// total length of the card number minus the start digits given by the
// user, minus the check digit at the end.
int randomNumberLength = length - (bin.length() + 1);
StringBuilder builder = new StringBuilder(bin);
for (int i = 0; i < randomNumberLength; i++) {
int digit = this.random.nextInt(10);
builder.append(digit);
}
// Do the Luhn algorithm to generate the check digit.
int checkDigit = this.getCheckDigit(builder.toString());
builder.append(checkDigit);
CardValidator cardValidator =new CardValidator();
if(cardValidator.isCardValid(builder.toString())){
return builder.toString();
}else{
generate(bin, randomNumberLength);
return builder.toString();
}
}
/**
* Generates the check digit required to make the given card number
* valid (i.e. pass the Luhn check)
*
* @param number
* The card number for which to generate the check digit.
* @return The check digit required to make the given card number
* valid.
*/
private int getCheckDigit(String number) {
// Get the sum of all the digits, however we need to replace the value
// of the first digit, and every other digit, with the same digit
// multiplied by 2. If this multiplication yields a number greater
// than 9, then add the two digits together to get a single digit
// number.
//
// The digits we need to replace will be those in an even position for
// card numbers whose length is an even number, or those is an odd
// position for card numbers whose length is an odd number. This is
// because the Luhn algorithm reverses the card number, and doubles
// every other number starting from the second number from the last
// position.
int sum = 0;
for (int i = 0; i < number.length(); i++) {
// Get the digit at the current position.
int digit = Integer.parseInt(number.substring(i, (i + 1)));
if ((i % 2) == 0) {
digit = digit * 2;
if (digit > 9) {
digit = (digit / 10) + (digit % 10);
}
}
sum += digit;
}
// The check digit is the number required to make the sum a multiple of
// 10.
int mod = sum % 10;
return ((mod == 0) ? 0 : 10 - mod);
}
}
No comments:
Post a Comment