Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Sign Manifest File to Generate a Passbook .pkpass file

See more Digital Signatures Examples

Demonstrates how to create a Passbook .pkpass archive by creating a signature of a manifest file and then zipping to a .pkpass archive.

Note: Chilkat also has the capability to do everything in-memory (no files would be involved). If this is of interest, please send email to support@chilkatsoft.com

Chilkat Dart Downloads

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

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

  // ---------------------------------------------------------------------------------------------
  // Note: Chilkat also has the capability to do everything in-memory (no files would be involved).  
  // See this example:  Sign Manifest File to Generate a Passbook .pkpass in Memory
  // ---------------------------------------------------------------------------------------------

  // First create the manifest.json

  final manifest = CkJsonObject();
  final crypt = CkCrypt2();

  final zip = CkZip();
  zip.newZip('qa_data/p7s/pass-wallet/example.pkpass');
  // Set the AppendFromDir property to prevent that relative paths from being stored in the .pkpass archive.
  zip.appendFromDir = 'qa_data/p7s/pass-wallet/';

  crypt.hashAlgorithm = 'sha1';
  // Return hashes as lowercase hex.
  crypt.encodingMode = 'hexlower';

  var fileHash = '';
  var filePath = '';
  filePath = 'qa_data/p7s/pass-wallet/icon.png';
  fileHash = crypt.hashFileENC(filePath);
  zip.addFile('icon.png', false);
  manifest.updateString('"icon.png"', fileHash);

  filePath = 'qa_data/p7s/pass-wallet/icon@2x.png';
  fileHash = crypt.hashFileENC(filePath);
  zip.addFile('icon@2x.png', false);
  manifest.updateString('"icon@2x.png"', fileHash);

  filePath = 'qa_data/p7s/pass-wallet/logo.png';
  fileHash = crypt.hashFileENC(filePath);
  zip.addFile('logo.png', false);
  manifest.updateString('"logo.png"', fileHash);

  filePath = 'qa_data/p7s/pass-wallet/logo@2x.png';
  fileHash = crypt.hashFileENC(filePath);
  zip.addFile('logo@2x.png', false);
  manifest.updateString('"logo@2x.png"', fileHash);

  filePath = 'qa_data/p7s/pass-wallet/pass.json';
  fileHash = crypt.hashFileENC(filePath);
  zip.addFile('pass.json', false);
  manifest.updateString('"pass.json"', fileHash);

  final sbJson = CkStringBuilder();
  manifest.emitSb(sbJson);
  final manifestPath = 'qa_data/p7s/pass-wallet/manifest.json';
  sbJson.writeFile(manifestPath, 'utf-8', false);
  zip.addFile('manifest.json', false);

  // Make sure we have the Apple WWDR intermediate certificate available for 
  // the cert chain in the signature.
  final certVault = CkXmlCertVault();
  final appleWwdrCert = CkCert();
  try {
    appleWwdrCert.loadByCommonName('Apple Worldwide Developer Relations Certification Authority');
  } on ChilkatException {
    print('The Apple WWDR intermediate certificate is not installed.');
    print('It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer');
    print('You may alternatively load the .cer like this...');
    try {
      appleWwdrCert.loadFromFile('qa_data/certs/AppleWWDRCA.cer');
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }
  }

  certVault.addCert(appleWwdrCert);
  crypt.useCertVault(certVault);

  // Use a digital certificate and private key from a PFX file (.pfx or .p12).
  final pfxPath = 'qa_data/pfx/cert_test123.pfx';
  final pfxPassword = 'test123';

  final cert = CkCert();
  try {
    cert.loadPfxFile(pfxPath, pfxPassword);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Provide the signing cert (with associated private key).
  try {
    crypt.setSigningCert(cert);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Specify the signed attributes to be included.
  // (These attributes appear to not be necessary, but we're including
  // them just in case they become necessary in the future.)
  final jsonSignedAttrs = CkJsonObject();
  jsonSignedAttrs.updateInt('contentType', 1);
  jsonSignedAttrs.updateInt('signingTime', 1);
  crypt.signingAttributes = jsonSignedAttrs.emit();

  // Sign the manifest JSON file to produce a file named "signature".
  final sigPath = 'qa_data/p7s/pass-wallet/signature';

  // Create the "signature" file.
  try {
    crypt.createP7S(manifestPath, sigPath);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  zip.addFile('signature', false);

  // ---------------------------------------------------------------------------------------------
  // Note: Chilkat also has the capability to do everything in-memory (no files would be involved).  
  // If this is of interest, please send email to support@chilkatsoft.com
  // ---------------------------------------------------------------------------------------------

  // Create the .pkipass archive (which is a .zip archive containing the required files).
  try {
    zip.writeZipAndClose();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success.');
}