Sample code for 30+ languages & platforms
Rust

Duplicate OpensSSL to Create Signature using Cert and Key Files

See more OpenSSL Examples

This example duplicates the following:
openssl smime –sign -in something.xml -out something.der -signer mycert.crt -inkey cert.key -outform der –nodetach

Note: Although "smime" is the OpenSSL command, it's not actually producing S/MIME. The arguments "-outform der -binary" indicates that the output is binary DER (i.e. the PKCS7 binary signature). The input can be any type of file: XML, PDF, JPG, ... *anything*...

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

// Load the cert and private key from separate files.
let cert = chilkat::Cert::new();
if cert.load_from_file("myCert.crt").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

let bd = chilkat::BinData::new();
let _ = bd.load_file("cert.key").is_ok();
let privkey = chilkat::PrivateKey::new();
// Load from any format private key.
// If the file does not need a password, the 2nd arg is ignored.
if privkey.load_any_format(&bd, "password_if_needed").is_err() {
    println!("{}", privkey.last_error_text());
    return;
}

if crypt.set_signing_cert2(&cert, &privkey).is_err() {
    println!("{}", crypt.last_error_text());
    return;
}

// Create the opaque signature (PKCS7 binary DER that contains both the signature and original file data).
if crypt.create_p7_m("qa_data/infile.anything", "qa_output/outfile.der").is_err() {
    println!("{}", crypt.last_error_text());
    return;
}

println!("Success.");