Sample code for 30+ languages & platforms
Dart Requires Chilkat v10.1.2+

Sign a File to Create a .p7m File (using a PFX)

See more Encryption Examples

_LANGUAGE_ example to sign a file creating a .p7m file as output. The .p7m contains the signed contents of the original file. It can be verified and restored by calling VerifyP7M.

Chilkat Dart Downloads

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

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

  final crypt = CkCrypt2();

  // Use a digital certificate and private key from a PFX file (.pfx or .p12).
  final signingCertSubject = 'Acme Inc';
  final pfxFilename = '/Users/chilkat/testData/pfx/acme.pfx';
  final pfxPassword = 'test123';

  final certStore = CkCertStore();
  try {
    certStore.loadPfxFile(pfxFilename, pfxPassword);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final jsonCN = CkJsonObject();
  jsonCN.updateString('CN', signingCertSubject);

  final cert = CkCert();
  try {
    certStore.findCert(jsonCN, cert);
  } on ChilkatException {
    print('Failed to find certificate by subject common name.');
    return;
  }

  // Tell the crypt component to use this cert.
  crypt.setSigningCert(cert);

  // We can sign any type of file, creating a .p7m as output:
  var inFile = '/Users/chilkat/testData/pdf/sample.pdf';
  var outputFile = '/Users/chilkat/testData/p7m/sample.pdf.p7m';
  try {
    crypt.createP7M(inFile, outputFile);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Verify and restore the original file:
  crypt.setVerifyCert(cert);

  inFile = outputFile;
  outputFile = '/Users/chilkat/testData/pdf/restored.pdf';

  try {
    crypt.verifyP7M(inFile, outputFile);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success!');
}