Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Dkim
oleobject loo_PrivKey
oleobject loo_MimeData

li_Success = 0

//  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.

loo_Dkim = create oleobject
li_rc = loo_Dkim.ConnectToNewObject("Chilkat.Dkim")
if li_rc < 0 then
    destroy loo_Dkim
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Configure the DKIM signature.
loo_Dkim.DkimDomain = "example.com"
loo_Dkim.DkimSelector = "myselector"

//  Signing algorithm written to the a= tag.
loo_Dkim.DkimAlg = "rsa-sha256"

//  Canonicalization for headers and body, written to the c= tag.
loo_Dkim.DkimCanon = "relaxed/relaxed"

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

//  Maximum number of canonicalized body bytes to hash.  0 means the entire body.
loo_Dkim.DkimBodyLengthCount = 0

//  Load the RSA private key and give it to the Dkim object.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivKey.LoadPemFile("qa_data/dkim_private.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Dkim
    destroy loo_PrivKey
    return
end if

li_Success = loo_Dkim.SetDkimPrivateKey(loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Dkim.LastErrorText
    destroy loo_Dkim
    destroy loo_PrivKey
    return
end if

//  Load the complete MIME message to be signed.  It must already contain all headers, transfer
//  encodings, MIME boundaries, and body bytes.
loo_MimeData = create oleobject
li_rc = loo_MimeData.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_MimeData.LoadFile("qa_data/message.eml")
if li_Success = 0 then
    Write-Debug loo_MimeData.LastErrorText
    destroy loo_Dkim
    destroy loo_PrivKey
    destroy loo_MimeData
    return
end if

//  Sign.  The DKIM-Signature header is prepended to the MIME in place.
li_Success = loo_Dkim.DkimSign(loo_MimeData)
if li_Success = 0 then
    Write-Debug loo_Dkim.LastErrorText
    destroy loo_Dkim
    destroy loo_PrivKey
    destroy loo_MimeData
    return
end if

//  Save the signed message.
li_Success = loo_MimeData.WriteFile("qa_output/signed.eml")
if li_Success = 0 then
    Write-Debug loo_MimeData.LastErrorText
    destroy loo_Dkim
    destroy loo_PrivKey
    destroy loo_MimeData
    return
end if

Write-Debug "Message signed."


destroy loo_Dkim
destroy loo_PrivKey
destroy loo_MimeData