Sample code for 30+ languages & platforms
Dart

Italian FatturaPA (e-Invoice) Signed XML (CADES-BES P7M) using USB SmartCard Reader

See more CAdES Examples

Demonstrates Italian e-Invoice (FatturaPA) signing by using a private key stored on a USB smartcard reader.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final crypt = CkCrypt2();
  crypt.verboseLogging = true;

  final cert = CkCert();
  // Use your smart card user PIN for signing.
  cert.smartCardPin = '0000';

  try {
    cert.loadFromSmartcard('');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    crypt.setSigningCert(cert);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The CadesEnabled property applies to all methods that create PKCS7 signatures. 
  // To create a CAdES-BES signature, set this property equal to true.
  crypt.cadesEnabled = true;

  crypt.hashAlgorithm = 'sha256';

  final signedAttrs = CkJsonObject();
  signedAttrs.updateInt('contentType', 1);
  signedAttrs.updateInt('signingTime', 1);
  signedAttrs.updateInt('messageDigest', 1);
  signedAttrs.updateInt('signingCertificateV2', 1);
  crypt.signingAttributes = signedAttrs.emit();

  // Load XML such as the following:
  //  <p:FatturaElettronica xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPR12">
  //     <FatturaElettronicaHeader>
  //        <DatiTrasmissione>
  // 	...
  //        </DatiTrasmissione>
  //        <CedentePrestatore>
  // 	...
  //        </CedentePrestatore>
  //        <CessionarioCommittente>
  // 	...
  //        </CessionarioCommittente>
  //     </FatturaElettronicaHeader>
  //     <FatturaElettronicaBody>
  //        <DatiGenerali>
  //           <DatiGeneraliDocumento>
  // 		...
  //           </DatiGeneraliDocumento>
  //        </DatiGenerali>
  //        <DatiBeniServizi>
  // 	...
  //        </DatiBeniServizi>
  //     </FatturaElettronicaBody>
  //  </p:FatturaElettronica>

  final inputXmlPath = 'c:/someDir/e-Invoice.xml';
  final outputP7mPath = 'c:/someDir/signed.p7m';

  // Create the CAdES-BES attached signature, which contains the original data.
  try {
    crypt.createP7M(inputXmlPath, outputP7mPath);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success.');
}