Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oManifest
LOCAL oCrypt
LOCAL oZip
LOCAL cDigestStr
LOCAL oPngData
LOCAL cPassJson
LOCAL oCertVault
LOCAL oAppleWwdrCert
LOCAL oBdPfx
LOCAL cPfxPassword
LOCAL oCert
LOCAL oJsonSignedAttrs
LOCAL cSig
LOCAL oBdSig
LOCAL oBdZip
nSuccess := 0
// 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
oManifest := CreateObject("Chilkat.JsonObject")
oCrypt := CreateObject("Chilkat.Crypt2")
oZip := CreateObject("Chilkat.Zip")
oZip:NewZip("notUsedAndNeverCreated.zip")
oCrypt:HashAlgorithm := "sha1"
// Return hashes as lowercase hex.
oCrypt:EncodingMode := "hexlower"
oPngData := CreateObject("Chilkat.BinData")
// Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
oZip:AddBd("icon.png", oPngData)
cDigestStr := oCrypt:HashBdENC(oPngData)
oManifest:UpdateString('"icon.png"', cDigestStr)
oPngData:Clear()
// Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
oZip:AddBd("icon@2x.png", oPngData)
cDigestStr := oCrypt:HashBdENC(oPngData)
oManifest:UpdateString('"icon@2x.png"', cDigestStr)
oPngData:Clear()
// Assume we load the pngData with bytes for "logo.png" from somewhere...
oZip:AddBd("logo.png", oPngData)
cDigestStr := oCrypt:HashBdENC(oPngData)
oManifest:UpdateString('"logo.png"', cDigestStr)
oPngData:Clear()
// Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
oZip:AddBd("logo@2x.png", oPngData)
cDigestStr := oCrypt:HashBdENC(oPngData)
oManifest:UpdateString('"logo@2x.png"', cDigestStr)
cPassJson := "{ .... }"// Contains the contents of pass.json
oZip:AddString("pass.json", cPassJson, "utf-8")
cDigestStr := oCrypt:HashStringENC(cPassJson)
oManifest:UpdateString('"pass.json"', cDigestStr)
oZip:AddString("manifest.json", oManifest:Emit(), "utf-8")
// Make sure we have the Apple WWDR intermediate certificate available for
// the cert chain in the signature.
oCertVault := CreateObject("Chilkat.XmlCertVault")
oAppleWwdrCert := CreateObject("Chilkat.Cert")
nSuccess := oAppleWwdrCert:LoadByCommonName("Apple Worldwide Developer Relations Certification Authority")
IF (nSuccess != 1)
? "The Apple WWDR intermediate certificate is not installed."
? "It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer"
? "You may alternatively load the .cer like this..."
nSuccess := oAppleWwdrCert:LoadFromFile("qa_data/certs/AppleWWDRCA.cer")
IF (nSuccess == 0)
? oAppleWwdrCert:LastErrorText
oManifest:destroy()
oCrypt:destroy()
oZip:destroy()
oPngData:destroy()
oCertVault:destroy()
oAppleWwdrCert:destroy()
RETURN
ENDIF
ENDIF
oCertVault:AddCert(oAppleWwdrCert)
oCrypt:UseCertVault(oCertVault)
// Use a digital certificate and private key from a PFX
oBdPfx := CreateObject("Chilkat.BinData")
// Assume we loaded a PFX into bdPfx....
cPfxPassword := "test123"
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxBd(oBdPfx, cPfxPassword)
IF (nSuccess == 0)
? oCert:LastErrorText
oManifest:destroy()
oCrypt:destroy()
oZip:destroy()
oPngData:destroy()
oCertVault:destroy()
oAppleWwdrCert:destroy()
oBdPfx:destroy()
oCert:destroy()
RETURN
ENDIF
// Provide the signing cert (with associated private key).
nSuccess := oCrypt:SetSigningCert(oCert)
IF (nSuccess == 0)
? oCrypt:LastErrorText
oManifest:destroy()
oCrypt:destroy()
oZip:destroy()
oPngData:destroy()
oCertVault:destroy()
oAppleWwdrCert:destroy()
oBdPfx:destroy()
oCert:destroy()
RETURN
ENDIF
// 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.)
oJsonSignedAttrs := CreateObject("Chilkat.JsonObject")
oJsonSignedAttrs:UpdateInt("contentType", 1)
oJsonSignedAttrs:UpdateInt("signingTime", 1)
oCrypt:SigningAttributes := oJsonSignedAttrs:Emit()
// Sign the manifest JSON to produce a signature
oCrypt:EncodingMode := "base64"
cSig := oCrypt:SignStringENC(oManifest:Emit())
oBdSig := CreateObject("Chilkat.BinData")
oBdSig:AppendEncoded(cSig, "base64")
oZip:AddBd("signature", oBdSig)
// ---------------------------------------------------------------------------------------------
// 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
oBdZip := CreateObject("Chilkat.BinData")
nSuccess := oZip:WriteBd(oBdZip)
IF (nSuccess == 0)
? oZip:LastErrorText
oManifest:destroy()
oCrypt:destroy()
oZip:destroy()
oPngData:destroy()
oCertVault:destroy()
oAppleWwdrCert:destroy()
oBdPfx:destroy()
oCert:destroy()
oJsonSignedAttrs:destroy()
oBdSig:destroy()
oBdZip:destroy()
RETURN
ENDIF
? "Success."
oManifest:destroy()
oCrypt:destroy()
oZip:destroy()
oPngData:destroy()
oCertVault:destroy()
oAppleWwdrCert:destroy()
oBdPfx:destroy()
oCert:destroy()
oJsonSignedAttrs:destroy()
oBdSig:destroy()
oBdZip:destroy()