Sample code for 30+ languages & platforms
Lua

Create a DKIM-Signature for a MIME Message

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to a complete MIME message held in a BinData, modifying it in place. The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount properties configure the generated signature.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: The signature is computed over a hash of the body plus a chosen set of headers, so the message must be completely built — every header, encoding, and boundary — before signing; any later change invalidates it. The d= (domain) and s= (selector) tags tell a verifier where to find the public key: at selector._domainkey.domain in DNS. relaxed/relaxed canonicalization tolerates the minor whitespace changes mail systems make in transit, which is why it is the common choice.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to
-- a complete MIME message.  The only argument is a BinData holding the MIME, which is modified in
-- place.
--
-- The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount
-- properties configure the signature that is generated.

local dkim = chilkat.Dkim.new()

-- Configure the DKIM signature.
dkim.dkim_domain = "example.com"
dkim.dkim_selector = "myselector"

-- Signing algorithm written to the a= tag.
dkim.dkim_alg = "rsa-sha256"

-- Canonicalization for headers and body, written to the c= tag.
dkim.dkim_canon = "relaxed/relaxed"

-- Colon-separated list of header fields to sign, written to the h= tag.
dkim.dkim_headers = "From:To:Subject:Date:Message-ID"

-- Maximum number of canonicalized body bytes to hash.  0 means the entire body.
dkim.dkim_body_length_count = 0

-- Load the RSA private key and give it to the Dkim object.
local privKey = chilkat.PrivateKey.new()
local success = privKey:load_pem_file("qa_data/dkim_private.pem")
if not success then
    print(privKey.last_error_text)
    return
end

success = dkim:set_dkim_private_key(privKey)
if not success then
    print(dkim.last_error_text)
    return
end

-- Load the complete MIME message to be signed.  It must already contain all headers, transfer
-- encodings, MIME boundaries, and body bytes.
local mimeData = chilkat.BinData.new()
success = mimeData:load_file("qa_data/message.eml")
if not success then
    print(mimeData.last_error_text)
    return
end

-- Sign.  The DKIM-Signature header is prepended to the MIME in place.
success = dkim:dkim_sign(mimeData)
if not success then
    print(dkim.last_error_text)
    return
end

-- Save the signed message.
success = mimeData:write_file("qa_output/signed.eml")
if not success then
    print(mimeData.last_error_text)
    return
end

print("Message signed.")