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

Sign Manifest File to Generate a Passbook .pkpass in Memory

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

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.

  // ---------------------------------------------------------------------------------------------
  // This example is the same as Sign Manifest File to Generate a Passbook .pkpass file
  // except everything happens in memory (no input files, no output files)
  // ---------------------------------------------------------------------------------------------

  // First create the manifest.json

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

  final zip = CkZip();
  zip.newZip('notUsedAndNeverCreated.zip');

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

  var digestStr = '';

  final pngData = CkBinData();
  // Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
  zip.addBd('icon.png', pngData);
  digestStr = crypt.hashBdENC(pngData);
  manifest.updateString('"icon.png"', digestStr);

  pngData.clear();
  // Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
  zip.addBd('icon@2x.png', pngData);
  digestStr = crypt.hashBdENC(pngData);
  manifest.updateString('"icon@2x.png"', digestStr);

  pngData.clear();
  // Assume we load the pngData with bytes for "logo.png" from somewhere...
  zip.addBd('logo.png', pngData);
  digestStr = crypt.hashBdENC(pngData);
  manifest.updateString('"logo.png"', digestStr);

  pngData.clear();
  // Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
  zip.addBd('logo@2x.png', pngData);
  digestStr = crypt.hashBdENC(pngData);
  manifest.updateString('"logo@2x.png"', digestStr);

  final passJson = '{ .... }';  // Contains the contents of pass.json
  zip.addString('pass.json', passJson, 'utf-8');
  digestStr = crypt.hashStringENC(passJson);
  manifest.updateString('"pass.json"', digestStr);

  zip.addString('manifest.json', manifest.emit(), 'utf-8');

  // 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
  final bdPfx = CkBinData();
  // Assume we loaded a PFX into bdPfx....
  final pfxPassword = 'test123';

  final cert = CkCert();
  try {
    cert.loadPfxBd(bdPfx, 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 to produce a signature
  crypt.encodingMode = 'base64';
  final sig = crypt.signStringENC(manifest.emit());
  final bdSig = CkBinData();
  bdSig.appendEncoded(sig, 'base64');
  zip.addBd('signature', bdSig);

  // ---------------------------------------------------------------------------------------------
  // 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).
  // the .zip is written to bdZip
  final bdZip = CkBinData();
  try {
    zip.writeBd(bdZip);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success.');
}