Rust Requires Chilkat v11.0.0+
Rust
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 memoryChilkat Rust Downloads
// 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 = chilkat::JsonObject::new();
let crypt = chilkat::Crypt2::new();
let zip = chilkat::Zip::new();
let _ = zip.new_zip("notUsedAndNeverCreated.zip");
crypt.set_hash_algorithm("sha1");
// Return hashes as lowercase hex.
crypt.set_encoding_mode("hexlower");
let mut digest_str = String::new();
let png_data = chilkat::BinData::new();
// Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
let _ = zip.add_bd("icon.png", &png_data);
digest_str = crypt.hash_bd_enc(&png_data).unwrap_or_default();
let _ = manifest.update_string("\"icon.png\"", &digest_str);
let _ = png_data.clear();
// Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
let _ = zip.add_bd("icon@2x.png", &png_data);
digest_str = crypt.hash_bd_enc(&png_data).unwrap_or_default();
let _ = manifest.update_string("\"icon@2x.png\"", &digest_str);
let _ = png_data.clear();
// Assume we load the pngData with bytes for "logo.png" from somewhere...
let _ = zip.add_bd("logo.png", &png_data);
digest_str = crypt.hash_bd_enc(&png_data).unwrap_or_default();
let _ = manifest.update_string("\"logo.png\"", &digest_str);
let _ = png_data.clear();
// Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
let _ = zip.add_bd("logo@2x.png", &png_data);
digest_str = crypt.hash_bd_enc(&png_data).unwrap_or_default();
let _ = manifest.update_string("\"logo@2x.png\"", &digest_str);
let pass_json = "{ .... }".to_string();// Contains the contents of pass.json
let _ = zip.add_string("pass.json", &pass_json, "utf-8");
digest_str = crypt.hash_string_enc(&pass_json).unwrap_or_default();
let _ = manifest.update_string("\"pass.json\"", &digest_str);
let _ = zip.add_string("manifest.json", &manifest.emit().unwrap_or_default(), "utf-8");
// 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
let bd_pfx = chilkat::BinData::new();
// Assume we loaded a PFX into bdPfx....
let pfx_password = "test123".to_string();
let cert = chilkat::Cert::new();
if cert.load_pfx_bd(&bd_pfx, &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 to produce a signature
crypt.set_encoding_mode("base64");
let sig = crypt.sign_string_enc(&manifest.emit().unwrap_or_default()).unwrap_or_default();
let bd_sig = chilkat::BinData::new();
let _ = bd_sig.append_encoded(&sig, "base64");
let _ = zip.add_bd("signature", &bd_sig);
// ---------------------------------------------------------------------------------------------
// 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 bd_zip = chilkat::BinData::new();
if zip.write_bd(&bd_zip).is_err() {
println!("{}", zip.last_error_text());
return;
}
println!("Success.");