Sample code for 30+ languages & platforms
Java 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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.XmlCertVault;
import com.chilkatsoft.chilkat2.Zip;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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

        JsonObject manifest = new JsonObject();
        Crypt2 crypt = new Crypt2();

        Zip zip = new Zip();
        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.setAppendFromDir("qa_data/p7s/pass-wallet/");

        crypt.setHashAlgorithm("sha1");
        // Return hashes as lowercase hex.
        crypt.setEncodingMode("hexlower");

        String filePath = "qa_data/p7s/pass-wallet/icon.png";
        String 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);

        StringBuilder sbJson = new StringBuilder();
        manifest.emitSb(sbJson);
        String 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.
        XmlCertVault certVault = new XmlCertVault();
        Cert appleWwdrCert = new Cert();
        try {
            appleWwdrCert.loadByCommonName("Apple Worldwide Developer Relations Certification Authority");
        } catch (ChilkatException e) {
            System.out.println("The Apple WWDR intermediate certificate is not installed.");
            System.out.println("It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer");
            System.out.println("You may alternatively load the .cer like this...");
            try {
                appleWwdrCert.loadFromFile("qa_data/certs/AppleWWDRCA.cer");
            } catch (ChilkatException e2) {
                System.out.println(e2.getLastErrorText());
                return;
            }
        }

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

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

        Cert cert = new Cert();
        try {
            cert.loadPfxFile(pfxPath, pfxPassword);

            // Provide the signing cert (with associated private key).
            crypt.setSigningCert(cert);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            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.)
        JsonObject jsonSignedAttrs = new JsonObject();
        jsonSignedAttrs.updateInt("contentType", 1);
        jsonSignedAttrs.updateInt("signingTime", 1);
        crypt.setSigningAttributes(jsonSignedAttrs.emit());

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

        // Create the "signature" file.
        try {
            crypt.createP7S(manifestPath, sigPath);

            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).
            zip.writeZipAndClose();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Success.");
    }
}