Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Verify DKIM using a public key supplied as text

See more DKIM Examples

Applications that keep public keys in configuration can pass a complete PEM string or the base64 value of the DNS p= tag to LoadPublicKey. This example reads a trusted PEM as text so it can be run alongside the file-based example. Supply just the key material when passing a base64 value. Reuse the same verifier for signatures with that exact domain and selector.

Chilkat Rust Downloads

Rust
let mut success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.

let message = chilkat::BinData::new();
success = message.load_file("signed.eml").is_ok();
if !success {
    println!("Could not read signed.eml.");
    return;
}

let verifier = chilkat::Dkim::new();
let domain = "example.com".to_string();
let selector = "mail2026".to_string();
let key_text = chilkat::StringBuilder::new();
success = key_text.load_file("dkim-public.pem", "utf-8").is_ok();
if !success {
    println!("Could not read the trusted public PEM.");
    return;
}

success = verifier.load_public_key(&selector, &domain, &key_text.get_as_string().unwrap_or_default()).is_ok();
if !success {
    println!("{}", verifier.last_error_text());
    return;
}

// signed.eml from the signing example has our newly added signature at index zero.
if verifier.num_dkim_sigs(&message) == 0 {
    println!("No DKIM signature in signed.eml.");
    return;
}

success = verifier.dkim_verify(0, &message).is_ok();
if !success {
    println!("{}", verifier.last_error_text());
    println!("{}", verifier.verify_info());
    return;
}

let info = chilkat::JsonObject::new();
success = info.load(&verifier.verify_info()).is_ok();
if (!success) || ((!info.string_of_equals("domain", &domain, true)) || (!info.string_of_equals("selector", &selector, true))) {
    println!("The first signature is not from the selected domain and selector.");
    return;
}

println!("The configured public key verifies this message.");