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

Check a published DKIM selector against a signed message

See more DKIM Examples

After publishing a selector, run this check with a message signed by its matching private key. Start with a fresh verifier, fetch the public key from DNS, and then verify the message with that same object. DNS lookup alone shows only that a usable key was returned. Signature verification also checks that it matches what the signer used.

Set the signing domain and selector exactly as they appear in the signature. Applications processing many messages can reuse the verifier for that selector. After a key change, use a new verifier and allow for the DNS resolver’s TTL.

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();
success = verifier.prefetch_public_key(&selector, &domain).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 published key verifies this message for {}._domainkey.{}", selector, domain);