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

Validate a .pkpass Archive

See more Digital Signatures Examples

Opens a .pkpass archive (which is just a .zip renamed to .pkpass) and validates the contents. The hashes in the manifest are compared with the computed hash values for each individual file. If all computed hash values match, then the signature is verified.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let crypt = chilkat::Crypt2::new();
let zip = chilkat::Zip::new();

if zip.open_zip("qa_data/pkpass/invalid.pkpass").is_err() {
    println!("{}", zip.last_error_text());
    return;
}

// Get the contents of the manifest.json file, which contains something like this:

// {
//   "icon.png" : "0296b01347b3173e98438a003b0e88986340b2d8",
//   "logo.png" : "25de09e2d3b01ce1fe00c2ca9a90a2be1aaa05cf",
//   "icon@2x.png" : "5afd9585b08c65fdf105a90c8bd643407cba2787",
//   "pass.json" : "145ea5a5db784fff485126c77ecf7a1fc2a88ee7",
//   "strip@2x.png" : "468fa7bc93e6b55342b56fda09bdce7c829d7d46",
//   "strip.png" : "736d01f84cb73d06e8a9932e43076d68f19461ff"
// }

let ent = chilkat::ZipEntry::new();
if zip.entry_of("manifest.json", &ent).is_err() {
    println!("{}", zip.last_error_text());
    return;
}

// Get the exact content of the manifest.json for later signature verification.
let bd_manifest = chilkat::BinData::new();
let _ = ent.unzip_to_bd(&bd_manifest).is_ok();

let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
let _ = json.load(&ent.unzip_to_string(0, "utf-8").unwrap_or_default());
println!("{}", json.emit().unwrap_or_default());

// For each file in the JSON, get the filename and hex hash value.
crypt.set_encoding_mode("hexlower");
crypt.set_hash_algorithm("sha1");

let mut some_hashes_failed = false;
let mut filename = String::new();
let sb_hash_hex = chilkat::StringBuilder::new();
let bd_file_data = chilkat::BinData::new();
let num_members = json.size();
let mut i = 0;
while i < num_members {
    filename = json.name_at(i).unwrap_or_default();
    sb_hash_hex.clear();
    let _ = sb_hash_hex.append(&json.string_at(i).unwrap_or_default());

    if zip.entry_of(&filename, &ent).is_err() {
        println!("{}", zip.last_error_text());
        return;
    }

    // Get the data for this file.
    let _ = bd_file_data.clear();
    let _ = ent.unzip_to_bd(&bd_file_data).is_ok();

    let computed_hash_hex = crypt.hash_bd_enc(&bd_file_data).unwrap_or_default();
    if !sb_hash_hex.contents_equal(&computed_hash_hex, false) {
        println!("Computed hash does not match stored hash for {}", filename);
        println!("  computed: {}", computed_hash_hex);
        println!("  stored:   {}", sb_hash_hex.get_as_string().unwrap_or_default());
        some_hashes_failed = true;
    } else {
        println!("hash verified for {}({})", filename, computed_hash_hex);
    }

    i = i + 1;
}

if some_hashes_failed {
    println!("Some hashes failed.");
    return;
}

// Let's verify the signature..
// First get the signature.
if zip.entry_of("signature", &ent).is_err() {
    println!("{}", zip.last_error_text());
    return;
}

let bd_signature = chilkat::BinData::new();
let _ = ent.unzip_to_bd(&bd_signature).is_ok();

// Show the contents of the signature in base64 encoding.
println!("Signature:");
println!("{}", bd_signature.get_encoded("base64_mime").unwrap_or_default());
println!("----");

// Verify the signature against the manifest.json
crypt.set_encoding_mode("base64");
let verified = crypt.verify_bd_enc(&bd_manifest, &bd_signature.get_encoded("base64").unwrap_or_default()).is_ok();
if !verified {
    println!("{}", crypt.last_error_text());
}

println!("signature verified = {}", verified);