How To Generate A Valid Credit Card Number For A Bin (First 6 Digits)
source link: https://blog.jakubholy.net/2014/03/25/how-to-generate-a-valid-credit-card-number-for-a-bin-first-6-digits/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How To Generate A Valid Credit Card Number For A Bin (First 6 Digits)
org.apache.commons.validator.routines.CreditCardValidator
from common-validator 1.4:
// Groovy:
/** Map RegExp from C.C.Validator to the total length of the CC# */
binReToLen = [
(~/^(3[47]\d{0,13})$/) : 13+2, // amex
(~/^30[0-5]\d{0,11}$/) : 11+3, // diners 1
(~/^(3095\d{0,10})$/) : 10+4, // diners 2
(~/^(36\d{0,12})$/) : 12+2, // diners 3
(~/^|3[8-9]\d{0,12}$/) : 12+2, // diners 4
(~/^(5[1-5]\d{0,14})$/) : 14+2, // master
(~/^(4)(\d{0,12}|\d{15})$/) : 12+1 // visa
// Discover cards omitted
]
/** Bin is e.g. 123456 */
def completeCCn(String bin) {
def ccnFill = "1" * 19
int ccnLen = lenForBin(bin)
def ccnWithoutCheck = bin + ccnFill[0..<(ccnLen - 6 - 1)] // - bin, - check digit
def check = computeLuhncheckDigit(ccnWithoutCheck)
return "$ccnWithoutCheck$check"
}
def lenForBin(String bin) {
def match = binReToLen.find { it.key.matcher(bin).matches() }
if (match == null) {
throw new RuntimeException("Bin $bin does not match any known CC issuer")
}
match.value
}
def computeLuhncheckDigit(def ccnWithoutCheck) {
org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit.LUHN_CHECK_DIGIT.calculate(ccnWithoutCheck)
}
completeCCn('465944') // => 4659441111118
Testing FTW!
Are you benefitting from my writing? Consider buying me a coffee or supporting my work via GitHub Sponsors. Thank you! You can also book me for a mentoring / pair-programming session via Codementor or (cheaper) email.
Allow me to write to you!
Let's get in touch! I will occasionally send you a short email with a few links to interesting stuff I found and with summaries of my new blog posts. Max 1-2 emails per month. I read and answer to all replies.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK