xDisfigure

Retrieve missing credit cards digits - BRUTE FORCE

Feb 28th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * You lost some of your CC digits ? Let's try
  3.  *
  4.  * TESTED ONLY WITH VISA
  5.  * IT DOES NOT REQUIRE EXPIRATION DATE and CVV
  6.  *
  7.  * This program has been created
  8.  * for assisting you and your memory to recover missing digits of your credit card.
  9.  * It helped me to order a pizza when I left my cc somewhere else.
  10.  *
  11.  * If you forget :
  12.  * - 1 digit you'll get only one result, your Credit Card results.
  13.  * - 2 digit you'll get approximately ~10 Credit Card results.
  14.  * - 3 digit you'll get approximately ~100 Credit Card results.
  15.  * - +4 digit you'll get approximately +1000 Credit Card results.
  16.  *
  17.  * Let's try with your own.
  18.  */
  19. var card = '4974111111111xx11';
  20. var results = generateValidCC(card, 'x', 10e5);
  21.  
  22. console.log(results);
  23.  
  24. /**
  25.  * Returns an array of valid credit card digit.
  26.  * Dynamic placeholder for random digit,
  27.  * and place the x where you want in the CC digit
  28.  *
  29.  * Ex:  497411111xxx1111
  30.  *      49xx111111111111
  31.  *      4xxxx11111111111
  32.  *      411111111111x111
  33.  *      411111xxx1111111
  34.  *
  35.  * @param ccNumber '11111111xxxx1111'
  36.  * @param placeholder 'x'
  37.  * @param maxTry 10e4
  38.  *
  39.  * @returns {string[]}
  40.  */
  41. function generateValidCC(ccNumber, placeholder = null, maxTry = 10e4) {
  42.   if(!ccNumber || !placeholder) {
  43.     throw new TypeError('No ccNumber or placeholder given');
  44.   }
  45.  
  46.   const ccArr = [];
  47.   const xAmount = (ccNumber.split(placeholder).length - 1);
  48.   var i = 0;
  49.  
  50.   if (xAmount <= 0 ) {
  51.     throw new TypeError('No placeholder found in ccNumber');
  52.   }
  53.  
  54.   while (i < maxTry) {
  55.     const rndCard = ccNumber.replace(placeholder.repeat(xAmount), random(xAmount));
  56.     const cValid = isValid(rndCard);
  57.     if (cValid && ccArr.indexOf(rndCard) === -1) {
  58.       ccArr.push(rndCard);
  59.     }
  60.     i++;
  61.   }
  62.   return ccArr;
  63. }
  64.  
  65. function getRandomIntInclusive(min, max) {
  66.   min = Math.ceil(min);
  67.   max = Math.floor(max);
  68.   return Math.floor(Math.random() * (max - min + 1)) + min;
  69. }
  70.  
  71. function random(length) {
  72.   var output = '';
  73.   var i = 0;
  74.   while (i < length) {
  75.     output += getRandomIntInclusive(0, 9);
  76.     i++;
  77.   }
  78.   return output;
  79. }
  80.  
  81. /**
  82.  * Luhn Algorithm
  83.  * @see https://en.wikipedia.org/wiki/Luhn_algorithm
  84.  */
  85. function isValid(ccNumber) {
  86.   var sum = 0;
  87.   var alternate = false;
  88.   var i = ccNumber.length - 1;
  89.   while (i >= 0) {
  90.     var n = parseInt(ccNumber.substring(i, i + 1));
  91.     if (alternate) {
  92.       n *= 2;
  93.       if (n > 9) {
  94.         n = (n % 10) + 1;
  95.       }
  96.     }
  97.     sum += n;
  98.     alternate = !alternate;
  99.     i--;
  100.   }
  101.   return (sum % 10 == 0);
  102. };
Add Comment
Please, Sign In to add comment