make a nodejs function that generates a luhn valid card number, expiry date, cvc. the…
- User
make a nodejs function that generates a luhn valid card number, expiry date, cvc. the card number will be passed a bin number it will generate it based off of.
- Assistant
Here's a Node.js function that generates a Luhn valid card number, expiry date, and CVC based on a given BIN (Bank Identification Number):
function generateCardDetails(bin) { // Generate a random 9-digit account number const accountNumber = Math.floor(Math.random() * 900000000) + 100000000; // Combine the BIN and account number to create the card number const cardNumber = bin + accountNumber; // Calculate the Luhn check digit const digits = cardNumber.split(''); let sum = 0; let even = false; for (let i = digits.length - 1; i >= 0; i--) { let digit = parseInt(digits[i], 10); if (even) { digit *= 2; if (digit > 9) { digit -= 9; } } sum += digit; even = !even; } const checkDigit = (sum % 10 === 0) ? 0 : (10 - (sum % 10)); const luhnValidCardNumber = cardNumber + checkDigit; // Generate random expiry date (between current year and 5 years in the future) const currentYear = new Date().getFullYear(); const expiryYear = currentYear + Math.floor(Math.random() * 6); const expiryMonth = Math.floor(Math.random() * 12) + 1; // Generate a random 3-digit CVC const cvc = Math.floor(Math.random() * 900) + 100; return { cardNumber: luhnValidCardNumber, expiryDate: `${expiryMonth}/${expiryYear}`, cvc: cvc }; } // Example usage const binNumber = '123456'; // Replace with your desired BIN number const cardDetails = generateCardDetails(binNumber); console.log(cardDetails.cardNumber); console.log(cardDetails.expiryDate); console.log(cardDetails.cvc);