Rust Requires Chilkat v11.0.0+
Rust
Sign a saved MIME message with DKIM
See more DKIM Examples
Sign the complete bytes of message.eml and save the result as signed.eml. Finish the message’s From, To, Date, Message-ID, MIME parts and transfer encodings before signing. Existing signatures stay in place and the new signature is inserted first.
The example signs the full body and includes From in the signed headers. Relaxed canonicalization tolerates defined whitespace and header folding changes. Editing a signed field or changing the body text can still break verification. Prepare the matching DNS record.
Chilkat Rust Downloads
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let message = chilkat::BinData::new();
if message.load_file("message.eml").is_err() {
println!("Could not read message.eml.");
return;
}
// Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
let private_key = chilkat::PrivateKey::new();
if private_key.load_any_format_file("dkim-private.pem", "").is_err() {
println!("{}", private_key.last_error_text());
return;
}
let signer = chilkat::Dkim::new();
signer.set_dkim_domain("example.com");
signer.set_dkim_selector("mail2026");
signer.set_dkim_alg("rsa-sha256");
if signer.set_dkim_private_key(&private_key).is_err() {
println!("{}", signer.last_error_text());
return;
}
// DkimCanon takes a single setting and applies it to both headers and body.
signer.set_dkim_canon("relaxed");
signer.set_dkim_headers("from:to:subject:date:message-id:mime-version:content-type:content-transfer-encoding");
// Zero signs the entire canonicalized body and omits the optional l= tag.
signer.set_dkim_body_length_count(0);
if signer.dkim_sign(&message).is_err() {
println!("{}", signer.last_error_text());
return;
}
if message.write_file("signed.eml").is_err() {
println!("Could not write signed.eml.");
return;
}
println!("Saved signed.eml.");