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

Create PKCS7 Signed File (.p7m)

See more Encryption Examples

Demonstrates how to sign a file to create a .p7m that contains both the file contents and the signature.

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();

  final certStore = CkCertStore();

  // Load a PFX file into a certificate store object.
  try {
    certStore.loadPfxFile('myPfx.pfx', 'pfxPassword');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the certificate by subject common name.
  // This should be the cert within the PFX that also
  // has a private key (also stored within the PFX).
  final jsonCN = CkJsonObject();
  jsonCN.updateString('CN', 'myCert');
  final cert = CkCert();
  try {
    certStore.findCert(jsonCN, cert);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Tell the crypt object to use the certificate for signing:
  crypt.setSigningCert(cert);

  // Sign a file, producing a .p7m as output.
  // The input file is unchanged, the test.p7m contains the 
  // contents of the input file and the signature.
  final inFile = 'test.txt';
  final outFile = 'testp7m';
  try {
    crypt.createP7M(inFile, outFile);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success!');
}