Sample code for 30+ languages & platforms
Dart

Create XAdES using Smart Card or USB Token

See more XAdES Examples

Demonstrates how to create an XAdES signed XML document using a certificate located on a smartcard or USB token.

Chilkat Dart Downloads

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

void main() {
  // Load the XML to be signed.
  final xmlToSign = CkXml();
  try {
    xmlToSign.loadXmlFile('qa_data/fattura_electronica/docToSign.xml');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final gen = CkXmlDSigGen();

  gen.sigLocation = 'p:FatturaElettronica';
  gen.sigId = 'xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504';
  gen.sigNamespacePrefix = 'ds';
  gen.sigNamespaceUri = 'http://www.w3.org/2000/09/xmldsig#';
  gen.sigValueId = 'xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-sigvalue';
  gen.signedInfoCanonAlg = 'C14N';
  gen.signedInfoDigestMethod = 'sha256';

  // Create an Object to be added to the Signature.
  // Note: Chilkat will automatically populate the strings indicated by "TO BE GENERATED BY CHILKAT" with actual/correct values
  // when the XML is signed.
  final object1 = CkXml();
  object1.tag = 'xades:QualifyingProperties';
  object1.addAttribute('xmlns:xades', 'http://uri.etsi.org/01903/v1.3.2#');
  object1.addAttribute('xmlns:xades141', 'http://uri.etsi.org/01903/v1.4.1#');
  object1.addAttribute('Target', '#xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504');
  object1.updateAttrAt('xades:SignedProperties', true, 'Id', 'xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops');
  object1.updateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime', 'TO BE GENERATED BY CHILKAT');
  object1.updateAttrAt('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod', true, 'Algorithm', 'http://www.w3.org/2001/04/xmlenc#sha256');
  object1.updateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue', 'TO BE GENERATED BY CHILKAT');
  object1.updateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2', 'TO BE GENERATED BY CHILKAT');

  gen.addObject('', object1.getXml(), '', '');

  // -------- Reference 1 --------
  gen.keyInfoId = 'xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo';
  gen.addSameDocRef('xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo', 'sha256', '', '', '');

  // -------- Reference 2 --------
  gen.addSameDocRef('', 'sha256', '', '', '');
  gen.setRefIdAttr('', 'xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-ref0');

  // -------- Reference 3 --------
  gen.addObjectRef('xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops', 'sha256', '', '', 'http://uri.etsi.org/01903#SignedProperties');

  // ----------------------------------------------------------------
  //  Load a certificate that has been pre-installed on the Windows system
  //  This includes certificates on smartcards and USB tokens
  final cert = CkCert();

  //  You may provide the PIN here..
  cert.smartCardPin = '000000';

  // Load the certificate on the smartcard currently in the reader (or on the USB token).
  // Pass an empty string to allow Chilkat to automatically choose the CSP (Cryptographi Service Provider).
  // See Load Certificate on Smartcard for information about explicitly selecting a particular CSP.
  try {
    cert.loadFromSmartcard('');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  gen.setX509Cert(cert, true);
  gen.keyInfoType = 'X509Data';
  gen.x509Type = 'Certificate';

  // Load XML to be signed...
  final sbXml = CkStringBuilder();
  xmlToSign.getXmlSb(sbXml);

  gen.behaviors = 'IndentedSignature,ForceAddEnvelopedSignatureTransform';

  // Sign the XML...
  try {
    gen.createXmlDSigSb(sbXml);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Save the signed XMl to a file.
  sbXml.writeFile('qa_output/signedXml.xml', 'utf-8', false);

  print(sbXml.getAsString());

  // ----------------------------------------
  // Verify the signature we just produced...
  final verifier = CkXmlDSig();
  try {
    verifier.loadSignatureSb(sbXml);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    verifier.verifySignature(true);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('This signature was successfully verified.');
}