Sample code for 30+ languages & platforms
Rust Requires Chilkat v10.1.2+

Sign a File to Create a .p7s (Detached Signature)

See more Encryption Examples

_LANGUAGE_ example to create a detached signature file (.p7s) for any type file. The signature can be verified by calling VerifyP7S and passing the original filename and the .p7s filename.

Chilkat Rust Downloads

Rust

// 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 .p7s as output:
let in_file = "/Users/chilkat/testData/pdf/sample.pdf".to_string();
let sig_file = "/Users/chilkat/testData/p7s/sample.p7s".to_string();

if crypt.create_p7_s(&in_file, &sig_file).is_err() {
    println!("{}", crypt.last_error_text());
    return;
}

if crypt.verify_p7_s(&in_file, &sig_file).is_err() {
    println!("{}", crypt.last_error_text());
    return;
}

println!("Success!");