Overview
Implement the cnpj-gen package as a class-based utility to generate valid Brazilian CNPJ numbers (Cadastro Nacional da Pessoa Jurídica), the national registration number for legal entities in Brazil.
The implementation must be compatible with both the traditional numeric CNPJ and the new alphanumeric CNPJ format being gradually introduced from July 2026, as established by the Brazilian Federal Revenue Service (Receita Federal do Brasil — RFB) through Normative Instruction No. 2.229/2024.
Background
Current CNPJ Format
The CNPJ is a 14-character identifier structured as follows:
XX.XXX.XXX/XXXX-DV
└────────┘ └──┘ └┘
Root(8) Order Check
(4) Digits(2)
- Root (8 chars): Identifies the legal entity (company).
- Order (4 chars): Identifies each establishment (head office or branches).
- Check Digits (2 chars): Always numeric. Calculated via the Modulo 11 algorithm.
New Alphanumeric Format (from July 2026)
The first 12 positions (root + order) will accept any combination of uppercase letters (A–Z) and digits (0–9), while the last two positions (check digits) will remain numeric. Existing numeric CNPJs are not affected and remain valid alongside the new format.
Generation Algorithm
Generating a valid CNPJ consists of two steps:
1. Base Generation (12 characters)
Randomly produce the 12-character base (root + order):
- Numeric mode (traditional): Use only digits
0–9.
- Alphanumeric mode (new format): Use any combination of uppercase letters
A–Z and digits 0–9, ensuring at least one letter is present to distinguish it from the traditional format.
⚠️ The all-zeros base (000000000000) must be rejected, as it produces an invalid CNPJ.
2. Check Digit Calculation (Modulo 11)
Compute the two check digits from the 12-character base using the same algorithm defined in the cnpj-dv package:
- Character-to-value conversion: Each character is converted to a numeric value:
- Digits (
0–9): face value.
- Letters (
A–Z): ASCII value − 48 (e.g., A = 65 − 48 = 17, Z = 42).
- First check digit: Apply Modulo 11 on the 12-character base using the standard CNPJ weights.
- Second check digit: Append the first check digit, then apply Modulo 11 on the resulting 13-character sequence.
The final CNPJ is the concatenation of the 12-character base and the 2 numeric check digits.
Requirements
References
Overview
Implement the
cnpj-genpackage as a class-based utility to generate valid Brazilian CNPJ numbers (Cadastro Nacional da Pessoa Jurídica), the national registration number for legal entities in Brazil.The implementation must be compatible with both the traditional numeric CNPJ and the new alphanumeric CNPJ format being gradually introduced from July 2026, as established by the Brazilian Federal Revenue Service (Receita Federal do Brasil — RFB) through Normative Instruction No. 2.229/2024.
Background
Current CNPJ Format
The CNPJ is a 14-character identifier structured as follows:
New Alphanumeric Format (from July 2026)
The first 12 positions (root + order) will accept any combination of uppercase letters (A–Z) and digits (0–9), while the last two positions (check digits) will remain numeric. Existing numeric CNPJs are not affected and remain valid alongside the new format.
Generation Algorithm
Generating a valid CNPJ consists of two steps:
1. Base Generation (12 characters)
Randomly produce the 12-character base (root + order):
0–9.A–Zand digits0–9, ensuring at least one letter is present to distinguish it from the traditional format.2. Check Digit Calculation (Modulo 11)
Compute the two check digits from the 12-character base using the same algorithm defined in the
cnpj-dvpackage:0–9): face value.A–Z):ASCII value − 48(e.g.,A= 65 − 48 = 17,Z= 42).The final CNPJ is the concatenation of the 12-character base and the 2 numeric check digits.
Requirements
cnpj-genpackage following the existing conventions of this library.CnpjGen) that exposes a method to generate a random, valid CNPJ.cnpj-dvpackage (CnpjDv) — do not duplicate the algorithm.XX.XXX.XXX/XXXX-DV) or as a raw unformatted string, selectable by the caller.cnpj-dvlogic.XX.XXX.XXX/XXXX-DV).References
cnpj-dv(Implement packagecnpj-dv#11)