Why Generate CPF/CNPJ for Testing?
When developing software for the Brazilian market, you constantly need valid CPF (individual tax ID) and CNPJ (business tax ID) numbers for testing. Using real numbers is illegal and a privacy violation. Generated numbers pass validation algorithms but are not registered with the federal revenue service.
CPF Structure
A CPF has 11 digits: XXX.XXX.XXX-DD where DD are check digits.
// CPF validation algorithm
function validateCPF(cpf) {
cpf = cpf.replace(/\D/g, '');
if (cpf.length !== 11) return false;
if (/^(\d)\1{10}$/.test(cpf)) return false; // all same digits
// First check digit
let sum = 0;
for (let i = 0; i < 9; i++) sum += parseInt(cpf[i]) * (10 - i);
let d1 = 11 - (sum % 11);
if (d1 >= 10) d1 = 0;
// Second check digit
sum = 0;
for (let i = 0; i < 10; i++) sum += parseInt(cpf[i]) * (11 - i);
let d2 = 11 - (sum % 11);
if (d2 >= 10) d2 = 0;
return parseInt(cpf[9]) === d1 && parseInt(cpf[10]) === d2;
}CNPJ Structure
A CNPJ has 14 digits: XX.XXX.XXX/XXXX-DD. The first 8 digits identify the company, the next 4 are the branch number (0001 for headquarters), and the last 2 are check digits.
Important Legal Note
- Generated numbers are mathematically valid but not registered
- Using them for fraud is a crime under Brazilian law
- They are intended exclusively for software testing, form validation, and development
- Never use generated numbers in production systems or financial transactions
Common Testing Scenarios
| Scenario | What You Need |
|---|---|
| Form validation testing | Valid CPF/CNPJ numbers |
| Edge case testing | Invalid numbers to verify rejection |
| Database seeding | Multiple unique valid numbers |
| API testing | Valid format for request payloads |
| Automated tests (CI/CD) | Deterministic valid numbers |
Generate valid CPF and CNPJ numbers instantly for your tests with the PureTools CPF Generator and CNPJ Generator. Get formatted or unformatted numbers with a single click. Perfect for developers building Brazilian applications.