Java Requires Chilkat v11.0.0+
Chilkat2 Java
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 memoryChilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.JsonObject;
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.
// ---------------------------------------------------------------------------------------------
// 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
JsonObject manifest = new JsonObject();
Crypt2 crypt = new Crypt2();
Zip zip = new Zip();
zip.newZip("notUsedAndNeverCreated.zip");
crypt.setHashAlgorithm("sha1");
// Return hashes as lowercase hex.
crypt.setEncodingMode("hexlower");
BinData pngData = new BinData();
// Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
zip.addBd("icon.png", pngData);
String 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);
String 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.
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
BinData bdPfx = new BinData();
// Assume we loaded a PFX into bdPfx....
String pfxPassword = "test123";
Cert cert = new Cert();
try {
cert.loadPfxBd(bdPfx, 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 to produce a signature
crypt.setEncodingMode("base64");
String sig = crypt.signStringENC(manifest.emit());
BinData bdSig = new BinData();
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
BinData bdZip = new BinData();
try {
zip.writeBd(bdZip);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Success.");
}
}