Rust Requires Chilkat v10.1.2+
Rust
Sign a File to Create a .p7m File (using a PFX)
See more Encryption Examples
_LANGUAGE_ example to sign a file creating a .p7m file as output. The .p7m contains the signed contents of the original file. It can be verified and restored by calling VerifyP7M.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let crypt = chilkat::Crypt2::new();
// Use a digital certificate and private key from a PFX file (.pfx or .p12).
let signing_cert_subject = "Acme Inc".to_string();
let pfx_filename = "/Users/chilkat/testData/pfx/acme.pfx".to_string();
let pfx_password = "test123".to_string();
let cert_store = chilkat::CertStore::new();
if cert_store.load_pfx_file(&pfx_filename, &pfx_password).is_err() {
println!("{}", cert_store.last_error_text());
return;
}
let json_cn = chilkat::JsonObject::new();
let _ = json_cn.update_string("CN", &signing_cert_subject);
let cert = chilkat::Cert::new();
if cert_store.find_cert(&json_cn, &cert).is_err() {
println!("Failed to find certificate by subject common name.");
return;
}
// Tell the crypt component to use this cert.
let _ = crypt.set_signing_cert(&cert).is_ok();
// We can sign any type of file, creating a .p7m as output:
let mut in_file = "/Users/chilkat/testData/pdf/sample.pdf".to_string();
let mut output_file = "/Users/chilkat/testData/p7m/sample.pdf.p7m".to_string();
if crypt.create_p7_m(&in_file, &output_file).is_err() {
println!("{}", crypt.last_error_text());
return;
}
// Verify and restore the original file:
let _ = crypt.set_verify_cert(&cert).is_ok();
in_file = output_file.clone();
output_file = "/Users/chilkat/testData/pdf/restored.pdf".to_string();
if crypt.verify_p7_m(&in_file, &output_file).is_err() {
println!("{}", crypt.last_error_text());
return;
}
println!("Success!");