encryptedCsr function

Map<String, String> encryptedCsr(
  1. String csr, {
  2. String encryptKey = _defaultAesKey,
})

Cifra el CSR usando AES-128-ECB con padding PKCS7. Devuelve el cuerpo esperado por la API: { csr: '

Implementation

Map<String, String> encryptedCsr(
  String csr, {
  String encryptKey = _defaultAesKey,
}) {
  try {
    if (encryptKey.length != 16) {
      throw ApacuanaAPIError(
        'AES key debe tener exactamente 16 caracteres (AES-128).',
        statusCode: 500,
        errorCode: 'CSR_ENCRYPT_FAILED',
      );
    }
    final key = encrypt.Key.fromUtf8(encryptKey);
    final aes = encrypt.AES(
      key,
      mode: encrypt.AESMode.ecb,
      padding: 'PKCS7',
    );
    final encrypter = encrypt.Encrypter(aes);
    final encrypted = encrypter.encrypt(csr);
    return {'csr': encrypted.base64};
  } catch (e) {
    throw ApacuanaAPIError(
      'No se pudo cifrar el CSR: ${e is Exception ? e.toString() : 'Unknown error'}',
      statusCode: 500,
      errorCode: 'CSR_ENCRYPT_FAILED',
    );
  }
}