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

Sign a File to Create a .p7s (Detached Signature)

See more Encryption Examples

_LANGUAGE_ example to create a detached signature file (.p7s) for any type file. The signature can be verified by calling VerifyP7S and passing the original filename and the .p7s filename.

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 .p7s as output:
  final inFile = '/Users/chilkat/testData/pdf/sample.pdf';
  final sigFile = '/Users/chilkat/testData/p7s/sample.p7s';

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

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

  print('Success!');
}