Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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

Lua
local chilkat = require("chilkat")

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

local manifest = chilkat.JsonObject.new()
local crypt = chilkat.Crypt2.new()

local zip = chilkat.Zip.new()
zip:new_zip("qa_data/p7s/pass-wallet/example.pkpass")
-- Set the AppendFromDir property to prevent that relative paths from being stored in the .pkpass archive.
zip.append_from_dir = "qa_data/p7s/pass-wallet/"

crypt.hash_algorithm = "sha1"
-- Return hashes as lowercase hex.
crypt.encoding_mode = "hexlower"

local filePath = "qa_data/p7s/pass-wallet/icon.png"
local fileHash = crypt:hash_file_enc(filePath)
zip:add_file("icon.png", false)
manifest:update_string("\"icon.png\"", fileHash)

filePath = "qa_data/p7s/pass-wallet/icon@2x.png"
fileHash = crypt:hash_file_enc(filePath)
zip:add_file("icon@2x.png", false)
manifest:update_string("\"icon@2x.png\"", fileHash)

filePath = "qa_data/p7s/pass-wallet/logo.png"
fileHash = crypt:hash_file_enc(filePath)
zip:add_file("logo.png", false)
manifest:update_string("\"logo.png\"", fileHash)

filePath = "qa_data/p7s/pass-wallet/logo@2x.png"
fileHash = crypt:hash_file_enc(filePath)
zip:add_file("logo@2x.png", false)
manifest:update_string("\"logo@2x.png\"", fileHash)

filePath = "qa_data/p7s/pass-wallet/pass.json"
fileHash = crypt:hash_file_enc(filePath)
zip:add_file("pass.json", false)
manifest:update_string("\"pass.json\"", fileHash)

local sbJson = chilkat.StringBuilder.new()
manifest:emit_sb(sbJson)
local manifestPath = "qa_data/p7s/pass-wallet/manifest.json"
sbJson:write_file(manifestPath, "utf-8", false)
zip:add_file("manifest.json", false)

-- Make sure we have the Apple WWDR intermediate certificate available for
-- the cert chain in the signature.
local certVault = chilkat.XmlCertVault.new()
local appleWwdrCert = chilkat.Cert.new()
local success = appleWwdrCert:load_by_common_name("Apple Worldwide Developer Relations Certification Authority")
if not success then
    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_from_file("qa_data/certs/AppleWWDRCA.cer")
    if not success then
        print(appleWwdrCert.last_error_text)
        return
    end
end

certVault:add_cert(appleWwdrCert)
crypt:use_cert_vault(certVault)

-- Use a digital certificate and private key from a PFX file (.pfx or .p12).
local pfxPath = "qa_data/pfx/cert_test123.pfx"
local pfxPassword = "test123"

local cert = chilkat.Cert.new()
success = cert:load_pfx_file(pfxPath, pfxPassword)
if not success then
    print(cert.last_error_text)
    return
end

-- Provide the signing cert (with associated private key).
success = crypt:set_signing_cert(cert)
if not success then
    print(crypt.last_error_text)
    return
end

-- 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.)
local jsonSignedAttrs = chilkat.JsonObject.new()
jsonSignedAttrs:update_int("contentType", 1)
jsonSignedAttrs:update_int("signingTime", 1)
crypt.signing_attributes = jsonSignedAttrs:emit()

-- Sign the manifest JSON file to produce a file named "signature".
local sigPath = "qa_data/p7s/pass-wallet/signature"

-- Create the "signature" file.
success = crypt:create_p7_s(manifestPath, sigPath)
if not success then
    print(crypt.last_error_text)
    return
end

zip:add_file("signature", false)

-- ---------------------------------------------------------------------------------------------
-- 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).
success = zip:write_zip_and_close()
if not success then
    print(zip.last_error_text)
    return
end

print("Success.")