Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Manifest
oleobject loo_Crypt
oleobject loo_Zip
string ls_DigestStr
oleobject loo_PngData
string ls_PassJson
oleobject loo_CertVault
oleobject loo_AppleWwdrCert
oleobject loo_BdPfx
string ls_PfxPassword
oleobject loo_Cert
oleobject loo_JsonSignedAttrs
string ls_Sig
oleobject loo_BdSig
oleobject loo_BdZip

li_Success = 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

loo_Manifest = create oleobject
li_rc = loo_Manifest.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Manifest
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

loo_Zip = create oleobject
li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")

loo_Zip.NewZip("notUsedAndNeverCreated.zip")

loo_Crypt.HashAlgorithm = "sha1"
// Return hashes as lowercase hex.
loo_Crypt.EncodingMode = "hexlower"

loo_PngData = create oleobject
li_rc = loo_PngData.ConnectToNewObject("Chilkat.BinData")

// Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
loo_Zip.AddBd("icon.png",loo_PngData)
ls_DigestStr = loo_Crypt.HashBdENC(loo_PngData)
loo_Manifest.UpdateString("~"icon.png~"",ls_DigestStr)

loo_PngData.Clear()
// Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
loo_Zip.AddBd("icon@2x.png",loo_PngData)
ls_DigestStr = loo_Crypt.HashBdENC(loo_PngData)
loo_Manifest.UpdateString("~"icon@2x.png~"",ls_DigestStr)

loo_PngData.Clear()
// Assume we load the pngData with bytes for "logo.png" from somewhere...
loo_Zip.AddBd("logo.png",loo_PngData)
ls_DigestStr = loo_Crypt.HashBdENC(loo_PngData)
loo_Manifest.UpdateString("~"logo.png~"",ls_DigestStr)

loo_PngData.Clear()
// Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
loo_Zip.AddBd("logo@2x.png",loo_PngData)
ls_DigestStr = loo_Crypt.HashBdENC(loo_PngData)
loo_Manifest.UpdateString("~"logo@2x.png~"",ls_DigestStr)

ls_PassJson = "{ .... }"//  Contains the contents of pass.json
loo_Zip.AddString("pass.json",ls_PassJson,"utf-8")
ls_DigestStr = loo_Crypt.HashStringENC(ls_PassJson)
loo_Manifest.UpdateString("~"pass.json~"",ls_DigestStr)

loo_Zip.AddString("manifest.json",loo_Manifest.Emit(),"utf-8")

// Make sure we have the Apple WWDR intermediate certificate available for 
// the cert chain in the signature.
loo_CertVault = create oleobject
li_rc = loo_CertVault.ConnectToNewObject("Chilkat.XmlCertVault")

loo_AppleWwdrCert = create oleobject
li_rc = loo_AppleWwdrCert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_AppleWwdrCert.LoadByCommonName("Apple Worldwide Developer Relations Certification Authority")
if li_Success <> 1 then
    Write-Debug "The Apple WWDR intermediate certificate is not installed."
    Write-Debug "It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer"
    Write-Debug "You may alternatively load the .cer like this..."
    li_Success = loo_AppleWwdrCert.LoadFromFile("qa_data/certs/AppleWWDRCA.cer")
    if li_Success = 0 then
        Write-Debug loo_AppleWwdrCert.LastErrorText
        destroy loo_Manifest
        destroy loo_Crypt
        destroy loo_Zip
        destroy loo_PngData
        destroy loo_CertVault
        destroy loo_AppleWwdrCert
        return
    end if

end if

loo_CertVault.AddCert(loo_AppleWwdrCert)
loo_Crypt.UseCertVault(loo_CertVault)

// Use a digital certificate and private key from a PFX
loo_BdPfx = create oleobject
li_rc = loo_BdPfx.ConnectToNewObject("Chilkat.BinData")

// Assume we loaded a PFX into bdPfx....
ls_PfxPassword = "test123"

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxBd(loo_BdPfx,ls_PfxPassword)
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Manifest
    destroy loo_Crypt
    destroy loo_Zip
    destroy loo_PngData
    destroy loo_CertVault
    destroy loo_AppleWwdrCert
    destroy loo_BdPfx
    destroy loo_Cert
    return
end if

// Provide the signing cert (with associated private key).
li_Success = loo_Crypt.SetSigningCert(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Manifest
    destroy loo_Crypt
    destroy loo_Zip
    destroy loo_PngData
    destroy loo_CertVault
    destroy loo_AppleWwdrCert
    destroy loo_BdPfx
    destroy loo_Cert
    return
end if

// 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.)
loo_JsonSignedAttrs = create oleobject
li_rc = loo_JsonSignedAttrs.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonSignedAttrs.UpdateInt("contentType",1)
loo_JsonSignedAttrs.UpdateInt("signingTime",1)
loo_Crypt.SigningAttributes = loo_JsonSignedAttrs.Emit()

// Sign the manifest JSON to produce a signature
loo_Crypt.EncodingMode = "base64"
ls_Sig = loo_Crypt.SignStringENC(loo_Manifest.Emit())
loo_BdSig = create oleobject
li_rc = loo_BdSig.ConnectToNewObject("Chilkat.BinData")

loo_BdSig.AppendEncoded(ls_Sig,"base64")
loo_Zip.AddBd("signature",loo_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
loo_BdZip = create oleobject
li_rc = loo_BdZip.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Zip.WriteBd(loo_BdZip)
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Manifest
    destroy loo_Crypt
    destroy loo_Zip
    destroy loo_PngData
    destroy loo_CertVault
    destroy loo_AppleWwdrCert
    destroy loo_BdPfx
    destroy loo_Cert
    destroy loo_JsonSignedAttrs
    destroy loo_BdSig
    destroy loo_BdZip
    return
end if

Write-Debug "Success."


destroy loo_Manifest
destroy loo_Crypt
destroy loo_Zip
destroy loo_PngData
destroy loo_CertVault
destroy loo_AppleWwdrCert
destroy loo_BdPfx
destroy loo_Cert
destroy loo_JsonSignedAttrs
destroy loo_BdSig
destroy loo_BdZip