Tiago Porto

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


Tiago Porto

Logo novo CNPJ alfanumérico
CNPJ Alphanumeric Logo

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.

N - NumberS - Letter or NumberSS.SSS.SSS/SSSS - NNRoot/Order-CDAlphanumeric CNPJ

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 CDcolumn, according to the table below (or subtract 48 from the ASCII value):

Alphanumeric
CNPJ
ASCII valueValue to calculate
CD
0480
1491
2502
3513
4524
5535
6546
7557
8568
9579
A6517
B6618
C6719
D6820
E6921
F7022
G7123
H7224
I7325
J7426
K7527
L7628
M7729
N7830
O7931
P8032
Q8133
R8234
S8335
T8436
U8537
V8638
W8739
X8840
Y8941
Z9042

Example:

CNPJ12ABC34501DE
Value12171819345012021

Distribute the importance values from 2 to 9 from right to left (starting again after the eighth character), as shown in the example:

CNPJ12ABC34501DE
Value12171819345012021
Importance543298765432

Multiply the value and importance of each column and sum up all the results:

CNPJ12ABC34501DE
Value12171819345012021
Importance543298765432
Multiplication585136171242830046042

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:

CNPJ12ABC34501DE3
Value121718193450120213
Importance6543298765432
Multiplication6106854382732350580636

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.

References

Comments and reactions