Gerador de cartão
Autor: GonG4 | Publicado: 2011-12-13T19:12:00-02:00

Gerador simples de cartões, geralmente de crédito, que utilizam o algoritmo Luhn. O código fonte também está no post para quem quer estudar.

Sites de fonte de conhecimento:
http://www.merriampark.com/anatomycc.htm
http://bin-iin.com/MasterCard-BIN-List.html
http://www.compago.it/ajax/ID-13742.HeadA.Hack_25.html

Modo de usar: Insira os primeiros 6 dígitos oficiais de um cartão (caso sejam 4, insira outros 2 aleatórios) e o tamanho do código que será gerado (geralmente 16).

--

function creditgen(numero, tamanho)
{
 if (isNaN(numero) || numero.length != 6)
  return -1;
 else if (tamanho <= 6 || tamanho > 19)
  return -2;
 
 var soma = 0;
 var card = "";
 
 {
  soma += Number(numero.charAt(0) * 2 >= 10 ? numero.charAt(0) * 2 - 9 : numero.charAt(0) * 2);

  soma += Number(numero.charAt(2) * 2 >= 10 ? numero.charAt(2) * 2 - 9 : numero.charAt(2) * 2);
  
  soma += Number(numero.charAt(4) * 2 >= 10 ? numero.charAt(4) * 2 - 9 : numero.charAt(4) * 2);
  
  soma += Number(numero.charAt(1)) + Number(numero.charAt(3)) + Number(numero.charAt(5));
  
  card = numero;
 }

 var i = 6;
 
 { 
  if ((Number(tamanho)-1) > i)
   while (i < (Number(tamanho)-1))
   {
    card += Math.floor((Math.random()*100)%10);
    
    if (i%2)
     soma += Number(card.charAt(i));
    else
     soma += Number(card.charAt(i) * 2 >= 10 ? card.charAt(i) * 2 - 9 : card.charAt(i) * 2);
     
    i++;
   }
 }
 
 {
  var check = soma;
  
  while (check%10 != 0)
   check++;
  
  check -= soma;
  
  card += check;
 }
 
 return card;
}

function gerar(numero, tamanho)
{
 var creditogerado;
 
 if ((creditogerado = creditgen(numero, tamanho)) == -1)
  alert("Deve ser número de 6 dígitos.");
 else if(creditogerado == -2)
  alert("Tamanho tem que ser maior do que 6 e menor do que 20.");
 else
  return creditogerado;
}




Digitos: 
Tamanho: 

Cartão gerado: 


Código fonte (javascript):


Spoiler: 



function creditgen(numero, tamanho)
{
&nbsp;&nbsp;&nbsp; if (isNaN(numero) || numero.length != 6)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return -1;
&nbsp;&nbsp;&nbsp; else if (tamanho &lt;= 6)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return -2;
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; var soma = 0;
&nbsp;&nbsp;&nbsp; var card = "";
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; // Faz a soma dos 6 primeiros dígitos.
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; soma += Number(numero.charAt(0) * 2 &gt;= 10 ? numero.charAt(0) * 2 - 9 : numero.charAt(0) * 2);

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; soma += Number(numero.charAt(2) * 2 &gt;= 10 ? numero.charAt(2) * 2 - 9 : numero.charAt(2) * 2);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; soma += Number(numero.charAt(4) * 2 &gt;= 10 ? numero.charAt(4) * 2 - 9 : numero.charAt(4) * 2);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; soma += Number(numero.charAt(1)) + Number(numero.charAt(3)) + Number(numero.charAt(5));
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; card = numero;
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; var i = 6;
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; // Gera valores aleatórios até n-1.
&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if ((Number(tamanho)-1) &gt; i)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (i &lt; (Number(tamanho)-1))
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; card += Math.floor((Math.random()*100)%10);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (i%2)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; soma += Number(card.charAt(i));
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; soma += Number(card.charAt(i) * 2 &gt;= 10 ? card.charAt(i) * 2 - 9 : card.charAt(i) * 2);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i++;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; // Cálculo do "check digit".
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var check = soma;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while (check%10 != 0)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; check++;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; check -= soma;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; card += check;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; return card;
}

function gerar(numero, tamanho)
{
&nbsp;&nbsp;&nbsp; var creditogerado;
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; if ((creditogerado = creditgen(numero, tamanho)) == -1)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; alert("Deve ser número de 6 dígitos.");
&nbsp;&nbsp;&nbsp; else if(creditogerado == -2)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; alert("Tamanho tem que ser maior do que 6.");
&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return creditogerado;
}


--

*Obs: Esse post não tem como objetivo incentivar a fraude de uso de cartões online, somente ensina o funcionamento do algoritmo Luhn.