Sample code for 30+ languages & platforms
Rust

AddDetachedSignaturePk

See more MIME Examples

Demonstrates how to add a detached signature using a certifcate and it's corresponding private key. The easiest way of doing it is when both the certificate and private key are contained together within a PFX (.pfx or .p12) file. This example demonstrates how to use the certificate and its corresponding private key when they are stored in separate files -- a .cer for the certificate, and a .pem for the private key.

Chilkat Rust Downloads

Rust

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

let mime = chilkat::Mime::new();

// Load the certificate from a .cer file.
let cert = chilkat::Cert::new();
if cert.load_from_file("aaworkarea/myCert.cer").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// Load the private key from an encrypted PEM file.
// (A private key can be loaded from other file formats also..)
let priv_key = chilkat::PrivateKey::new();
if priv_key.load_encrypted_pem_file("aaworkarea/myPrivateKey.pem", "myPassword").is_err() {
    println!("{}", priv_key.last_error_text());
    return;
}

let _ = mime.set_body_from_plain_text("This is the plain-text MIME body.").is_ok();

// Sign the MIME (adds a PKCS7 detached signature)
if mime.add_detached_signature_pk(&cert, &priv_key).is_err() {
    println!("{}", mime.last_error_text());
    return;
}

// Save the S/MIME to a file.
if mime.save_mime("aaworkarea/signedMime.txt").is_err() {
    println!("{}", mime.last_error_text());
    return;
}

println!("success!");