Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Manifest
oleobject loo_Crypt
oleobject loo_Zip
string ls_FileHash
string ls_FilePath
oleobject loo_SbJson
string ls_ManifestPath
oleobject loo_CertVault
oleobject loo_AppleWwdrCert
string ls_PfxPath
string ls_PfxPassword
oleobject loo_Cert
oleobject loo_JsonSignedAttrs
string ls_SigPath

li_Success = 0

// 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

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("qa_data/p7s/pass-wallet/example.pkpass")
// Set the AppendFromDir property to prevent that relative paths from being stored in the .pkpass archive.
loo_Zip.AppendFromDir = "qa_data/p7s/pass-wallet/"

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

ls_FilePath = "qa_data/p7s/pass-wallet/icon.png"
ls_FileHash = loo_Crypt.HashFileENC(ls_FilePath)
loo_Zip.AddFile("icon.png",0)
loo_Manifest.UpdateString("~"icon.png~"",ls_FileHash)

ls_FilePath = "qa_data/p7s/pass-wallet/icon@2x.png"
ls_FileHash = loo_Crypt.HashFileENC(ls_FilePath)
loo_Zip.AddFile("icon@2x.png",0)
loo_Manifest.UpdateString("~"icon@2x.png~"",ls_FileHash)

ls_FilePath = "qa_data/p7s/pass-wallet/logo.png"
ls_FileHash = loo_Crypt.HashFileENC(ls_FilePath)
loo_Zip.AddFile("logo.png",0)
loo_Manifest.UpdateString("~"logo.png~"",ls_FileHash)

ls_FilePath = "qa_data/p7s/pass-wallet/logo@2x.png"
ls_FileHash = loo_Crypt.HashFileENC(ls_FilePath)
loo_Zip.AddFile("logo@2x.png",0)
loo_Manifest.UpdateString("~"logo@2x.png~"",ls_FileHash)

ls_FilePath = "qa_data/p7s/pass-wallet/pass.json"
ls_FileHash = loo_Crypt.HashFileENC(ls_FilePath)
loo_Zip.AddFile("pass.json",0)
loo_Manifest.UpdateString("~"pass.json~"",ls_FileHash)

loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")

loo_Manifest.EmitSb(loo_SbJson)
ls_ManifestPath = "qa_data/p7s/pass-wallet/manifest.json"
loo_SbJson.WriteFile(ls_ManifestPath,"utf-8",0)
loo_Zip.AddFile("manifest.json",0)

// 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_SbJson
        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 file (.pfx or .p12).
ls_PfxPath = "qa_data/pfx/cert_test123.pfx"
ls_PfxPassword = "test123"

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

li_Success = loo_Cert.LoadPfxFile(ls_PfxPath,ls_PfxPassword)
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Manifest
    destroy loo_Crypt
    destroy loo_Zip
    destroy loo_SbJson
    destroy loo_CertVault
    destroy loo_AppleWwdrCert
    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_SbJson
    destroy loo_CertVault
    destroy loo_AppleWwdrCert
    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 file to produce a file named "signature".
ls_SigPath = "qa_data/p7s/pass-wallet/signature"

// Create the "signature" file.
li_Success = loo_Crypt.CreateP7S(ls_ManifestPath,ls_SigPath)
if li_Success = 0 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Manifest
    destroy loo_Crypt
    destroy loo_Zip
    destroy loo_SbJson
    destroy loo_CertVault
    destroy loo_AppleWwdrCert
    destroy loo_Cert
    destroy loo_JsonSignedAttrs
    return
end if

loo_Zip.AddFile("signature",0)

// ---------------------------------------------------------------------------------------------
// 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).
li_Success = loo_Zip.WriteZipAndClose()
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Manifest
    destroy loo_Crypt
    destroy loo_Zip
    destroy loo_SbJson
    destroy loo_CertVault
    destroy loo_AppleWwdrCert
    destroy loo_Cert
    destroy loo_JsonSignedAttrs
    return
end if

Write-Debug "Success."


destroy loo_Manifest
destroy loo_Crypt
destroy loo_Zip
destroy loo_SbJson
destroy loo_CertVault
destroy loo_AppleWwdrCert
destroy loo_Cert
destroy loo_JsonSignedAttrs