This Perl code generates valid MasterCard or Visa card number and uses my implementation of Luhn algorithm.
To access the values you can do
$ccvar= getcc(); $cvvvar= getcvv();
which saves the credit card number to variable ccvar and CVV value to cvvvar. The full source as below.
########################################################### # 11 Apr, 2014 # Disclaimer: I believe some routines can be simplified. Sorry. I started writing this script with 0 knowledge of Perl, and learnt while writing. ########################################################### # This program generates a valid MasterCard or Visa card number. my $i; my $numdigit=16; my $numcvv=3; my @ccnum; my @cvv; sub getcvv { for ($i=1; $i<=$numcvv; $i++) { $cvv[$i] = int(rand(9)); $tempcvv=$tempcvv.$cvv[$i]; } return $tempcvv; } sub getcc { gencc(); luhncorrectcc(); for ($i=1;$i<=$numdigit;$i++) { $tempcc=$tempcc.$ccnum[$i]; } return $tempcc; } sub sumofdigit { my $testvalue=$_[0]; my $sum=0; while ($testvalue) { $sum+=$testvalue%10; $testvalue/=10; } return $sum; } sub gencc { my $numprefix; my $choice=int(rand(5)) + 1; if($choice == 1){ $ccnum[1]=4; $ccnum[2]=5; $ccnum[3]=3; $ccnum[4]=2; $numprefix=4; }if($choice == 2){ $ccnum[1]=4; $ccnum[2]=5; $ccnum[3]=3; $ccnum[4]=9; $numprefix=4; }if($choice == 3){ $ccnum[1]=4; $ccnum[2]=5; $ccnum[3]=5; $ccnum[4]=6; $numprefix=4; }if($choice == 4){ $ccnum[1]=4; $ccnum[2]=7; $ccnum[3]=1; $ccnum[4]=6; $numprefix=4; }if($choice == 5){ $ccnum[1]=5; $ccnum[2]=1; $numprefix=2; }if($choice == 6){ $ccnum[1]=5; $ccnum[2]=4; $numprefix=2; } for ($i=$numprefix+1; $i<=$numdigit; $i++) { $ccnum[$i] = int(rand(9)); } } sub luhncorrectcc { my $state=1; my $sumofd=0; for($i=$numdigit-1;$i>0;$i--) { if($state) { $sumofd+=sumofdigit($ccnum[$i] * 2); $state=0; }else { $sumofd+=$ccnum[$i]; $state=1; } } $sumofd=$sumofd*9; $sumofd=$sumofd%10; $ccnum[$numdigit]=$sumofd; } $ccvar= getcc(); $cvvvar= getcvv(); print $ccvar; print "\n".$cvvvar;