Skip to content

Instantly share code, notes, and snippets.

@julia-mareike
Last active May 3, 2023 10:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julia-mareike/200e34dacef83d1933d57d1caac9ea86 to your computer and use it in GitHub Desktop.
Save julia-mareike/200e34dacef83d1933d57d1caac9ea86 to your computer and use it in GitHub Desktop.
Generate & validate credit cards based on BIN number
// quickly hacked together from this article, thanks friend
// https://medium.com/cyberdoggo/luhn-algorithm-in-javascript-python-clojure-part-1-c4ea3079d0f7
const isEven = number => number % 2 === 0
const calculateEven = even => (even * 2 < 10) ? even * 2 : even * 2 - 9
const generateCheckSum = card => {
const checksum = card.split('')
.map((number, index) => (isEven(index)) ? calculateEven(number) : parseInt(number, 10))
.reduce((previous, current) => previous + current) % 10
return checksum === 0 ? 0 : 10 - checksum
}
const isValidCreditCard = card => generateCheckSum(card) === 0
const generateCreditCard = () => {
const ANZBinNumber = '471540'
const randomNumber = Math.random().toString().slice(2,11)
const partialCreditCardNumber = ANZBinNumber + randomNumber
const checksum = generateCheckSum(partialCreditCardNumber)
return partialCreditCardNumber + checksum
}
expect(generateCheckSum('471540123456789')).toBe(7)
expect(isValidCreditCard('4715401234567897')).toBe(true)
expect(isValidCreditCard('4715401234567891')).toBe(false)
expect(isValidCreditCard(generateCreditCard())).toBe(true)
// what does it mean??
// a checksum is the final digit in a credit card, and is calculated using the rest of the card number
// this is what is used to _validate_ a credit card number
// Every second digit of the card number needs to be doubled...
// if the result is 10 or more, subtract 9
// so that the result is only ever 1 digit long
// then, add all those resulting numbers together
// if that result is divisible by 10 with no remainder, it's a valid credit card...
// if it's not a valid credit card, you will get the remainder digit (ie 3)
// to calculate the checksum, subtract the remainder from 10 ( 10 - 3 )
// and you will get your checksum, 7
// add this to the end of your invalid (partial) credit card number :D
@web-apply
Copy link

web-apply commented Apr 29, 2021

The credit card number consists of a sequence of 16 digits and is located at the bottom of the front of your card. This number alone may not mean anything to you, but when you break it up, you see that the credit card number actually has a more meaningful structure than it seems.
A similar list for generator tools;

The first number in the credit card generator number tells which payment system the card uses. That is, it shows whether your card uses Visa or Mastercard (we mentioned the differences between Visa and Mastercard in a previous article). Credit card number starts with 4 for cards using Visa, and credit card number starts with 5 for cards using Mastercard.

Including the first digit of the credit card number, the first 6 digits, called prefix, indicate which bank issued the card. This number is the Bank Identification Number (BIN), the number that identifies the bank that issued the card.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment