Sample code for 30+ languages & platforms
Rust

CAdES BES Detached Signature

See more Encryption Examples

Demonstrates how to create a CAdES BES detached signature file (.p7s).

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();

// Use a digital certificate and private key from a PFX file (.pfx or .p12).
let pfx_path = "/Users/chilkat/testData/pfx/acme.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;
}

// Tell the crypt component to use this cert.
if crypt.set_signing_cert(&cert).is_err() {
    println!("{}", crypt.last_error_text());
    return;
}

// The CadesEnabled property applies to all methods that create PKCS7 signatures. 
// To create a CAdES-BES signature, set this property equal to true. 
crypt.set_cades_enabled(true);

// 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();

// Create the detached CAdES-BES signature:
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!");