extractCpf static method

String? extractCpf({
  1. String? subjectDn,
  2. String? otherNameNaturalPerson,
  3. String? otherNameResponsible,
})

Extracts the CPF in the order recommended by the standard:

  1. serialNumber of the Subject DN (CPF directly);
  2. natural-person otherName (positional block);
  3. otherName of the legal entity's responsible (same block layout).

Returns 11 digits with verified check digits, or null.

Implementation

static String? extractCpf({
  String? subjectDn,
  String? otherNameNaturalPerson,
  String? otherNameResponsible,
}) {
  final fromDn = _digitsOnly(_dnAttribute(subjectDn, 'serialNumber') ??
      _dnAttribute(subjectDn, oidDnSerialNumber));
  if (fromDn != null && fromDn.length == 11 && isValidCpf(fromDn)) {
    return fromDn;
  }

  for (final block in [otherNameNaturalPerson, otherNameResponsible]) {
    final cpf = extractCpfFromBlock(block);
    if (cpf != null) return cpf;
  }
  return null;
}