Brazil CNPJ
Based >Extensible Algorithm Framework
The Brazil CNPJ algorithm is a built-in instance of the Checkdigit framework.
This algorithm masks input in both the legacy numeric and the new alphanumeric formats of Brazilian CNPJ (Cadastro Nacional da Pessoa Juridica) numbers.
The characteristics of the input numbers are:
-
14 characters, comprised of:
-
12-character body +
-
2 sequential Mod-11 check digits in the final two positions
-
-
body characters may be 0-9 (numeric) or A-Z/0-9 (alphanumeric)
-
formatting separators (., /, -) are accepted and preserved in the output
-
short inputs with no formatting characters are left-padded with zeros
-
check digits are always numeric regardless of body format
-
alphanumeric character values follow Receita Federal's ASCII-48 mapping (A=17...Z=42)
The algorithm masks all 12 body characters: numeric bodies are masked using CM Digits, alphanumeric bodies using CM Alpha-Numeric. Two sequential Mod-11 check digits are recomputed and placed in the final two positions.
Input that does not meet the expected format will be masked using a fallback instance of the CM Alpha-Numeric algorithm.
Examples
Numeric:
-
"87.714.363/8656-66" -> "52.390.671/4823-41"
-
"87714363865666" -> "52390671482341"
Alphanumeric:
-
"KQ.I79.WV5/UA6S-03" -> "BR.4X2.NM8/PH3T-27"
-
"KQI79WV5UA6S03" -> "BR4X2NM8PH3T27"