Sample code for 30+ languages & platforms
Swift

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

Swift

func chilkatTest() {
    var success: Bool = false

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

    let manifest = CkoJsonObject()!
    let crypt = CkoCrypt2()!

    let zip = CkoZip()!
    zip.newZip(zipPath: "notUsedAndNeverCreated.zip")

    crypt.hashAlgorithm = "sha1"
    // Return hashes as lowercase hex.
    crypt.encodingMode = "hexlower"

    var digestStr: String?

    let pngData = CkoBinData()!
    // Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
    zip.addBd(pathInZip: "icon.png", bd: pngData)
    digestStr = crypt.hashBdENC(bd: pngData)
    manifest.updateString(jsonPath: "\"icon.png\"", value: digestStr)

    pngData.clear()
    // Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
    zip.addBd(pathInZip: "icon@2x.png", bd: pngData)
    digestStr = crypt.hashBdENC(bd: pngData)
    manifest.updateString(jsonPath: "\"icon@2x.png\"", value: digestStr)

    pngData.clear()
    // Assume we load the pngData with bytes for "logo.png" from somewhere...
    zip.addBd(pathInZip: "logo.png", bd: pngData)
    digestStr = crypt.hashBdENC(bd: pngData)
    manifest.updateString(jsonPath: "\"logo.png\"", value: digestStr)

    pngData.clear()
    // Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
    zip.addBd(pathInZip: "logo@2x.png", bd: pngData)
    digestStr = crypt.hashBdENC(bd: pngData)
    manifest.updateString(jsonPath: "\"logo@2x.png\"", value: digestStr)

    var passJson: String? = "{ .... }"    //  Contains the contents of pass.json
    zip.addString(pathInZip: "pass.json", content: passJson, charset: "utf-8")
    digestStr = crypt.hashStringENC(str: passJson)
    manifest.updateString(jsonPath: "\"pass.json\"", value: digestStr)

    zip.addString(pathInZip: "manifest.json", content: manifest.emit(), charset: "utf-8")

    // Make sure we have the Apple WWDR intermediate certificate available for 
    // the cert chain in the signature.
    let certVault = CkoXmlCertVault()!
    let appleWwdrCert = CkoCert()!
    success = appleWwdrCert.load(byCommonName: "Apple Worldwide Developer Relations Certification Authority")
    if success != true {
        print("The Apple WWDR intermediate certificate is not installed.")
        print("It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer")
        print("You may alternatively load the .cer like this...")
        success = appleWwdrCert.load(fromFile: "qa_data/certs/AppleWWDRCA.cer")
        if success == false {
            print("\(appleWwdrCert.lastErrorText!)")
            return
        }

    }

    certVault.addCert(cert: appleWwdrCert)
    crypt.useCertVault(vault: certVault)

    // Use a digital certificate and private key from a PFX
    let bdPfx = CkoBinData()!
    // Assume we loaded a PFX into bdPfx....
    var pfxPassword: String? = "test123"

    let cert = CkoCert()!
    success = cert.loadPfxBd(pfxData: bdPfx, password: pfxPassword)
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    // Provide the signing cert (with associated private key).
    success = crypt.setSigningCert(cert: cert)
    if success == false {
        print("\(crypt.lastErrorText!)")
        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.)
    let jsonSignedAttrs = CkoJsonObject()!
    jsonSignedAttrs.updateInt(jsonPath: "contentType", value: 1)
    jsonSignedAttrs.updateInt(jsonPath: "signingTime", value: 1)
    crypt.signingAttributes = jsonSignedAttrs.emit()

    // Sign the manifest JSON to produce a signature
    crypt.encodingMode = "base64"
    var sig: String? = crypt.signStringENC(str: manifest.emit())
    let bdSig = CkoBinData()!
    bdSig.appendEncoded(encData: sig, encoding: "base64")
    zip.addBd(pathInZip: "signature", bd: 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
    let bdZip = CkoBinData()!
    success = zip.writeBd(binData: bdZip)
    if success == false {
        print("\(zip.lastErrorText!)")
        return
    }

    print("Success.")

}