Skip to content

Instantly share code, notes, and snippets.

@deathslocus
Forked from B-Con/Credit Card Generator
Last active August 29, 2015 14:11
Show Gist options
  • Save deathslocus/0482aecadc29b27070bb to your computer and use it in GitHub Desktop.
Save deathslocus/0482aecadc29b27070bb to your computer and use it in GitHub Desktop.
//
// Random Credit Card Number Generator
//
// By: Brad Conte (http://bradconte.com)
//
function cc_gen() {
var pos = 12;
var str = new Array(7,8,2,4,6,7,0,2,0,0,0,0,5,4,5,3,3,6,1);
var sum = 0;
var final_digit = 0;
var t = 0;
var len_offset = 0;
var len = 19;
var issuer;
//
// Fill all the remaining numbers except for the last one with random values.
//
while (pos < len - 1) {
str[pos++] = Math.floor(Math.random() * 10) % 10;
}
//
// Calculate the Luhn checksum of the values thus far.
//
len_offset = (len + 1) % 2;
for (pos = 0; pos < len - 1; pos++) {
if ((pos + len_offset) % 2) {
t = str[pos] * 2;
if (t > 9) {
t -= 9;
}
sum += t;
}
else {
sum += str[pos];
}
}
//
// Choose the last digit so that it causes the entire string to pass the checksum.
//
final_digit = (10 - (sum % 10)) % 10;
str[len - 1] = final_digit;
// Output the CC value.
t = str.join('');
t = t.substr(0, len);
console.log(t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment