feat: support alphanumeric CNPJ (July 2026 format) - #2
Merged
Conversation
Since July 2026 the CNPJ is alphanumeric: the first twelve characters may be digits or uppercase letters (A-Z), while the last two remain numeric verification digits. Each character is worth its ASCII code minus 48 (0-9 = 0-9, A = 17 ... Z = 42); the module-11 weights are unchanged, so every legacy numeric CNPJ stays valid. Also replaces the isDigit() char check with explicit '0'..'9' / 'A'..'Z' ranges, since Char.isDigit() accepts non-ASCII digits that would feed garbage into the ASCII-48 calculation. Reference: https://www.serpro.gov.br/menu/noticias/videos/calculodvcnpjalfanaumerico.pdf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds support for the alphanumeric CNPJ format, in effect since July 2026.
The first twelve characters may now be digits (
0-9) or uppercase letters (A-Z); the last two remain numeric verification digits. Total length stays 14.Algorithm
Each character is worth its ASCII code minus 48, so
0-9map to 0-9 andA-Zcontinue the sequence from 17 (A) to 42 (Z). The module-11 weights are unchanged, which makes the change fully backwards compatible — every legacy numeric CNPJ still validates.Verified against the official SERPRO / Receita Federal documentation, including its worked example
12.ABC.345/01DE→ check digits35.Changes
isCnpj()acceptsA-Zin the first 12 positions; last 2 must be digits.Char.isDigit()with explicit'0'..'9'/'A'..'Z'ranges —isDigit()accepts non-ASCII digits (e.g. Arabic-Indic٦) that would corrupt the ASCII-48 math.Long.isCnpj()unchanged (numeric-only by nature; an alphanumeric CNPJ is not representable as a number).Tests
ValidNumericCnpjGeneratorproperty test locking legacy numeric compatibility.All 16 tests pass; detekt clean.