Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * You lost some of your CC digits ? Let's try
- *
- * TESTED ONLY WITH VISA
- * IT DOES NOT REQUIRE EXPIRATION DATE and CVV
- *
- * This program has been created
- * for assisting you and your memory to recover missing digits of your credit card.
- * It helped me to order a pizza when I left my cc somewhere else.
- *
- * If you forget :
- * - 1 digit you'll get only one result, your Credit Card results.
- * - 2 digit you'll get approximately ~10 Credit Card results.
- * - 3 digit you'll get approximately ~100 Credit Card results.
- * - +4 digit you'll get approximately +1000 Credit Card results.
- *
- * Let's try with your own.
- */
- var card = '4974111111111xx11';
- var results = generateValidCC(card, 'x', 10e5);
- console.log(results);
- /**
- * Returns an array of valid credit card digit.
- * Dynamic placeholder for random digit,
- * and place the x where you want in the CC digit
- *
- * Ex: 497411111xxx1111
- * 49xx111111111111
- * 4xxxx11111111111
- * 411111111111x111
- * 411111xxx1111111
- *
- * @param ccNumber '11111111xxxx1111'
- * @param placeholder 'x'
- * @param maxTry 10e4
- *
- * @returns {string[]}
- */
- function generateValidCC(ccNumber, placeholder = null, maxTry = 10e4) {
- if(!ccNumber || !placeholder) {
- throw new TypeError('No ccNumber or placeholder given');
- }
- const ccArr = [];
- const xAmount = (ccNumber.split(placeholder).length - 1);
- var i = 0;
- if (xAmount <= 0 ) {
- throw new TypeError('No placeholder found in ccNumber');
- }
- while (i < maxTry) {
- const rndCard = ccNumber.replace(placeholder.repeat(xAmount), random(xAmount));
- const cValid = isValid(rndCard);
- if (cValid && ccArr.indexOf(rndCard) === -1) {
- ccArr.push(rndCard);
- }
- i++;
- }
- return ccArr;
- }
- function getRandomIntInclusive(min, max) {
- min = Math.ceil(min);
- max = Math.floor(max);
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- function random(length) {
- var output = '';
- var i = 0;
- while (i < length) {
- output += getRandomIntInclusive(0, 9);
- i++;
- }
- return output;
- }
- /**
- * Luhn Algorithm
- * @see https://en.wikipedia.org/wiki/Luhn_algorithm
- */
- function isValid(ccNumber) {
- var sum = 0;
- var alternate = false;
- var i = ccNumber.length - 1;
- while (i >= 0) {
- var n = parseInt(ccNumber.substring(i, i + 1));
- if (alternate) {
- n *= 2;
- if (n > 9) {
- n = (n % 10) + 1;
- }
- }
- sum += n;
- alternate = !alternate;
- i--;
- }
- return (sum % 10 == 0);
- };
Add Comment
Please, Sign In to add comment