The 44 character access key (chNFe) of an NF-e or NFC-e.
Layout, fixed by the MOC: cUF(2) AAMM(4) emitter CNPJ(14) mod(2)
serie(3) nNF(9) tpEmis(1) cNF(8) cDV(1).
The CT-e (model 57), CT-e OS (67) and MDF-e (58) keys have the same 44
characters and the same check digit (CTe / MDFe Id prefix aside):
nNF is then nCT or nMDF and cNF is cCT or cMDF, so the fields
keep the names :n_nf and :c_nf. NFePHP's MakeCTe, MakeCTeOS and
Make (MDF-e) take the code from the caller and rebuild the key with the
same Keys::build, so the random code follows the same cNF rules.
The rules mirror NFePHP sped-common NFePHP\Common\Keys:
- Emitter: a CNPJ is 14 characters, alphanumeric in the first 12
(NT 2025.001) and numeric in the two check digits. A CPF (11 digits) is
left-padded with zeros to 14, exactly as
Keys::builddoes. - cDV (
Keys::verifyingDigit): the sum of(ASCII code - 48) * weight, weights 2 to 9 cycling from the right. So0..9weigh as themselves andAis 17. The digit is0when the remainder modulo 11 is 0 or 1, else11 - remainder. - cNF (
Keys::randomandKeys::cNFIsValid, NT 2019.001,cStat897): 8 digits, never equal tonNF(compared as integers) and never one of00000000to99999999(all digits equal) nor the ascending runs12345678,23456789,34567890,45678901,56789012,67890123,78901234,89012345,90123456,01234567. - valid? (
Keys::isValid): 44 characters matching^[0-9]{6}[A-Z0-9]{12}[0-9]{26}$and a correctcDV.
Summary
Functions
Builds the key from fields, each an integer or a string
The check digit of the first 43 characters of a key.
Whether c_nf equals n_nf as integers (the cStat 897 case).
A random cNF that SEFAZ accepts: 8 digits, not equal to n_nf and not one
of the trivial sequences (see the module doc).
Whether key is a well-formed 44 character key with a correct check digit.
Types
Functions
Builds the key from fields, each an integer or a string:
:c_uf,:mod,:serie,:n_nf,:tp_emis- numbers, zero-padded;:dh_emi-"YYYY-MM-DDThh:mm:ss+hh:mm"; only year and month are used;:cnpjor:cpf- the emitter;:c_nf- 8 digits; random (seerandom_c_nf/2) when absent.
The check digit is always computed. {:error, {field, reason}} names the
first field that is missing or out of range.
@spec dv(String.t()) :: 0..9
The check digit of the first 43 characters of a key.
@spec equals_n_nf?(String.t(), pos_integer() | String.t()) :: boolean()
Whether c_nf equals n_nf as integers (the cStat 897 case).
@spec random_c_nf(pos_integer() | String.t(), keyword()) :: String.t()
A random cNF that SEFAZ accepts: 8 digits, not equal to n_nf and not one
of the trivial sequences (see the module doc).
Option :random_bytes is a function from a byte count to that many bytes
(default :crypto.strong_rand_bytes/1); tests use it to drive the rejection.
Whether key is a well-formed 44 character key with a correct check digit.