New Brazilian CNPJ: Is Your Code Ready for the Change?

Given the demand for new CNPJs and the chance of running out of available numbers, on October 15, 2024, the Brazilian Federal Revenue Service published a normative instruction (Normative Instruction BFR No. 2229) changing the format of the CNPJ. The new format will now include letters and numbers.
The new number will continue to have 14 positions. The first twelve positions will be alphanumeric, and the last two, the check digits, will remain numeric.
Compatibility
The new format is compatible with the previous one, and all existing numeric CNPJs will remain valid.
When does it come into force?
According to the current Normative Instruction, the forecast is July 2026, with no postponements.
Algorithm
Calculating the check digits
The check digits (CD) are calculated in two steps using the first twelve characters.
Calculation of the first check digit
For each of the CNPJ characters,
assign the value in the Value to calculate CD
column, according to the table below
(or subtract 48 from the ASCII value
):
Alphanumeric CNPJ | ASCII value | Value to calculate CD |
---|---|---|
0 | 48 | 0 |
1 | 49 | 1 |
2 | 50 | 2 |
3 | 51 | 3 |
4 | 52 | 4 |
5 | 53 | 5 |
6 | 54 | 6 |
7 | 55 | 7 |
8 | 56 | 8 |
9 | 57 | 9 |
A | 65 | 17 |
B | 66 | 18 |
C | 67 | 19 |
D | 68 | 20 |
E | 69 | 21 |
F | 70 | 22 |
G | 71 | 23 |
H | 72 | 24 |
I | 73 | 25 |
J | 74 | 26 |
K | 75 | 27 |
L | 76 | 28 |
M | 77 | 29 |
N | 78 | 30 |
O | 79 | 31 |
P | 80 | 32 |
Q | 81 | 33 |
R | 82 | 34 |
S | 83 | 35 |
T | 84 | 36 |
U | 85 | 37 |
V | 86 | 38 |
W | 87 | 39 |
X | 88 | 40 |
Y | 89 | 41 |
Z | 90 | 42 |
Example:
CNPJ | 1 | 2 | A | B | C | 3 | 4 | 5 | 0 | 1 | D | E |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Value | 1 | 2 | 17 | 18 | 19 | 3 | 4 | 5 | 0 | 1 | 20 | 21 |
Distribute the importance values from 2 to 9 from right to left (starting again after the eighth character), as shown in the example:
CNPJ | 1 | 2 | A | B | C | 3 | 4 | 5 | 0 | 1 | D | E |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Value | 1 | 2 | 17 | 18 | 19 | 3 | 4 | 5 | 0 | 1 | 20 | 21 |
Importance | 5 | 4 | 3 | 2 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 |
Multiply the value and importance of each column and sum up all the results:
CNPJ | 1 | 2 | A | B | C | 3 | 4 | 5 | 0 | 1 | D | E |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Value | 1 | 2 | 17 | 18 | 19 | 3 | 4 | 5 | 0 | 1 | 20 | 21 |
Importance | 5 | 4 | 3 | 2 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 |
Multiplication | 5 | 8 | 51 | 36 | 171 | 24 | 28 | 30 | 0 | 4 | 60 | 42 |
Sum (5+8+…+42) = 459
Obtain the remainder of the division of the sum by 11. If the remainder of the division is equal to 1 or 0, the first digit will be equal to 0 (zero). Otherwise, the first digit is equal to the result of 11 minus the remainder.
Example:
Remainder of the division 459/11 = 8
1º CD = 3 (result of 11-8)
Calculation of the second check digit
To calculate the second digit, add the first check digit (CD) to the end of the CNPJ, making it 13 characters, then repeat the steps used for the first digit.
Example:
CNPJ | 1 | 2 | A | B | C | 3 | 4 | 5 | 0 | 1 | D | E | 3 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Value | 1 | 2 | 17 | 18 | 19 | 3 | 4 | 5 | 0 | 1 | 20 | 21 | 3 |
Importance | 6 | 5 | 4 | 3 | 2 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 |
Multiplication | 6 | 10 | 68 | 54 | 38 | 27 | 32 | 35 | 0 | 5 | 80 | 63 | 6 |
Sum (6+10+…+6) = 424
Remainder of the division 424/11 = 6
2º CD = 5 (result of 11-6)
Final result: 12.ABC.345/01DE-35
Technical adjustments
Database
CNPJ fields, if numeric, must be converted to alphanumeric.
Interface
Validation mask
As mentioned, the mask of the new CNPJ
has been changed, so it will be necessary to update the validation mask of the <input>
.
The new pattern:
[A-Z0-9]{2}.[A-Z0-9]{3}.[A-Z0-9]{3}/[A-Z0-9]{4}-[0-9]{2}
Validation algorithm
The validation algorithm needs to be updated to validate an alphanumeric CNPJ.
For convenience, I’ve released a TypeScript lib with the new algorithm (gerador-validador-cnpj).
Install with NPM:
npm install gerador-validador-cnpj -D
Use:
import { validate as validadeCNPJ } from 'gerador-validador-cnpj'
const isCNPJValid = validadeCNPJ('01234567890000')
const isCNPJAlphanumericValid = validadeCNPJ('9ZW2JIM2OWTG85', {
validateAlphanumeric: true
})
const isFormattedCNPJValid = validadeCNPJ('01.234.567/8900-00')
const isFormattedCNPJAlphanumericValid = validadeCNPJ('8I.S4O.LPO/PRD7-81', {
validateAlphanumeric: true
})
If you’re using Deno, you don’t need to install it, just import:
import { validate } from 'jsr:@tiagoporto/gerador-validador-cnpj'
Complete documentation on NPM or JSR.