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

Prepare a DKIM public key and DNS TXT record

See more DKIM Examples

Start with an RSA signing key in dkim-private.pem. This writes its public half to dkim-public.pem for local testing and prints the TXT record to enter at your DNS provider. Keep the private PEM on the signer. Use a 2048-bit or larger RSA key for a new deployment. DKIM RSA key sizes.

Use your signing domain and selector in place of example.com and mail2026. A provider that appends the zone usually wants just mail2026._domainkey as the name. If the value exceeds 255 bytes, split it into adjacent character strings within one TXT record. DNS joins those strings without adding spaces. Publish a new selector before rotating keys and keep the old record while previously signed mail is still in transit.

Chilkat Rust Downloads

Rust

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

// 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 public_key = chilkat::PublicKey::new();
if private_key.to_public_key(&public_key).is_err() {
    println!("{}", private_key.last_error_text());
    return;
}

if public_key.key_type() != "rsa" {
    println!("Choose an RSA key for this rsa-sha256 example.");
    return;
}

// false selects SubjectPublicKeyInfo. The p= value is base64 DER, on one line.
let p_value = public_key.get_encoded(false, "base64").unwrap_or_default();
if p_value == "" {
    println!("{}", public_key.last_error_text());
    return;
}

if public_key.save_pem_file(false, "dkim-public.pem").is_err() {
    println!("{}", public_key.last_error_text());
    return;
}

let selector = "mail2026".to_string();
let domain = "example.com".to_string();
println!("TXT owner: {}._domainkey.{}", selector, domain);
println!("TXT value: v=DKIM1; k=rsa; p={}", p_value);
println!("Saved dkim-public.pem.");