Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// 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
let manifest = chilkat::JsonObject::new();
let crypt = chilkat::Crypt2::new();
let zip = chilkat::Zip::new();
let _ = 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.set_append_from_dir("qa_data/p7s/pass-wallet/");
crypt.set_hash_algorithm("sha1");
// Return hashes as lowercase hex.
crypt.set_encoding_mode("hexlower");
let mut file_hash = String::new();
let mut file_path = String::new();
file_path = "qa_data/p7s/pass-wallet/icon.png".to_string();
file_hash = crypt.hash_file_enc(&file_path).unwrap_or_default();
let _ = zip.add_file("icon.png", false);
let _ = manifest.update_string("\"icon.png\"", &file_hash);
file_path = "qa_data/p7s/pass-wallet/icon@2x.png".to_string();
file_hash = crypt.hash_file_enc(&file_path).unwrap_or_default();
let _ = zip.add_file("icon@2x.png", false);
let _ = manifest.update_string("\"icon@2x.png\"", &file_hash);
file_path = "qa_data/p7s/pass-wallet/logo.png".to_string();
file_hash = crypt.hash_file_enc(&file_path).unwrap_or_default();
let _ = zip.add_file("logo.png", false);
let _ = manifest.update_string("\"logo.png\"", &file_hash);
file_path = "qa_data/p7s/pass-wallet/logo@2x.png".to_string();
file_hash = crypt.hash_file_enc(&file_path).unwrap_or_default();
let _ = zip.add_file("logo@2x.png", false);
let _ = manifest.update_string("\"logo@2x.png\"", &file_hash);
file_path = "qa_data/p7s/pass-wallet/pass.json".to_string();
file_hash = crypt.hash_file_enc(&file_path).unwrap_or_default();
let _ = zip.add_file("pass.json", false);
let _ = manifest.update_string("\"pass.json\"", &file_hash);
let sb_json = chilkat::StringBuilder::new();
let _ = manifest.emit_sb(&sb_json);
let manifest_path = "qa_data/p7s/pass-wallet/manifest.json".to_string();
let _ = sb_json.write_file(&manifest_path, "utf-8", false);
let _ = zip.add_file("manifest.json", false);
// Make sure we have the Apple WWDR intermediate certificate available for
// the cert chain in the signature.
let cert_vault = chilkat::XmlCertVault::new();
let apple_wwdr_cert = chilkat::Cert::new();
if apple_wwdr_cert.load_by_common_name("Apple Worldwide Developer Relations Certification Authority").is_err() {
println!("The Apple WWDR intermediate certificate is not installed.");
println!("It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer");
println!("You may alternatively load the .cer like this...");
if apple_wwdr_cert.load_from_file("qa_data/certs/AppleWWDRCA.cer").is_err() {
println!("{}", apple_wwdr_cert.last_error_text());
return;
}
}
let _ = cert_vault.add_cert(&apple_wwdr_cert);
let _ = crypt.use_cert_vault(&cert_vault);
// Use a digital certificate and private key from a PFX file (.pfx or .p12).
let pfx_path = "qa_data/pfx/cert_test123.pfx".to_string();
let pfx_password = "test123".to_string();
let cert = chilkat::Cert::new();
if cert.load_pfx_file(&pfx_path, &pfx_password).is_err() {
println!("{}", cert.last_error_text());
return;
}
// Provide the signing cert (with associated private key).
if crypt.set_signing_cert(&cert).is_err() {
println!("{}", crypt.last_error_text());
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 json_signed_attrs = chilkat::JsonObject::new();
let _ = json_signed_attrs.update_int("contentType", 1);
let _ = json_signed_attrs.update_int("signingTime", 1);
crypt.set_signing_attributes(&json_signed_attrs.emit().unwrap_or_default());
// Sign the manifest JSON file to produce a file named "signature".
let sig_path = "qa_data/p7s/pass-wallet/signature".to_string();
// Create the "signature" file.
if crypt.create_p7_s(&manifest_path, &sig_path).is_err() {
println!("{}", crypt.last_error_text());
return;
}
let _ = 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).
if zip.write_zip_and_close().is_err() {
println!("{}", zip.last_error_text());
return;
}
println!("Success.");